2

I have multiple files in multiple folders under one directory that need to be in one folder. Is there a command line that can help me accomplish this?

  • Give us some more hints..do you want to move all files in all subdirectories inside a directory?, or the directories are randomly located (how to find them?), do you want to move specific files or all of them inside the directories? also do you want to move them to a existing directory or a new directory? – heemayl Jun 8 '15 at 21:35
  • I'm not sure how to make the question simpler, but I'll give it a shot. There are over 50 folders (all containing files) that I need merged into one. – Junior Cortez Jun 8 '15 at 21:48
  • Are these folders distributed randomly or under the same directory? if distributed randomly then do they contain a pattern in their names (how to find them)? if under the same directory then does the directory contain any other file/directory that needs to be excluded? – heemayl Jun 8 '15 at 21:52
  • All under one directory, no other files or folders to be excluded. – Junior Cortez Jun 8 '15 at 22:02
3

Using find + xargs + mv:

find . -type f -print0 | xargs -0 -I file mv --backup=numbered file .

This will move all the files in the current working directory and its subdirectories (recursively) into the current working directory, numbering files with the same filename numerically in order to avoid overwrites of files with the same filename.

Sample result on a tmp folder with a 1, 2 and 3 subfolders each containing a 1.ext, 2.ext and 3.ext file:

ubuntu@ubuntu:~/tmp$ tree
.
├── 1
│   ├── 1.ext
│   ├── 2.ext
│   └── 3.ext
├── 2
│   ├── 1.ext
│   ├── 2.ext
│   └── 3.ext
└── 3
    ├── 1.ext
    ├── 2.ext
    └── 3.ext

3 directories, 9 files
ubuntu@ubuntu:~/tmp$ find . -type f -print0 | xargs -0 -I file mv --backup=numbered file .
ubuntu@ubuntu:~/tmp$ tree
.
├── 1
├── 1.ext
├── 1.ext.~1~
├── 1.ext.~2~
├── 2
├── 2.ext
├── 2.ext.~1~
├── 2.ext.~2~
├── 3
├── 3.ext
├── 3.ext.~1~
└── 3.ext.~2~

3 directories, 9 files
0

You can do this using find:

find . -type f -exec mv -i -t new_dir {} +

At first create the directory (mkdir new_dir) where you want to move all the files, here we are moving all the files in the ./new_dir directory.

  • find . -type f will find all the files under all the directories under the current directory, so you need to cd into the directory that contains all the subdirectories or you can use the absolute path e.g. ~/foo/bar

  • The -exec predicate of find will execute the mv command that will move all the files found to the new_dir directory. Once again you can use the absolute path.

  • mv -i will prompt you before overwriting a file.

If the new directory is located elsewhere, use absolute paths:

find ~/path/to/dir -type f -exec mv -i -t ~/path/to/new_dir {} +
0

you can use the command:

find . -type f -execdir mv '{}' /parent-dir \;

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.
0

If your directory structure looks like

dir root

  • dir A
    • file a
    • file b
  • dir B
    • file c
    • file d

and so on

you can do a simple

mv **/* .

to move all files at depth 1 to the root dir. Simple and Elegant!

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.