Rename files in subdirectories
5,864
You can use find
for this:
find ./your-top-dir/ -iname 'folder.jpg' -execdir mv -i '{}' cover.jpg \;
Related videos on Youtube
Author by
Elmi
Updated on September 18, 2022Comments
-
Elmi over 1 year
I want to rename some files "folder.jpg" to "cover.jpg". The files itself are located two levels below of the current directory. A simple call
mv */*/folder.jpg */*/cover.jpg
does not work.
So...how can this be done automatically for all subdirectories?
Thanks!
-
Schwesi over 7 yearsDid you try it recursively? Like
mv -R */*/folder.jpg */*/cover.jpg
? -
ganesh over 7 yearsor look at stackoverflow.com/questions/4793892/…
-
Elmi over 7 years@Schwesi both -R and -r are invalid options for mv :-)
-
-
Elmi over 7 yearsThis deleted all my files folder.jpg :-(
-
A. Loiseau over 7 yearsDamn! So sorry! I forgotten
-execdir
for the command to take place in the folder itself hence allmv
commands gets executed on the calling folder which resulted in moving all to the same./cover.jpg
file in a loop. -
Scott - Слава Україні over 7 yearsThat would work if there was only one file — but the question clearly says that there are multiple files, in multiple directories (e.g.,
dir1a/dir2a
,dir1a/dir2b
,dir1b/dir2a
,dir1b/dir2b
, etc.) … … P.S. Your answer isn’t even internally consistent; you say you’re calling the second directory “directory2”, but your command saysdir2
. -
Python Jim over 7 yearsto revise my answer, to move multiple files with a common part of their name such as the extension use the following
-
Python Jim over 7 yearsto revise my answer, to move multiple files with a common part of their name such as the extension, use the following:
-
Python Jim over 7 yearsto revise my answer, to move multiple files with a common part of their name such as an extension, use mv *.x ./dir1/dir2/ where x is the common extension.
-
Scott - Слава Україні over 7 years(1) If you want to revise your answer, edit it. (2) You’re still not getting it. There isn’t a “common part of their name such as the extension”; all the source files have exactly the same name:
folder.jpg
. And there isn’t one target directory; there are multiple directories that contain files (one relevant file per directory), and the files are to be renamed (but not moved into a different directory). You are not answering the question.