I want to rename all the files in a folder which starts with 123_xxx.txt
to xxx.txt
.
For example, my directory has:
123_xxx
prename s/^123_// 123_*
See prename in the official Perl5 wiki. Copy for your convenience:
prename
- A script that renames files according to a regular expression. (Where was the original published?) Originally named rename, it is found mostly as prename because the original one clashes with the rename command from the util-linux
package. Numerous forks and reimplementations:
You can make a little bash script for that.
Create a file named recursive_replace_filename
with this content :
#!/bin/bash
if test $# -lt 2; then
echo "usage: `basename $0` <to_replace> <replace_value>"
fi
for file in `find . -name "*$1*" -type f`; do
mv "'$file'" "${file/'$1'/'$2'}"
done
Make executable an run:
$ chmod +x recursive_replace_filename
$ ./recursive_replace_filename 123_ ""
Keep note that this script can be dangerous, be sure you know what it's doing and in which folder you are executing it, and with which arguments. In this case, all files in the current folder, recursively, containing 123_
will be renamed.
Do it this way:
find . -name '123*.txt' -type f -exec bash -c 'mv "$1" "${1/\/123_//}"' -- {} \;
Advantages:
In case you want to replace string in file name called foo to bar you can use this in linux ubuntu, change file type for your needs
find -name "*foo*.filetype" -exec rename 's/foo/bar/' {} ";"
Using rename from util-linux 2.28.2 I had to use a different syntaxt:
find -name "*.txt" -exec rename -v "123_" "" {} ";"
using the examples above, i used this to replace part of the file name... this was used to rename various data files in a directory, due to a vendor changing names.
find . -name 'o3_cloudmed_*.*' -type f -exec bash -c 'mv "$1" "${1/cloudmed/revint}"' -- {} \;