1

I have an hierarchy of folders like this:

MainFolder
|
| - 01
| - 02
| - 03
| - 04
| - 05
| - 06
| - 07
| - 08
| - 09
| - 10

And inside each of this ten folders, I have other 10 folders, and so on. In total, I have 10 levels.

I would like to move ALL files, including ALL possible extensions (including zip and rar) to a folder called FinalFolder.

I'm trying to use the following command:

find MainFolder -type f -print0 | xargs -0 mv -t FinalFolder

And is "working". In total, I'm catching 5k files, but I know that I have 25k files, so is not catching all. What could be the problem? And more important, how to fix it?

10
1

Use a simpler find command to flatten the source directory.

Alert: Make sure the target directory exists first!! (or you could end up with one file named FinalFolder with only one of your files in it)

 find MainFolder -type f -exec mv \{\} FinalFolder \;

EDIT

This can overwrite files if you have two or more that have a name collision. If you want to avoid that you can get version numbers appended by using the cp command in place of mv. This has the side effect that the original directory tree remains intact, so one might need to go and delete it after processing.

 find MainFolder -type f -exec cp --backup=numbered \{\} FinalFolder \;

If this keeps asking for confirmation it is probably due to the cp command being aliased to cp -i. In which case use /bin/cp in place of cp

6
  • you know, it'd probably be safer to use ln to make a new link for each file inside the target folder, then rm -rf the source folder when you were happy. – infixed Mar 16 '16 at 20:04
  • nevermind. too hard to make basename. "{}" does not seem to expand right inside backticks executed by find – infixed Mar 16 '16 at 20:15
  • apparently find MainFolder -type f -exec ln \{\} FinalFolder \; will work without needing the basename command to explicitly creating a base name though – infixed Mar 16 '16 at 20:25
  • One question: If i have a bunch of files with the same name, what happens? Can i automatically rename it? – FXux Mar 16 '16 at 20:31
  • what will happen now is the last one done will overwrite the previous. You could use mv with the -n no-clobber switch and the other files would stay in MainFolder. But if you want a version number, you probably should start a new question – infixed Mar 16 '16 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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