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 08 '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 08 '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 08 '15 at 21:52
-
All under one directory, no other files or folders to be excluded. – Junior Cortez Jun 08 '15 at 22:02
-
https://unix.stackexchange.com/questions/52814/flattening-a-nested-directory – Ferroao Dec 12 '17 at 14:58
5 Answers
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
- 35,535
- 13
- 101
- 151
-
-
1To keep the extension at the end (many programs use the extension to recognize files) you can [rename the files in another step](https://unix.stackexchange.com/a/371393/85530) with: `rename 's/((?:\..+)?)\.~(\d+)~$/_$2$1/' *.~*~` – Paul Rougieux Sep 23 '21 at 12:15
-
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!
- 151
- 1
- 4
-
-
This looks good, but I run into `mv: cannot move
: Directory not empty` – Cron Merdek Mar 13 '20 at 18:06
find /path/to/root/directory -type f -exec mv {} /path/to/destination/folder/ \;
Let's break down the command and understand each part:
find: The command used to search for files and directories within a specified location./path/to/root/directory: Replace this with the actual path to your root directory where the subdirectories are located.-type f: Specifies that we are looking for regular files (not directories).-exec mv {} /path/to/destination/folder/ \;: Executes the mv command on each found file ({}). It moves (mv) each file to the specified destination folder (/path/to/destination/folder/).
- 101
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 fwill find all the files under all the directories under the current directory, so you need tocdinto the directory that contains all the subdirectories or you can use the absolute path e.g.~/foo/barThe
-execpredicate offindwill execute themvcommand that will move all the files found to thenew_dirdirectory. Once again you can use the absolute path.mv -iwill 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 {} +
- 90,425
- 20
- 200
- 267
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.
- 82,867
- 54
- 239
- 271