I have a folder full of text files of the format 'four digits, space, alphanumeric string.txt'. For example:
$ touch '0001 jko0001.txt' '0002 jko0002.txt' '0003 jko0003.txt'
$ ls
'0001 jko0001.txt' '0002 jko0002.txt' '0003 jko0003.txt'
I would like to rename the files so leading digits and space are removed. Since I have a lot of files, I am using find
to pass filenames to rename
. I attempted to do this with the following command:
find . -type f -name '*.txt' -print0 | xargs -0 rename -n -- 's/^\d{4}\s+//' {} +
However, this fails. (Yes, -n
is only to print out changes without renaming the files. It fails even if I remove it).
Interestingly, if I split the command into pieces, it does work:
$ 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)
But when combined with xargs
, it fails. Why?
Also, I can't even get it working with find
's -execdir
:
find . -type f -name '*.txt' -execdir rename -n -- 's/^\d{4} //' {} \;
None of these work.
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} //' '{}' \;
Thanks in advance!