There is a directory dir/. It contains subdirectories a-z. I need to move subdirectories a-y into subdirectory z. If that's hard, then not a-z, but by providing a list of directories that need to be moved.
How can I do this in bash?
Use brace expansion : http://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html
For your case, do:
mv {a..y} z/ If you have list of directories, say dir1, dir2, and dir3, then do something like:
mv -t z/ dir1 dir2 dir3 Or maybe:
mv -t z/ dir{1..3} -t option means "target". It is usually used to avoid confusion in cases involving movement of multiple files/directories.{1..15} will print all the numbers from 1 till 15, and {a..f} will print all alphabets from a till f.As for me more secure way to use find
find dir/* -prune -type d -name "[a-y]" ! -name "z" -exec mv -t dir/z {} + name "[a-y]" has no preceding dash - is that a typo or a syntax I don't know? Note: this is not the cleanest way to do it (see shivams' answer for that), and just works if you only have the directories a-z inside dir.
I've always used the easier to remember
mv * z which, of course, complains that
mv: cannot move ‘z’ to a subdirectory of itself, ‘z/z’ but otherwise does what you want.
This applies (at least) to GNU coreutils
mv [!z] z