What a file carries besides its bytes
A file has more than data. It has a mode, which holds the read, write, and execute bits and special flags, an owner and group, timestamps for access and modification, and sometimes extended attributes. A symbolic link is a file whose data is a path, and a hard link is a second name for the same inode. A copy that loses any of these may still look right at a glance and behave differently later.
By default, cp copies the contents of a file into a new file and sets the new file mode from the source mode masked by your umask. It does not copy the owner, and it does not copy the timestamps. For a single file that is often what you want. For a tree, the defaults can change the meaning of the data.
What -a means
The -a option is shorthand for -dR --preserve=all. The -R flag copies directories recursively. The -d flag copies symbolic links as links instead of following them, and it also preserves hard links among the copied files. The --preserve=all flag keeps mode, ownership, timestamps, and as much metadata as the system allows.
The option matters most for symbolic links. Without -d, copying a link can copy the file it points to, which may be somewhere else entirely or may not exist in the new tree. With -a, the new tree contains a link with the same target, and that is a faithful copy.
Building a tree with a link
To see the difference, build a small tree that includes a symbolic link, then copy it with cp -a. The tree has one file with a restrictive mode and one link to that file.
mkdir -p src
echo one > src/a
chmod 640 src/a
ln -s a src/link
cp -a src copyThe copy is complete. The mode of copy/a is still 640, and copy/link is still a symbolic link that points to a. You can check both facts directly with stat and readlink. The first prints the mode in octal, and the second prints the target of the link without following it.
stat -c "%a %n" copy/a
readlink copy/linkVerifying a copy
A copy is not finished until you compare it. diff -r compares the trees by name and by content. It reports a difference when a file is missing or its bytes change, but it does not compare modes or ownership. For those, use stat on each side, or ask ls -l for a long listing.
diff -r src copy && echo tree-matchesUse the content comparison together with the metadata check. A tree that matches in content but has lost its modes is still a broken copy for programs that depend on execute bits or restricted access.
Ownership is preserved only when the copying user is allowed to set it. As an ordinary user, cp -a keeps your own ownership and cannot give files to another owner. Root can preserve owners, which is one reason backups are usually taken by a privileged account.
Copying into a different place
If the destination already exists, cp -a src dest creates dest/src when dest is a directory. Be deliberate about the trailing slash. A slash after the source name copies its contents instead of the directory itself. Both forms are useful, but mixing them up is the most common cause of a copy with an extra level of nesting.
mkdir -p mirror
cp -a src/. mirror/The src/. form copies the contents of src into mirror directly. The result is the same as copying the tree, without the extra directory level.
When not to preserve everything
Preserving everything is right for a backup, but it is not always right for a copy meant to become a new file. Copying a downloaded program into a shared directory with its original owner may be wrong. Copying files out of another system may carry over settings that do not belong on the target. Read what a copy preserves before you choose the flag, and keep only the attributes the job needs.
The reverse problem is just as real. A plain cp of a tree can produce links whose targets were not copied, and it applies the umask of the copying shell to every file. These differences are silent. The tree looks fine until a script runs and fails on a missing link or on a file that is no longer executable.
Permissions and the umask
The umask is a mask that removes bits from the mode of every new file. With the common umask of 022, a file created with a requested mode of 666 receives 644, and a directory created with 777 receives 755. A plain copy applies the same mask to the copy. Preserving the mode keeps the source mode, including bits the mask would remove, which is what -a does for you.
You can see the effect of your own umask with one command. The mode printed is a three digit octal value, and it shows what a new regular file receives when you do not name a mode.
umask
touch fresh.txt
stat -c "%a %n" fresh.txtHands-on: a faithful mirror
Make a tree with a link and a restrictive file, mirror it with the contents form of cp -a, and prove the copy is faithful with a content comparison and a link check. The test of a copy is whether the copy behaves the same as the source.
mkdir -p tree/inner
echo data > tree/inner/file
ln -s inner/file tree/shortcut
cp -a tree/. mirror2/
diff -r tree mirror2 && readlink mirror2/shortcutThe final command prints inner/file, which is the target of the link in the copy. A content match together with a link that still points to the same relative path is the result you want from cp -a.