Hi! In tar info page the following is written:
" When reading from an archive, the `--dereference' (`-h') option causes `tar' to follow an already-existing symbolic link when `tar' writes or reads a file named in the archive. " I wrote a simple script and it seems that symlinks to files are not followed (whereas symlinks to directories are followed). In other words, if I extract a file using -h and a symlink with the same name exists then the symlinks is just removed and a new file is created. But if I extract a directory using -h and a symlink with the same name exists, the symlinks is followed. So, the behaviour is different for files and directories. I'm writing some script and I wouldn't like the following versions of tar to break it, so I have questions: 1. Is this behaviour a bug, or does it works as expected? 2. Is there a possibility this behaviour will be changed in future? The following output demonstrates the issue. In the output, the header "file (-h)" (and similar) means we're checking how a _file_ overwrites a symlink to a _file_ if being extracted with _-h_ option. Two groups of lines for each header are filesystem status before and after extracting so we can see the change. Tar 1.26 is used. /tmp$ ./tar.sh file () drwxr-xr-x 3 pkg users 4096 Nov 1 14:54 dst drwxr-xr-x 2 pkg users 4096 Nov 1 14:54 dst/dir -rw-r--r-- 1 pkg users 0 Nov 1 14:54 dst/dir/regfile lrwxrwxrwx 1 pkg users 7 Nov 1 14:54 dst/dir/symfile -> regfile drwxr-xr-x 3 pkg users 4096 Nov 1 14:54 dst drwxr-xr-x 2 pkg users 4096 Nov 1 14:54 dst/dir -rw-r--r-- 1 pkg users 0 Nov 1 14:54 dst/dir/regfile -rw-r--r-- 1 pkg users 5 Nov 1 14:54 dst/dir/symfile dir () drwxr-xr-x 2 pkg users 4096 Nov 1 14:54 dst lrwxrwxrwx 1 pkg users 1 Nov 1 14:54 dst/dir -> . -rw-r--r-- 1 pkg users 0 Nov 1 14:54 dst/symfile drwxr-xr-x 3 pkg users 4096 Nov 1 14:54 dst drwxr-xr-x 2 pkg users 4096 Nov 1 14:54 dst/dir -rw-r--r-- 1 pkg users 5 Nov 1 14:54 dst/dir/symfile -rw-r--r-- 1 pkg users 0 Nov 1 14:54 dst/symfile file (-h) drwxr-xr-x 3 pkg users 4096 Nov 1 14:54 dst drwxr-xr-x 2 pkg users 4096 Nov 1 14:54 dst/dir -rw-r--r-- 1 pkg users 0 Nov 1 14:54 dst/dir/regfile lrwxrwxrwx 1 pkg users 7 Nov 1 14:54 dst/dir/symfile -> regfile drwxr-xr-x 3 pkg users 4096 Nov 1 14:54 dst drwxr-xr-x 2 pkg users 4096 Nov 1 14:54 dst/dir -rw-r--r-- 1 pkg users 0 Nov 1 14:54 dst/dir/regfile -rw-r--r-- 1 pkg users 5 Nov 1 14:54 dst/dir/symfile dir (-h) drwxr-xr-x 2 pkg users 4096 Nov 1 14:54 dst lrwxrwxrwx 1 pkg users 1 Nov 1 14:54 dst/dir -> . -rw-r--r-- 1 pkg users 0 Nov 1 14:54 dst/symfile drwxr-xr-x 2 pkg users 4096 Nov 1 14:54 dst lrwxrwxrwx 1 pkg users 1 Nov 1 14:54 dst/dir -> . -rw-r--r-- 1 pkg users 5 Nov 1 14:54 dst/symfile The script in question follows: /tmp$ cat tar.sh #!/bin/sh for opt in '' '-h'; do echo echo "file ($opt)" echo rm -rf src dst mkdir -p src/dir echo file >src/dir/symfile mkdir -p dst/dir touch dst/dir/regfile ln -s regfile dst/dir/symfile find dst |xargs ls -ld ( cd src; tar -cf - . ) |tar -x $opt -f - -C dst echo find dst |xargs ls -ld echo echo "dir ($opt)" echo rm -rf src dst mkdir -p src/dir echo file >src/dir/symfile mkdir -p dst touch dst/symfile ln -s . dst/dir find dst |xargs ls -ld ( cd src; tar -cf - . ) |tar -x $opt -f - -C dst echo find dst |xargs ls -ld done -- pv4