linux命令rename , tr 的命令使用

1:linux 有时需要进行批量修改文件名,这时就使用到了rename命令很方便

2:在写shell脚本的时候,经常需要替换字符串,这个使用使用tr命令很方便


rename命令,先使用man命令查看使用方法

[root@sftp01 shell]# man rename 
RENAME(1)                  Linux Programmer’s Manual                 RENAME(1)


NAME
       rename - Rename files


SYNOPSIS
       rename from to file...


DESCRIPTION
       rename will rename the specified files by replacing the first occurrence of from in their name by to.


       For example, given the files foo1, ..., foo9, foo10, ..., foo278, the commands


              rename foo foo0 foo?
              rename foo foo0 foo??


       will turn them into foo001, ..., foo009, foo010, ..., foo278.


       And
              rename .htm .html *.htm


       will fix the extension of your html files.


SEE ALSO
       mmv(1), mv(1)


                                1 January 2000                       RENAME(1)
[root@sftp01 shell]# 

从上面解释看,rename 的命令格式为 rename   需要修改的字符     需要修改成的字符   需要修改的文件

eg:

[root@sftp01 test]# touch aa01.txt

[root@sftp01 test]# touch aa02.txt

[root@sftp01 test]# touch aa03.txt
[root@sftp01 test]# touch aa11.txt
[root@sftp01 test]# 
[root@sftp01 test]# rename aa bb aa*.txt
[root@sftp01 test]# ll
总计 0
-rw-r----- 1 root root 0 04-08 17:01 bb01.txt
-rw-r----- 1 root root 0 04-08 17:02 bb02.txt
-rw-r----- 1 root root 0 04-08 17:02 bb03.txt
-rw-r----- 1 root root 0 04-08 17:02 bb11.txt
[root@sftp01 test]# rename 0 1 bb0*
[root@sftp01 test]# ll
总计 0
-rw-r----- 1 root root 0 04-08 17:01 bb11.txt
-rw-r----- 1 root root 0 04-08 17:02 bb12.txt
-rw-r----- 1 root root 0 04-08 17:02 bb13.txt
[root@sftp01 test]# rename 1 5 bb11.txt
[root@sftp01 test]# ll
总计 0
-rw-r----- 1 root root 0 04-08 17:02 bb12.txt
-rw-r----- 1 root root 0 04-08 17:02 bb13.txt
-rw-r----- 1 root root 0 04-08 17:01 bb51.txt
[root@sftp01 test]# 


tr命令,先通过man命令查询使用方法

[root@MGHJ-YW-mg-sftp01 test]# man tr
TR(1)                            User Commands                           TR(1)


NAME
       tr - translate or delete characters


SYNOPSIS
       tr [OPTION]... SET1 [SET2]


DESCRIPTION
       Translate, squeeze, and/or delete characters from standard input, writing to standard output.


       -c, -C, --complement
              first complement SET1


       -d, --delete
              delete characters in SET1, do not translate


       -s, --squeeze-repeats
              replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character


       -t, --truncate-set1
              first truncate SET1 to length of SET2


       --help display this help and exit


       --version
              output version information and exit


       SETs are specified as strings of characters.  Most represent themselves.  Interpreted sequences are:


       \NNN   character with octal value NNN (1 to 3 octal digits)


       \\     backslash


       \a     audible BEL


       \b     backspace


       \f     form feed


       \n     new line


       \r     return


       \t     horizontal tab


       \v     vertical tab


       CHAR1-CHAR2
              all characters from CHAR1 to CHAR2 in ascending order


       [CHAR*]
              in SET2, copies of CHAR until length of SET1


       [CHAR*REPEAT]
              REPEAT copies of CHAR, REPEAT octal if starting with 0


       [:alnum:]
              all letters and digits


       [:alpha:]
              all letters


       [:blank:]
              all horizontal whitespace


       [:cntrl:]
              all control characters


       [:digit:]
              all digits


       [:graph:]
              all printable characters, not including space


       [:lower:]
              all lower case letters


       [:print:]
              all printable characters, including space


       [:punct:]
              all punctuation characters


       [:space:]
              all horizontal or vertical whitespace


       [:upper:]
              all upper case letters


       [:xdigit:]
              all hexadecimal digits


       [=CHAR=]
              all characters which are equivalent to CHAR


       Translation occurs if -d is not given and both SET1 and SET2 appear.  -t may be used only when translating.  SET2 is extended to length of SET1
       by repeating its last character as necessary.  Excess characters of SET2 are ignored.  Only [:lower:] and [:upper:] are guaranteed to expand in
       ascending  order;  used  in SET2 while translating, they may only be used in pairs to specify case conversion.  -s uses SET1 if not translating
       nor deleting; else squeezing uses SET2 and occurs after translation or deletion.


AUTHOR
       Written by Jim Meyering.


REPORTING BUGS
       Report bugs to .


COPYRIGHT
       Copyright © 2006 Free Software Foundation, Inc.
       This   is   free   software.    You   may   redistribute   copies   of   it   under   the   terms   of   the   GNU   General   Public   License
       .  There is NO WARRANTY, to the extent permitted by law.


SEE ALSO
       The full documentation for tr is maintained as a Texinfo manual.  If the info and tr programs are properly installed at your site, the command


              info tr


       should give you access to the complete manual.


tr 5.97                            May 2011                              TR(1)

从解释来看,tr命令是一个强大的字符串替换命令,主要有三个参数,开始进行总结和测试

针对参数

-c或——complerment:取代所有不属于第一字符集的字符;
-d或——delete:删除所有属于第一字符集的字符;
-s或--squeeze-repeats:把连续重复的字符以单独一个字符表示;
-t或--truncate-set1:先删除第一字符集较第二字符集多出的字符。

进行命令测试: 

[root@sftp01 test]# echo "TEST TEST" | tr 'A-Z' 'a-z'
test test
[root@sftp01 test]# echo "TEST 123 TEST 123" | tr -d '1 2 3'
TESTTEST
[root@sftp01 test]# echo "TESTTT 123 TEESSTT" | tr -s 'TES'
TEST 123 TEST
[root@sftp01 test]# 

[root@sftp01 test]# cat 11.txt 
11
2211
2211
2211
2211
2211
2211
2211
2211
2211
2211
22
[root@sftp01 test]# cat 11.txt  | tr '\n' ' '

11 2211 2211 2211 2211 2211 2211 2211 2211 2211 2211 22

[root@sftp01 test]# 

[root@sftp01 test]# 








你可能感兴趣的:(linux)