I am trying to rename a file using find
command.
I am trying to rename file-a to file-10.
To do this I first tried below command:
sps@sps-Inspiron-N5110:~$ find ~ -type f -name test-a -exec mv test-10 '{}' ';'
mv: cannot stat `test-10': No such file or directory
sps@sps-Inspiron-N5110:~$
Then I tried below:
sps@sps-Inspiron-N5110:~$ find ~ -type f -name test-a -exec mv test-a test-10 '{}' ';'
mv: target `/home/sps/test-a' is not a directory
sps@sps-Inspiron-N5110:~$
Now I cant think how to do that with find
. I am trying to do this with find
, because I will have many directories with same filename, and I want to change all the test-a
to test-10
in one command. Anyone please suggest.
Thanks.
The syntax of
mv
ismv <source> <target>
, so the final command thatfind
executes should look like:So, the first guess would be try:
However, this will fail, since
{}
gets expanded to the full path andmv
is still run in the current directory, resulting in all the files being moved to your current directory and getting overwritten. To avoid this, you can use-execdir
so thatmv
gets executed in the directory where the file was found:Or, since the filename is always the same: