Sample Header Ad - 728x90

Why does tar command create this directory structure when extracting a tar file?

1 vote
1 answer
97 views
I have created this simple directory structure:
$ tree testdir/
testdir/
├── subdir1
│   ├── file1.txt
│   └── file2.txt
├── subdir2
│   ├── file3.txt
│   └── file4.txt
and would like to archive the contents of testdir into a tar file. So I run the following command (note the exclude is because of this answer ):
tar cvf /home/myuser/testdir/testdir_tarfile.tar --exclude=/home/myuser/testdir/testdir_tarfile.tar /home/myuser/testdir
Now I want to extract the files (along with their directory structure) once again. So first create a target directory:
$ mkdir /home/myuser/testdir/target_dir
and now extract the tar file to the target directory:
tar xf /home/myuser/testdir/testdir_tarfile.tar -C /home/myuser/testdir/target_dir/
The resulting file structure looks like this:
$ tree testdir/
testdir/
├── subdir1
│   ├── file1.txt
│   └── file2.txt
├── subdir2
│   ├── file3.txt
│   └── file4.txt
├── target_dir
│   └── home
│       └── myuser
│           └── testdir
│               ├── subdir1
│               │   ├── file1.txt
│               │   └── file2.txt
│               └── subdir2
│                   ├── file3.txt
│                   └── file4.txt
└── testdir_tarfile.tar
Why does the target directory now contain subfolders "home" and "myuser"? I would like it to just contain "subdir1" and "subdir2", i.e. like the original archived directory.
Asked by teeeeee (305 rep)
Oct 7, 2024, 08:56 PM
Last activity: Oct 7, 2024, 09:59 PM