Doug McMurray
Doug McMurray

Reputation: 65

Renaming all files within subdirectories

I am trying to rename all the files with certain regex patterns within the same subdirectory. Right now, I am in the current directory (above all the sub directories) and am running the following code:

for d in */ ; do
    cp $d*hw4*.pl $dhw4.pl
done

I wish to find all the files with hw4 in them, and rename them to hw4.pl and keep them in the same directory. Could someone assist me with this? I know you can do this with the mv command, but I don't want to make a mistake, so I am using copy for now.

Thank you.

Upvotes: 1

Views: 210

Answers (1)

hek2mgl
hek2mgl

Reputation: 157992

I suggest to use find, like this:

find . -type f -name '*hw4*' -execdir mv {} hw4.pl \;

Upvotes: 3

Related Questions