This question already has an answer here:

I have about 2855 folders containing a large number of images and I want to copy or move all the images into one folder.

Please provide a detailed explanation.

marked as duplicate by Tom Brossman, Melebius, Fabby, George Udosen, pzkpfw Nov 20 '18 at 9:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Can you give an idea of the naming scheme (if any) of the images? Are there other files in these directories that you do not want to move, or do you just want to move ALL the files from your 2855 folders into one? If you only want to move images, can they be identified by name (for example, do they end with .png)? To prevent files being overwritten when doing this, you should be confident that they all have unique names. It's safest to specify a new naming scheme. Can you provide any further info in an edit to your question, preferably with a sample of the directory structure with files – Zanna Nov 16 '18 at 21:07
  • 5
    Be aware that nautilus will choke if you try to open a folder with such large amounts of files in a single folder. – Jacob Vlijm Nov 16 '18 at 21:22

This is a Very Bad Idea.

However, assuming that your image names are unique (although duplicates will be handled by using mv --backups-numbered), and all your image-containing directories are under a directory called $HOME/source, and contain ONLY images (no files that should not be moved), and the the filenames might contain silly characters, and that the destination is $HOME/destination, then...

First read man find, man xargs and man mv. Then a simple

find $HOME/source -type f -print0 |\
    xargs -0 --no-run-if-empty mv --target-directory=$HOME/destination/ --backup=numbered

Reread man find, man xargs and man mv for detailed explanation.

After this finishes, you'll be left with a whole lot of empty directories under $HOME/source, which can be removed with

find $HOME/source -depth -type d -print0 | xargs -0 --no-run-if-empty rmdir

Read man rmdir.

  • The removal of the directories can also be written as find $HOME/source -type d -delete. – PerlDuck Nov 17 '18 at 16:14
  • @PerlDuck -delete will delete non-empty directories! rmdir will only delete empty directories. – waltinator Nov 17 '18 at 16:56
  • True. But if I understood your first find command correctly, the directories are empty when it has run. – PerlDuck Nov 17 '18 at 17:22
  • Honestly, I would create hardlinks instead of moving the files as a first approach and then see how such a crowded target directory behaves. Jacob said in a comment that "nautilus will choke". With hardlinks this can easily be undone. The command would be find $HOME/source -type f -exec ln --target-directory=$HOME/destination --backup=numbered {} +. No xargs or quoting is required because find calls ln with {} properly replaced by each filename and the + instead of \; calls it with as many args as fit into the commandline. – PerlDuck Nov 17 '18 at 17:53
  • It overwrites the files with same names. Tried it. – VeeJay Dec 15 '18 at 16:58

Change directory cd to the directory where all the folders are. This will move all files in your working folder and subfolders to one folder. Different files with same name will remain in the original folders

Run the commands with caution. Running from a wrong location may cause loss of important files.


find . -type f -print0 | xargs -0 -I _ mv -vn _ /path/to/copy/

if you want files with some ext e.g. .jpg


find . \( -name '*.jpg' -or -name '*.png' \) -type f -print0 | xargs -0 -I _ mv -vn _ /path/to/copy/
  • 1
    Why sort? Why a shell loop, rather than xargs? – waltinator Nov 16 '18 at 15:31
  • Thanks waltinator. – VeeJay Nov 16 '18 at 15:51
  • Why xargs -I _? You could use mv's --target-directory=DIRECTORY switch, e.g.: xargs -0 mv --target-directory=/path/to/copy – waltinator Dec 15 '18 at 14:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.