Symlinks and permissions in backup archives

I installed Linux on another computer and I want to move my /home directory to that computer. I want to back up that directory with any file permission, symlinks etc.

How should I do it? Are there any parameters for tar gzip?

Answer

If you mean you want to include the files that the symlinks point to, use -h.

tar -chzf foo.tar.gz directory/

Permissions and ownership are preserved by default. If you just want to include the symlinks as symlinks, leave out -h. Small -z is for gzip.

This is all spelled out in man tar; you can search for terms (such as “symlink”) in man via the forward-slash key /.


When you extract the archive (tar -xzf foo.tar.gz), ownership will only be preserved if you unpack as root, otherwise, all the files will be owned by you. This is a feature, since otherwise it would often be impossible for a normal user to access files in an archive they (e.g.) download. Modal permissions (read/write/execute) will remain the same. If as a regular user you want the ownership preserved anyway, you can use the -p switch (so tar -xzpf foo.tar.gz).

However, there is a catch.

File ownership is actually stored as a number, not a name; the system reports names by correlating them with a value from /etc/passwd. You can find the number corresponding to your user name with:

grep yourusername < /etc/passwd

Which will print something like:

piotek:x:1001:1001::/home/piotek:/bin/bash

The first number is your user number, the second one is your group number (they are usually the same). The other fields are explained in man 5 passwd (note the 5).

As a consequence, if you tar up some files and unpack them on another system as root, or using -p (so that ownership is preserved), and there is a user on that system whose user number is 1001, those files will be owned by that user, even if their name is not piotek (and even if there is a piotek user on the system with a different corresponding number).

The tar man page is a little confusing in this regard, since it refers to the -p switch as involving file permissions. This is a common *nix ambiguity in a context where the state of the read/write/execute bits are referred to as mode.

Leave a Reply

Your email address will not be published. Required fields are marked *