Reputation: 2585
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?
Upvotes: 14
Views: 18502
Reputation: 9
Replace 1100 with old_value and 2200 with new_value that you want to replace.
example
for i in $(find . -type d -iname '1100');do echo "mv "$i" "$i"__" >> test.txt; sed 's/1100__/2200/g' test.txt > test_1.txt; bash test_1.txt ; rm test*.txt ; done
Proof
[user@server test]$ ls -la check/
drwxr-xr-x. 1 user user 0 Jun 7 12:16 1100
[user@server test]$ for i in $(find . -type d -iname '1100');do echo "mv "$i" "$i"__" >> test.txt; sed 's/1100__/2200/g' test.txt > test_1.txt; bash test_1.txt ; rm test*.txt ; done
[user@server test]$ ls -la check/
drwxr-xr-x. 1 user user 0 Jun 7 12:16 2200
here __ in sed is used only to change the name it have no other significance
Upvotes: -1
Reputation: 1
.../ABC -> .../BCD
find . -depth -type d -name 'ABC' -execdir mv {} $(dirname $i)/BCD \;
Upvotes: -1
Reputation: 344
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 \;
Upvotes: 7
Reputation: 39
With the previous answer my folders contents are disappeared.
This is my solution. It works well:
for i in
find -type d -name 'oldFolderName'
;
do
dirname=$(dirname "$i")
mv $dirname/oldFolderName $dirname/newFolderName
done
Upvotes: 1
Reputation: 361585
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.
Upvotes: 23