How to move all files in current folder to subfolder?

The command

mv !(new) new

should do the trick. If it doesn't work, run shopt -s extglob first.

To also move hidden files/directories (that beginning with a dot), run also shopt -s dotglob first.
So, to sum up:

shopt -s extglob dotglob
mv !(new) new
shopt -u dotglob

(it is always better to unset dotglob to avoid bad surprises).


I found something like this but is a bit simpler to understand, and it might work well for you too:

ls | grep -v new | xargs mv -t new

Adding an explanation to the above solution:

From man pages:

  • mv -t

    -t, --target-directory=DIRECTORY
          move all SOURCE arguments into DIRECTORY
    
  • grep -v

    -v, --invert-match
          Invert the sense of matching, to select non-matching lines.
    

Explained by step:

  • ls will list the files in current directory
  • grep -v new will return piped to that is not match new
  • xargs mv -t new will move the files piped to it from grep -v to the target directory

Simple idea. Assuming you are in /myuser, rename downloads to new, create a new downloads directory then move new into it.

mv downloads new # downloads is now called new
mkdir downloads # create new directory downloads
mv new downloads # move new into it.

Tags:

Files