Copy (cp) shell command examples.

# To copy a file to another location:
cp <path/to/source_file.ext> <path/to/target_file.ext>

# To copy a file into another directory, keeping the filename:
cp <path/to/source_file.ext> <path/to/target_parent_directory>

# To recursively copy a directory's contents to another location (if the destination exists, the directory is copied inside it):
cp -R <path/to/source_directory> <path/to/target_directory>

# To copy a directory recursively, in verbose mode (shows files as they are copied):
cp -vR <path/to/source_directory> <path/to/target_directory>

# To copy text files to another location, in interactive mode (prompts user before overwriting):
cp -i <*.txt> <path/to/target_directory>

# To dereference symbolic links before copying:
cp -L <link> <path/to/target_directory>

# ---

# Create a copy of a file:
cp ~/Desktop/foo.txt ~/Downloads/foo.txt

# Create a copy of a directory:
cp -r ~/Desktop/cruise_pics/ ~/Pictures/

# Create a copy but ask to overwrite if the destination file already exists
cp -i ~/Desktop/foo.txt ~/Documents/foo.txt


# To copy file1 to file2:
cp file1 file2

# To copy file1 into directory:
cp file1 directory

# To copy files into directory:
cp file1 file2 file3 ... directory

# To copy dir1 into dir2 including subdirectries:
cp -R dir1 dir2/

# To copy directory, preserving permissions:
cp -pR dir1 dir2/

##
# Copy a directory with all files and subdirectories
##
src=/home/$USER         # source
dst=/tmp/backup         # destination
cd "$src"
find . -print | cpio -pdmv "$dst"

# This method can be used to receate a directory hierarchy, without files, too:
find . -type d -print | cpio -pdmv "$dst"

# Copying across a network, this is an indispensable technique:
cd /the/source
tar cf - dir1 dir2 | ssh user@host "cd /the/dest && tar xvf -"

# Copy file and overwrite w/out confirmation (https://stackoverflow.com/questions/8488253/how-to-force-cp-to-overwrite-without-confirmation):
yes | cp -rf /usr/local/www/jonlabelle.com/offline.html /usr/local/www/jonlabelle.com/public