Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Anytime I want to move thousands of files to a new folder, I always encounter the same problem.

> mkdir my_folder
> mv * my_folder
mv: cannot move 'my_folder to a subdirectory of itself 'my_folder'

While I think that the error above is harmless (is it?) I am wondering if there is a way of avoiding it.

In case it matters, I am interested in a solution in zsh or one that works well across various shells.

share|improve this question

marked as duplicate by Gilles, uther, jasonwryan, Renan, Michael Mrozek Feb 6 '13 at 21:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

There was a similar question on the Ubuntu SE (although for bash). Perhaps this works for zsh too? askubuntu.com/questions/91740/… – howardh Jan 31 '13 at 18:47
Use find -type f -exec mv {} folder \; – ott-- Jan 31 '13 at 19:00
up vote 11 down vote accepted

In zsh, with the extended_glob option enabled, you can use ~ to exclude patterns from globs, so you could use:

setopt extended_glob
mv -- *~my_folder my_folder

Or use the negation operator (still with extended_glob):

mv -- ^my_folder my_folder

Use braces to avoid typing the directory name twice:

mv -- {^,}my_folder

In bash (for other answer-seekers using it), you can use Ksh-style extended globs:

# If it's not already enabled
shopt -s extglob
mv -- !(my_folder) my_folder

You can also use that syntax in zsh if you enable the ksh_glob option.

share|improve this answer

If there are only regular files apart from the new subdirectory, you could use find:

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

The -type f option will only find files (not dirs). -maxdepth 1 will keep find looking only in . (not recurs down into other dirs). The -exec … does your move.

In zsh, you can abbreviate this to

mv *(.) my_folder

Yes, the error is harmless.

share|improve this answer
Is there anyway you could mention how (.) works in zsh? – Emanuel Berg Jan 31 '13 at 23:58
@EmanuelBerg Those are glob qualifiers. *(.) represents ALL plain files. see the 'Glob Qualifiers' section in the zshexpn man page. – h3rrmiller Feb 4 '13 at 18:37

you can use :

mv * my_folder 2>/dev/null
share|improve this answer
This doesn't actually prevent the error from occuring. It simply hides it by piping it to /dev/null instead of to your terminal window. – howardh Jan 31 '13 at 18:49
@howardh: Although I would not use the above solution in this particular case, in general, if you deem an error harmless, that's a good way disposing of the error message. – Emanuel Berg Jan 31 '13 at 23:55
Poster just wants to hide error, So it's good solution. – PersianGulf Feb 1 '13 at 0:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.