Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and rename a directory

I am trying to find and rename a directory on a linux system.

the folder name is something like : thefoldername-23423-431321

thefoldername is consistent but the numbers change every time.

I tried this:

find . -type d -name 'thefoldername*' -exec mv {} newfoldername \;

The command actually works and rename that directory. But I got an error on terminal saying that there is no such file or directory.

How can I fix it?

like image 760
noway Avatar asked Oct 23 '12 21:10

noway


2 Answers

It's a harmless error which you can get rid of with the -depth option.

find . -depth -type d -name 'thefoldername*' -exec mv {} newfoldername \;

Find's normal behavior is to process directories and then recurse into them. Since you've renamed it find complains when it tries to recurse. The -depth option tells find to recurse first, then process the directory after.

like image 63
John Kugelman Avatar answered Oct 16 '22 12:10

John Kugelman


It's missing the -execdir option! As stated in man pages of find:

-execdir command {};

Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.

find . -depth -type d -name 'thefoldername*' -execdir mv {} newfoldername \;

like image 39
Amadeu Barbosa Avatar answered Oct 16 '22 12:10

Amadeu Barbosa