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?
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)
what i usually do
cd A
ls > a
(assuming you have no 'a' file).
vi a
remove whatever file or directory to be kept in place.
mv $(<a) B
-. You won't be able to spot file names that contain newline characters and it will omit hidden files.
ls is an alias to ls -F (which is common when not using colors), source paths can have leading /, which can have severe side effects - see "Warning" and "Trailing slashes" in info mv (or pinfo mv, but not in man mv)
You can use find with excluded expressions:
find . ! -name . -prune ! -path ./file \
! -path ./file2 \
! -path ./directory \
! -path ./directory2 \
-exec mv {} your_destination \;
This solution is inspired by this question.
-E option. But there might be differences between our systems, since I'm currently on OSX.
If ./A and ./B are on the same filesystem and if these files do not already exist in ./B:
file file2 directory directory2
...then this operation should just be atomic:
cd ./A; mv * ../B
for mv in file file2 directory directory2
do mv ../B/"$mv" .
done
If they are not, then there are at least 8 additional cross-device copies done with the above set of commands.
mv unlinks and renames when not crossing devs - it just affects dentries, basically, as i understand it. But the shell doesnt get into that.
$ sudo mv (source folder name) A B(destination foldername)
ex: create empty dir some sub folder a 1)sudo mkdir -p A/{b/{a,b,c},c,d}
2)ls A/
b c d
3) sudo mkdir B
5)sudo mv a(folder) b(folder)
You could use a bash array to store which ones you want to skip and then use the containsElement function demonstrated here:
$ skiplist=(file file2 directory directory2)
$ for f in *; do if containsElement "$f" "${skiplist[@]}"; then continue; fi;
mv -v "$f" B; done