5

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?

1

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? – Izkata May 18 '15 at 18:03
8

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}

Brief Explanations:

  • -t option means "target". It is usually used to avoid confusion in cases involving movement of multiple files/directories.
  • Brace expansion work in the way that {1..15} will print all the numbers from 1 till 15, and {a..f} will print all alphabets from a till f.
1

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

  • To avoid complaints: mv [!z] z – don_crissti May 18 '15 at 21:13

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.