Moving large number of files

find folder2 -name '*.*' -exec mv \{\} /dest/directory/ \;


The other find answers work, but are horribly slow for a large number of files, since they execute one command for each file. A much more efficient approach is either to use + at the end of find, or use xargs:

# Using find ... -exec +
find folder2 -name '*.*' -exec mv --target-directory=folder '{}' +

# Using xargs
find folder2 -name '*.*' | xargs mv --target-directory=folder

First, thanks to Karl's answer. I have only minor correction to this.

My scenario:

Millions of folders inside /source/directory, containing subfolders and files inside. Goal is to copy it keeping the same directory structure.

To do that I use such command:

find /source/directory -mindepth 1 -maxdepth 1 -name '*' -exec mv {} /target/directory \;

Here:

  • -mindepth 1 : makes sure you don't move root folder
  • -maxdepth 1 : makes sure you search only for first level children. So all it's content is going to be moved too, but you don't need to search for it.

Commands suggested in answers above made result directory structure flat - and it was not what I looked for, so decided to share my approach.


find folder2 -name '*.*' -exec mv {} folder \;

-exec runs any command, {} inserts the filename found, \; marks the end of the exec command.

Tags:

Bash