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?
2 Answers
Use the -h tar option. From the man page:
-h, --dereference don't archive symlinks; archive the files they point to - 4If I have /sym1 points to /backups and /sym2 points to /backups, then I run
tar -hcf file.tar /sym1 /sym2will I get /backups twice?Felipe Alvarez– Felipe Alvarez2015-01-28 23:55:31 +00:00Commented Jan 28, 2015 at 23:55 -
- 2the order of options matters. ie
tar -czfvhwill failtar -hczfvwill worktil– til2023-01-26 12:49:26 +00:00Commented Jan 26, 2023 at 12:49
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/*/ - Doesn't work here. Seems like
tar -cf /path/to/backup.tar /backup/source/a/alone doesn't even work whenais a symlink.Zero3– Zero32016-01-03 19:52:53 +00:00Commented Jan 3, 2016 at 19:52