I have a directory with symbolic links to other directories. I want to archive the symbolic links, not as links but as archives containing the files of directories they refer to, using tar command. How can I do this?
Asked
Active
Viewed 9.6k times
2 Answers
123
Use the -h tar option. From the man page:
-h, --dereference
don't archive symlinks; archive the files they point to
Mat
- 51,578
- 10
- 158
- 140
-
4If I have /sym1 points to /backups and /sym2 points to /backups, then I run `tar -hcf file.tar /sym1 /sym2` will I get /backups twice? – Felipe Alvarez Jan 28 '15 at 23:55
-
@FelipeAlvarez `-L` counts files once, `-l` multiple times. See [here](https://unix.stackexchange.com/a/82477/105615). – Suuuehgi Apr 11 '18 at 22:02
-
1the order of options matters. ie `tar -czfvh` will fail `tar -hczfv` will work – til Jan 26 '23 at 12:49
3
If the links are all in the root directory you can have the shell dereference them and pass to tar as arguments. For example if you have /backup/source/a and /backup/source/b, both of which are symlinks pointing to the real directory, something like the following would work
tar -cf /path/to/backup.tar /backup/source/*/
phemmer
- 70,657
- 19
- 188
- 223
-
Doesn't work here. Seems like `tar -cf /path/to/backup.tar /backup/source/a/` alone doesn't even work when `a` is a symlink. – Zero3 Jan 03 '16 at 19:52