Rename files in subdirectories

5,864

You can use find for this:

find ./your-top-dir/ -iname 'folder.jpg' -execdir mv -i '{}' cover.jpg \;
Share:
5,864

Related videos on Youtube

Elmi
Author by

Elmi

Updated on September 18, 2022

Comments

  • Elmi
    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
      Schwesi over 7 years
      Did you try it recursively? Like mv -R */*/folder.jpg */*/cover.jpg?
    • ganesh
      ganesh over 7 years
    • Elmi
      Elmi over 7 years
      @Schwesi both -R and -r are invalid options for mv :-)
  • Elmi
    Elmi over 7 years
    This deleted all my files folder.jpg :-(
  • A. Loiseau
    A. Loiseau over 7 years
    Damn! So sorry! I forgotten -execdir for the command to take place in the folder itself hence all mv commands gets executed on the calling folder which resulted in moving all to the same ./cover.jpg file in a loop.
  • Scott - Слава Україні
    Scott - Слава Україні over 7 years
    That 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 says dir2.
  • Python Jim
    Python Jim over 7 years
    to revise my answer, to move multiple files with a common part of their name such as the extension use the following
  • Python Jim
    Python Jim over 7 years
    to revise my answer, to move multiple files with a common part of their name such as the extension, use the following:
  • Python Jim
    Python Jim over 7 years
    to 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 - Слава Україні
    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.