操作系统:CentOS Linux release 7.4.1708 (Core)
需求:需要将.txt文件的后缀改为.log
需要命令:rename
[root@test test]# rename -h
Usage:
rename [options] expression replacement file...
Options:
-v, --verbose explain what is being done
-s, --symlink act on symlink target
-h, --help display this help and exit
-V, --version output version information and exit
For more details see rename(1).
可以看到rename的使用方法是 rename [选项] 替换前的内容 替换后的内容 替换文件
试验样例:
[root@test test]# touch {1..10}.txt
[root@test test]# ll
total 0
-rw-r--r--. 1 root root 0 Feb 26 10:59 10.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 1.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 2.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 3.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 4.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 5.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 6.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 7.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 8.txt
-rw-r--r--. 1 root root 0 Feb 26 10:59 9.txt
[root@test test]# rename -v .txt .log *.txt
`10.txt' -> `10.log'
`1.txt' -> `1.log'
`2.txt' -> `2.log'
`3.txt' -> `3.log'
`4.txt' -> `4.log'
`5.txt' -> `5.log'
`6.txt' -> `6.log'
`7.txt' -> `7.log'
`8.txt' -> `8.log'
`9.txt' -> `9.log'
[root@test test]# ll
total 0
-rw-r--r--. 1 root root 0 Feb 26 10:59 10.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 1.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 2.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 3.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 4.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 5.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 6.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 7.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 8.log
-rw-r--r--. 1 root root 0 Feb 26 10:59 9.log
ps:网上有很多类似这种rename 's/替换前内容/替换后内容/' 需要替换文件,rename并不能真正重命名。具体情况使用rename -h 查看帮助文档进行操作