Skip to content
Advertisement

Can tar extraction erase brother directory ?

I made several backups on different directories with Backup Manager. Eg: /home/user1 /home/user2…

It gives me some tar files. The content of a tar file looks like :

home/user1/
home/user1/.profile
home/user1/.bash_history
home/user1/.bash_logout
...

I tried to test the restoration with something like :

tar -xvzf home.user1.tar.gz -C home/user1

But the command above recreate all the structure inside the choosen directory. That gives /home/user1/home/user1/filname1.

So I guess I should use the command specifying the home directory (/home) instead of the user directory. But is there any risk to erase other user’s directories in /home ?

Thks for your time.

Advertisement

Answer

Actually tar does not erase data as a default. But any files that are contained within the tar archive will overwrite files of the same name if they are already present. Likewise a sub-directory’s contents will not be overwritten if the tar archive does not contain files matching them.

mkdir -p foo/bar/

touch foo/file1 foo/bar/file1

tar -cf foo.tar foo/

rm -rf foo

mkdir -p foo/bar/

touch foo/file2 foo/bar/file2

tar -xf foo.tar

ls foo foo/bar/

As once can see both file1 and file2 are present and the newly unarchived directory did not overwrite the old. Here is the output of ls from my system:

foo:
bar  file1  file2

foo/bar/:
file1  file2
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement