Move only files recursively from multiple directories to the same directory with mv

I currently have ~ 40k RAW images that are in a nested directory structure. (Some folders have up to 100 subfolders filled with files.) I would like to move them all to one master directory without subfolders. How can this be done with mv ? I know that the -r switch will copy recursively, but it also copies folders, and I don't want to have subdirectories in the main folder.

+6
source share
4 answers

If your photos are in /path/to/photos/ and its subdirectories, and you want to move them to /path/to/master/ , and you want to select them by extension .jpg , .jpg , .png , .png , and etc .:.

 find /path/to/photos \( -iname '*.jpg' -o -iname '*.png' \) -type f -exec mv -nv -t '/path/to/master' -- {} + 

If you don’t want to filter by extension and just move everything (i.e. all files):

 find /path/to/photos -type f -exec mv -nv -t '/path/to/master' -- {} + 

The -n option is to not overwrite existing files (optional if you don't care) and -v to let mv show what it is doing (very optional).

The -t option for mv is to specify the destination directory so that we can add all the files that need to be moved at the end of the command (see the + -exec separator). If your mv does not support -t :

 find /path/to/photos \( -iname '*.jpg' -o -iname '*.png' \) -type f -exec mv -nv -- {} '/path/to/master' \; 

but it will be less efficient since one instance of mv will be created for each file.

Btw, this moves the files, they do not copy them.

Notes.

  • The directory /path/to/master must already exist (it will not be created with this command).
  • Make sure the /path/to/master directory is not in /path/to/photos . That would make this thing awkward!
+14
source

Use the -execdir find option:

 find /path/of/images -type f -execdir mv '{}' /master-dir \; 

By man find :

  -execdir utility [argument ...] ; The -execdir primary is identical to the -exec primary with the exception that utility will be executed from the directory that holds the current file. The filename substituted for the string ``{}'' is not qualified. 

Since -execdir makes find execution of the given command from each directory, therefore, only the base file name is moved without any parent file path.

+1
source
 find <base location of files> -type -f -name \*\.raw -exec mv {} master \; 
0
source

If your hierarchical level is only at one level, here is another way to use StringSolver automated tools:

 mv -a firstfolder/firstfile.raw firstfile.raw 

The -a options immediately apply the same conversion to all similar files at nesting level 1 (i.e. for all other subfolders). If you do not trust the system, you can use other options, such as -e to explain the conversion or -t to check it on all files.

DISCLAIMER: I am a co-author of this work for academic purposes and am working on a bash script renderer. But the system is already available for testing.

0
source

Source: https://habr.com/ru/post/957748/


All Articles