6

I installed linux on another computer and I want to move my /home directory to that computer. I want to backup that directory with any file permission, symlinks etc. how should I do it? Are there any parameters for tar gzip?

  • Rsync is maybe better than tar... – Dom Mar 15 '14 at 15:55
  • why? what's the difference? what is the command? – piotrek Mar 15 '14 at 16:49
  • Sorry I read to quickly your question. I you want a backup, you are right, tar is better. If you want to deploy a new PC, a rsync copy from one host to another (with just -a option) is better than doing a tar and explode it on the other host – Dom Mar 17 '14 at 8:05
10

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.

  • and how can i properly extract it? preserving symlinks and permissions? ideally not removing other files as i'm migrating from lxde to xfce so i don't want to delete xfce files – piotrek Mar 18 '14 at 18:15
  • I've added a few paragraphs about that. – goldilocks Mar 18 '14 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.