我有一个文件夹,里面全是‘四位数字,空格,字母数字字符串.txt’格式的文本文件.例如:
$ touch '0001 jko0001.txt' '0002 jko0002.txt' '0003 jko0003.txt'
$ ls
'0001 jko0001.txt' '0002 jko0002.txt' '0003 jko0003.txt'
我想要重命名这些文件,以便删除前导数字和空格.因为我有很多文件,所以我使用find
将文件名传递给rename
.我try 使用以下命令执行此操作:
find . -type f -name '*.txt' -print0 | xargs -0 rename -n -- 's/^\d{4}\s+//' {} +
个
然而,这失败了.(是的,-n
仅用于打印更改,而不重命名文件.即使我移除它,它也会失败).
有趣的是,如果我将命令分成几个部分,它确实可以工作:
$ find . -type f -name '*.txt'
./0003 jko0003.txt
./0002 jko0002.txt
./0001 jko0001.txt
$ rename -n -- 's/^[0-9]{4}\s+//' *.txt
0001 jko0001.txt -> jko0001.txt
0002 jko0002.txt -> jko0002.txt
0003 jko0003.txt -> jko0003.txt
$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
但当与xargs
结合在一起时,它就失败了.为什么?
此外,我甚至不能让它在find
的-execdir
分下工作:
find . -type f -name '*.txt' -execdir rename -n -- 's/^\d{4} //' {} \;
个
这些都不管用.
find . -type f -name '*.txt' -print0 | xargs -0 rename -n -- 's/^\d{4}\s+//' "{}" +
find . -type f -name '*.txt' -print0 | xargs -0 rename -n -- 's/^[0-9]{4}\s+//' "{}" +
find . -type f -name '*.txt' -execdir rename -n -- 's/^\d{4} //' {} \;
find . -type f -name '*.txt' -execdir rename -n -- 's/^\d{4} //' '{}' \;
提前谢谢!