$_ CLI In Depth
Learning progress 0%
lesson disks mounts archives

Copying Trees: cp -a & Preserving Metadata

5 min read Module 12: Disks, Mounts & Archives Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

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.

BASH5 lines
mkdir -p src
echo one > src/a
chmod 640 src/a
ln -s a src/link
cp -a src copy

The 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.

BASH2 lines
stat -c "%a %n" copy/a
readlink copy/link

Verifying 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.

BASH1 lines
diff -r src copy && echo tree-matches

Use 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.

Note

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.

BASH2 lines
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.

BASH3 lines
umask
touch fresh.txt
stat -c "%a %n" fresh.txt

Hands-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.

BASH5 lines
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/shortcut

The 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.

Terminal lab · 2 steps · real Linux in your browser

Copy a tree and keep its links

Build a tree with a link and a restricted file, copy it with cp -a, and check the copy.

01
Copy the tree with cp -a
Create src with a file a that contains one, give it mode 640, and add a symbolic link named link that points to a. Copy the directory to copy with cp -a. ``bash mkdir -p src echo one > src/a chmod 640 src/a ln -s a src/link cp -a src copy ``
Show hint for step 1
Use ln -s TARGET LINK. cp -a copies the mode and keeps link as a link.
02
Mirror the contents into a directory
Create an empty directory named mirror. Copy the contents of src into it with the src/. form, so no src directory appears inside mirror. Check the result with diff and readlink. ``bash mkdir -p mirror cp -a src/. mirror/ diff -r src mirror && readlink mirror/link ``
Show hint for step 2
The trailing /. after src copies its contents. Without it you get mirror/src.
Check yourself Question 1 of 4
Concept Check What cp -a expands to recall

Which set of options does cp -a stand for?

Recap & Key Takeaways

A file carries mode, owner, timestamps, and links as well as data, and a plain cp may lose some of them.
cp -a is shorthand for -dR --preserve=all, so it copies recursively, keeps symbolic links as links, and preserves hard links and metadata.
Use the trailing form src/. to copy the contents of a directory into an existing destination without an extra level of nesting.
Verify a copy with diff -r for content, and with stat or readlink for modes and links.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue