我有一组文件,我想一致地重命名,这些文件的名称如下
"System-Log-01-01-2009-NODATA.txt"
"Something-Log-01-01-2009-NODATA.txt"
我希望它们是小写的,yyyymmdd,扩展名为 .log
"system.20090101.log"
"something.20090101.log"
答案1
我曾经编写 perl 脚本来执行此操作,直到我发现了重命名命令。
它接受 perl 正则表达式来执行重命名:
为此,我只需输入两个命令:
rename 's/(\w+)-(\w+)-(\d\d)-(\d{4})-NODATA.txt\$1.$4$3$2.log$//' *
rename 'y/A-Z/a-z/' *.log
但是对于某些发行版来说,rename
没有此功能(参见其手册页),您可能必须安装perl-rename
或prename
。
答案2
答案3
由于我没有重命名命令,因此我依赖于此:
for myfile in /my/folder/*; do
target=$(echo $myfile|sed -e 's/foo/bar/g')
mv "$myfile" "$target"
done
答案4
公平起见:
rename 's/(\w+)-(\w+)-(\d\d)-(\d{4})-NODATA.txt\$1.$4$3$2.log$//' *.txt
给出以下输出:
Use of uninitialized value $4 in regexp compilation at (eval 1) line 1.
Use of uninitialized value $3 in regexp compilation at (eval 1) line 1.
Use of uninitialized value $2 in regexp compilation at (eval 1) line 1.
但:
rename -n 's/(\w+)-\w+-(\d{2})-(\d{2})-(\d{4})-NODATA\.txt$/$1.$4$3$2\.log/' *.txt && rename 'y/A-Z/a-z/' System.20090101.log
给出正确的输出:
System-Log-01-01-2009-NODATA.txt renamed as System.20090101.log
System.20090101.log renamed as system.20090101.log
用 {-v} 替换 {-n} 开关