if you do not mind using Perl then you can do anything you like.
for example for renaming all files with this structure:
dir > find .
.
./00_file.txt
./01_file.txt
./02_file.txt
./dir1
./dir1/00_file.txt
./dir1/01_file.txt
./dir1/02_file.txt
./dir2
./dir2/00_file.txt
./dir2/01_file.txt
./dir2/02_file.txt
./dir3
./dir3/00_file.txt
./dir3/01_file.txt
./dir3/02_file.txt
./find.log
then save your list in a file like: find.log
now you can use Perl
dir > perl -lne '-f && ($old=$_) && s/file/ABCD/g && print "$old => $_"' find.log
./00_file.txt => ./00_ABCD.txt
./01_file.txt => ./01_ABCD.txt
./02_file.txt => ./02_ABCD.txt
./dir1/00_file.txt => ./dir1/00_ABCD.txt
./dir1/01_file.txt => ./dir1/01_ABCD.txt
./dir1/02_file.txt => ./dir1/02_ABCD.txt
./dir2/00_file.txt => ./dir2/00_ABCD.txt
./dir2/01_file.txt => ./dir2/01_ABCD.txt
./dir2/02_file.txt => ./dir2/02_ABCD.txt
./dir3/00_file.txt => ./dir3/00_ABCD.txt
./dir3/01_file.txt => ./dir3/01_ABCD.txt
./dir3/02_file.txt => ./dir3/02_ABCD.txt
how it works?
- read the file
- let only file; not directory or other stuffs with:
-f
- then save the old name:
($old=$_)
- then doing substitution with:
s///g
operator
- finally rename the old file to new one that is
$_
NOTE I used print
and you should use rename
like this:
dir > perl -lne '-f && ($old=$_) && s/file/ABCD/g && rename $old, $_' find.log
dir > find .
.
./00_ABCD.txt
./01_ABCD.txt
./02_ABCD.txt
./dir1
./dir1/00_ABCD.txt
./dir1/01_ABCD.txt
./dir1/02_ABCD.txt
./dir2
./dir2/00_ABCD.txt
./dir2/01_ABCD.txt
./dir2/02_ABCD.txt
./dir3
./dir3/00_ABCD.txt
./dir3/01_ABCD.txt
./dir3/02_ABCD.txt
./find.log
NOTE Since the find
returns list of all files and sub-directories, with this technique Perl renames all the files! Thus first use print
then rename
. And your pattern can be: -.*(?=\.)
. In fact:
s/-.*(?=\.)//g