1

I have a large collection of files packed into a two-level directory structure with many, many, many folders.

In other words, I have a folder with a great many subfolders in it. There are no subfolders in these subfolders, however, there are hundreds of files in them.

What I would like to do is move all the files out of these subfolders into the directory that holds the subfolders.

This is a limited resource system, so a way to do it without two copies of all these files having to exist at once would be amazing......

2
  • Can you please edit your post and give an approximated tree view of your current situation, and one of the desired "after"?
    – tink
    Commented Apr 4, 2013 at 5:28
  • So it has been done... Commented Apr 4, 2013 at 5:36

2 Answers 2

1

With GNU tools:

cd that-dir &&
  find . -mindepth 2 ! -type d -exec mv -it . {} +
0

Go to the parent directory and run

mv -- */* .

(-- is only necessary if there are subdirectories whose name begins with -)

If there are files in the subdirectories that begin with . (dot files), or subdirectories whose name begins with ., you'll need to move them as well:

mv -- */* */.[!.]* */..?* .
mv -- .[!.]*/* .[!.]*/.[!.]* .[!.]*/..?* .
mv -- ..?*/* ..?*/.[!.]* ..?*/..?* .

Make sure that there are no conflicts, otherwise the last file you move will silently overwrite a previously-moved file with the same name. Pass the -i option to mv (i.e. mv -i */* .) to be prompted in case of a conflict.

You can then remove the subdirectories:

rmdir */

If you get an error telling you that the command line is too long, you'll have to do it in several steps. The easy way is with the find command:

find */ -type f -exec mv -i {} . \;

or, slightly faster:

find */ -type f -exec sh -c 'mv -i -- "$@" "$0"' . {} +

Even faster, on a non-embedded Linux system:

find . -mindepth 2 -type f -exec mv -it . {} +

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .