I have a directory that I am trying to clean out that contains both files and subdirectories. What I am trying to do is simple: move all the files into another directory, but leave all the sub-directories as they are.

I am thinking something like:

mv [*_but_no_dirs] ./other_directory

It seems like there should be a simple way to do this with wildcards * and regex...

Anyone have ideas?

  • 1
    So move all the files in a subfolder, but ignore any in the subdirectories? – Wilf Jul 29 '14 at 18:37
  • @Wilf: Exactly. – Questionmark Jul 29 '14 at 18:38
  • 2
    In zsh, you could do mv **/*(.) ./other_directory - with bash you'd need to resort to external commands like find, though. – godlygeek Jul 29 '14 at 18:47

Regex aren't involved here. Wildcards in bash (like most other shells) only match files based on the file names, not based on the file type or other characteristics. There is one way to match by type: adding / at the end of the pattern makes it only match directories or symbolic links to directories. This way, you can move directories, then move what's left, and move directories back — cumbersome but it works.

tmp=$(TMPDIR=.. mktemp -d)
mv -- */ "$tmp"
mv -- * "$tmp"/other_directory/
mv "$tmp"/* .
rmdir "$tmp"

A standard way to match files by type is to call find.

find . -name . -o -type d -prune -o -exec sh -c 'mv "$@" "$0"' other_directory/ {} +

In zsh, you can use glob qualifiers to match files by type. The . qualifier matches regular files; use ^/ to match all non-directories, or -^/ to also include symbolic links to directories.

mv -- *(.) other_directory/

In any shell, you can simply loop.

for x in *; do
   if ! [ -d "$x" ]; then
     mv -- "$x" other_directory/

You could use something like

find . -maxdepth 1 \( ! -type d \) -exec sh -c 'mv  "$@" MYDIR' _ {} \;

First we use find to look only within the current directoy, then we ignore directories by using ! -type d finally we execute sh and move everything to the destination dir. You might try {} + at the end which will be faster.

  • 2
    A little bit of explanation, please... – Questionmark Jul 29 '14 at 18:47
  • 1
    @Questionmark I've updated the answer and did add some explanation. Also, it's worth visiting mywiki.wooledge.org/UsingFind – Valentin Bajrami Jul 29 '14 at 18:50
  • Why do you suggest -exec sh -c 'mv "$@" MYDIR' _ {} \; rather than -exec mv {} MYDIR \;? Just to get something that can be streamlined by changing the \; to a +? You can do that with the -exec mv -t MYDIR {} + form. – Scott Jul 29 '14 at 19:20
  • @Scott I had no idea about the -t flag. I also explained in the answer that the + is faster but not all find versions support it. So the above code is pretty much compatible with any find or shell that is used. – Valentin Bajrami Jul 29 '14 at 19:58
  • 1
    @val0x00ff: “…the above code is pretty much compatible with any find or shell that is used.” True, but so is find … -exec mv {} MYDIR \;, and that uses less resources than … -exec sh -c 'mv "$@" MYDIR' _ {} \;. – Scott Jul 29 '14 at 20:14

It's potentially a bit dangerous but

cp * destination/
rm *

As both cp and rm won't operate on directories without the -r switch.


I would suggest simply doing mv *.* destination/ with destination/ being the folder you are moving to.

  • 2
    check out: unix.stackexchange.com/help/formatting – Jeff Schaller Dec 3 '16 at 1:41
  • 2
    Directory names can contain 'dot' and filenames can exclude it (and often do) so *.* will move some directories and leave some files, which is not what the asker wanted. – dave_thompson_085 Dec 3 '16 at 5:18
  • Sorry but this is completely wrong. It will simply move anything with a dot in its name and not move anything without one. Dots are in no way exclusive to files and they're not required by files either, so this will just move a random selection of files and directories. – terdon Apr 14 '17 at 16:25
mv $(ls -l | grep "^-" | awk '{print $9}') ./folder_where_move

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.