How can I use the Linux terminal to copy everything in current directory to a subdirectory?
If you want to copy the contents of the folder recursively (will throw 1 error, alternatives below):
cp -r * sub/
A little more hacky, but works on non-empty subdirectories:
TARGETDIR='targetdir here';cp -r `find . -maxdepth 1 ! -name "$TARGETDIR"` "$TARGETDIR"
TARGETDIR='targetdir here';for file in *;do test "$file" != "$TARGETDIR" && cp "$file" "$TARGETDIR/";done
TARGETDIR='z';for file in *;do test "$file" != "$TARGETDIR" && cp -r "$file" "$TARGETDIR/";done
target is the name of the target subdirectory, if your shell is bash:
shopt -s extglob cp -r !(target) target/
In ksh, you can directly do
cp -r !(target) target/.
In zsh, you can do
setopt ksh_glob then
cp -r !(target) target/. Another possibility is
setopt extended_glob then
cp -r ^target target/.
I would suggest moving the target directory outside the source directory and then put it back again; mv is free (if you are careful not to move to a different filesystem), unless you are expecting other processes to interfere/be interfered.
Most solutions posted above won't work if there are spaces in filenames. I would suggest using variants of find -print0 | xargs -0, or find -exec, etc.