Monday, November 3, 2014

move all files in current directory into a subdirectory in the current directory

I needed to move all the files in the current working directory (mount point) into a sub-directory. I hope this helps others, and me in the future.

Use the -maxdepth 1 option to find, instead of the "*" and ".*", something like this:

mkdir ./dest_dir
find . -maxdepth 1 | grep -v dest_dir | xargs -i mv {} ./dest_dir

  -i,--replace=[R]             Replace R in initial arguments with names
                               read from standard input. If R is
                               unspecified, assume {}


addition per spiceworks! (Leecallen)

Nice, and useful.
Slight simplification - explicitly tell find that you are only interested in files, and then remove the grep command:

     find . -maxdepth 1 -type f | xargs -i mv {} ./dest_dir;

or just for kicks:

     find . -maxdepth 1 -type f -exec mv {} ./dest_dir \;