Last Updated: February 25, 2016
·
1.34K
· jwebcat

move all files in current directory to upper directory including dotfiles, files starting with .. and dash

There are many ways to chop a tree down as we all know.

  • Here are a couple ways to do it.
  1. With mv
$ mv * .[^.]* ..

Important note: this will not match files starting with ..

  • If you want to match files starting with .. and -- use the below mv command instead.
$ mv -- * .[^.] .??* .. 
  • which will match everything except . and .. . . * will match everything that doesn't start with a . and .[^.] will match all 2 character filenames starting with a dot except .. ,and .??* will match all filenames starting with a dot with at least 3 characters. As well as -- to match files starting with a dash.
  1. With shopt
$ (shopt -s dotglob; mv -- * ..)

this has got you covered for files starting with ..

  • The above is wrapped in parens so that it executes in a subshell, and so files beginning with -- are not interpreted as arguments to mv.
  1. Another alternative with find
$ find . -mindepth 1 -maxdepth 1 -exec mv -t.. -- {} +
  1. If you have rsync on your machine you can use this. (Windows doesn't really have a good port of rsync that I am aware of. If anyone knows of one for Git Bash, I would love to know.)
$ rsync -a --remove-source-files . ..
  • With the above command, we are telling rsync to copy the content of . into ..

  • The switch -a enables recursion into . ,subdirectories and enables some other common options.

  • The switch --remove-source-files tells rsync to remove the source files after a successful copy, i.e. it makes rsync behave similarly to the mv command.

  • Enjoy.