I've been reading about Bulk rename, change prefix and would try with my own files.
In this case, I would like to remove Old
and replace it with New
Test Files
01. Old Name.txt
02. Old Name.txt
03. Old Name.txt
Attempt 1
for f in *.txt
do
mv "$f" "New${f#Old}"
done
Output 1
New01. Old Name.txt
New02. Old Name.txt
New03. Old Name.txt
Attempt 2
for i in *.txt
do
mv ${i} ${i/#Old/New}
done
Output 2 (no changes)
user@linux:~$ for i in *.txt
> do
> mv ${i} ${i/#Old/New}
> done
mv: target 'Name.txt' is not a directory
mv: target 'Name.txt' is not a directory
mv: target 'Name.txt' is not a directory
user@linux:~$
What's wrong with my solution?
Desired Output
01. New Name.txt
02. New Name.txt
03. New Name.txt
rename
command. It's just as "built-in" as themv
command. i.e. neither of them are "built-in" to bash, they're both external commands run from the shell. And, like GNUcoreutils
(which containsmv
), it's available pre-packaged for most, if not all, linux distributions.rename 's/Old/New/' *.txt
– cas Oct 05 '19 at 06:10mv "${i}" "${i/Old/New}"
. See Why does my shell script choke on whitespace or other special characters? – cas Oct 05 '19 at 06:13mv: '01. Old Name.txt' and '01. Old Name.txt' are the same file
– Oct 05 '19 at 06:21rename
is not built-in .. need to install it. I've problem with a few servers whereby we cannot simply install anything new to it`Command 'rename' not found, but can be installed with:
sudo apt install rename `
– Oct 05 '19 at 06:21mv
is not "built-in" either. – cas Oct 05 '19 at 06:24mv "${i}" "${i/Old/New}"
as i suggested, or with a#
likemv "${i}" "${i/#Old/New}"
as in your attempt 2? The former will work. The latter won't. – cas Oct 05 '19 at 06:27mv: '01. Old Name.txt' and '01. Old Name.txt' are the same file
– Oct 05 '19 at 06:34