Command Line – Move All Files and Directories Except Some in Linux

command linedirectoryfilesmove

I have a folder A which has files and directories, I want to move all those files and directories to another folder B, except file, file2, directory, and directory2.

How can this be done?

Best Answer

With zsh:

setopt extendedglob # best in ~/.zshrc
mv A/^(file|directory)(|2)(D) B/

(the (D) to include dot (hidden) files).

With bash:

shopt -s extglob dotglob failglob
mv A/!(@(file|directory)?(2)) B/

With ksh93

(FIGNORE='@(.|..|@(file|directory)?(2))'; mv A/* B)
Related Question