Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

2  
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
3  
Use find -type f -exec mv {} folder \; –  ott-- Jan 31 '13 at 19:00

4 Answers 4

up vote 10 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

Actually, there is not problem with that error, the mv command makes its work and you can hide the error redirecting the error output to /dev/null. A simple, but not very elegant workaround is:

mv `ls -A | grep -v my_folder` ./my_folder

share|improve this answer
2  
Downvoted. piping the ls to grep -v my_folder will omit the entire line containing the string "my_folder" not just the string. Instead of piping to grep you should pipe to sed -e 's/my_folder//g' –  h3rrmiller Jan 31 '13 at 19:33
1  
Downvoted. Any version using ls breaks with files containing spaces. –  jordanm Jan 31 '13 at 20:00

you can use :

mv * my_folder 2>/dev/null
share|improve this answer
9  
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. –  Mohsen Pahlevanzadeh Feb 1 '13 at 0:01

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