Shell script article 4

Source: Internet
Author: User
Sort 013014 # Sort by month... shell script article 4 001 # File verification 002md5sum filename003 004 # sort by array 005 sort-n filename.txt 006 007 # sort by Reverse order 008 sort-r file.txt 009 010 # sort by month 011 012 sort -M file.txt 013 014 # Sort by month (by January, ,) 015 016 # determine whether a file has been sorted 017 #! Bin/bash018 # filename019sort-C file; 020if [$? -Eq 0]; then021 echo sored022else023 echo unsored024fi025 026 # to merge two sorted files 027 sort-m sorted1 sorted2028 029cat data.txt 0301 mac 20000312 winxp 40000323 bsd 10000334 linux 1000034 # There are many ways to sort this text 035 036 # Which column is used for sorting 037 038 sort-nrk 1 data.txt 039 winxp 4000040mac 2000041 linux 12742bsd 1000043 044 # Based on the first column, sort 045 046 # clothes in reverse order 047 sork-k 2 data.txt 048 049bsd 1000050 linux kernel 51mac 2000052 winxp 40000 53 054 # uniq repeated characters 055 056cat uniq. txt | uniq057 058cat uniq.txt | uniq-u # show only strings that have not been repeated 059 060cat uniq.txt | uniq-c # to count the number of times each row appears in the file 061 062cat uniq.txt | uniq- d # Find the duplicate row 063 064cat data.txt 065u in the file: 01: gnu066d: 04: linux067u: 01: bash068u: 01: back069 # We need the string in the middle of 01 and 04 as the only keyboard, ignore the first two characters (-s 2) and use the-w option (-w 2) specify the maximum number of characters used for comparison to select the key 070 071 sort data.txt | uniq-s 2-w 2072 073 074 uniq-z file.txt # Use the uniq command Generate the output 075 076 uniq-z file.txt | xargs-0 rm # Delete all specified files. if a file appears multiple times, the uniq command only writes this file to stdout once 077 078 # temporary file name 079temp_file = "/tmp/var. $"080 echo $ temp_file; 081 #. $ as the added suffix will be extended to the ID082 083 process of the current script # generate a temporary test file 084dd if =/dev/zero bs = 100 k count = 1 of = data. file085 086 # split the file 087-B 10 k data. file088 089 # add the prefix 090 split-B 10 k data to the split file. file-d-a 4 split_file091 092 # If you do not want to cut 093 split-l 10 data according to the data block by default. file 0 94 # Split the file into multiple files. each file contains 10 095 096 lines. # the file extension must be extracted using % 097file_jpg = "sample.jpg" 098 name =$ {file_jpg %. *} 099 echo File name Is $ name; 100 # $ {VAR %. *} indicates that the wildcard on the right of % is deleted. non-greedy mode 101 # $ {VAR %. *} perform greedy mode match 102 103 from the right to the left # use the help letter operation symbol to extract the. jpg image in the file name =$ {file_jpg #. *} 105 echo $ jpg106 #$ {VAR #. *} removes the wildcard located on the right of # from $ VAR. the wildcard matches 107 from the left to the right #$ {VAR ##*.} greedy mode 108 109 # batch rename shell script 110 111 #! /Bin/bash112 # filename rename113 # initialization variable 114 count = 1115 # loop through jpg files in all folders for img *. jpg117 do118 # generate new file name 119 new = image-$ count. $ {img ##*.} 120 # Move File 2>/dev/null standard error pointing to null, prevent printing to terminal 121 mv "$ img" "$ new" 2>/dev/null122 # if wildcard *. if jpg does not match a person or an image file, shell concatenates the wildcard character into a string. From the outputs of images, you can see the .jpg file that does not exist, so 123 # if the mv *. jpg image-x.jpg will certainly produce an error, if ($? -Eq] check the exit status ($ ?) If the latest command is not incorrect, then $? The value of is 0, otherwise 124 # returns a non-zero value. When an error occurs in the mv command, a non-zero value is returned to prevent the resulting Renameing file from being null, and the count is also added to 125 if [$? -Eq 0]; 126 then127 echo "Renameing $ img to $ new" 128 let count + + 129 fi130done131 132 will *. JPG renamed Wei *. jpg133rename *. JPG *. jpg134 135 replace the space in the file name with _ 136 rename's // _/g' * 137 #'s // _/g' to replace the file name, while * wildcard 138 139 for matching file names convert case-insensitive 140 rename 'Y/A-Z/a-z/'* 141 rename 'Y/a-z/A-Z/* 142 143 transfer an mp3 file to a specified directory, you can use 144 find path-type f-name "*. mpe "-exec mv {} target_dir \; 145 146 replace spaces in all file names with characters _ 147 find path-type f-exec rename '/s/_/g '{}\/;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.