Search code examples
bashpermissionstarsubdirectoryarchive

Tar cannot expand subfolders from .tar archive


I'm trying to expand tarball native archive (not a Gzipped version) from bash script using separate commands for a reason.

Tar archive format:

File1
File2
folder1/file3
folder1/file4

Then i execute next commands:

tar xvf archive.tar -C $TEMP_DIR/ File1
tar xvf archive.tar -C $TEMP_DIR/ File2

Both succesful

Next I execute separate commands for file 3 and 4:

tar xvf archive.tar -C $TEMP_DIR/ folder1/file3
tar xvf archive.tar -C $TEMP_DIR/ folder1/file4

Both fails for:

Tar: folder: Permission denied
Tar: file: No such file or directory

Please advise!


Solution

  • You need to make sure archive.tar is in the correct place, Following script reproduce the whole process :

    #!/usr/bin/env bash
    mkdir -p /tmp/{dest,src/folder1}
    cd /tmp/src
    touch File{1,2} folder1/file{3,4}
    
    tar cvf ../archive.tar *
    
    TEMP_DIR=/tmp/dest
    
    cd /tmp
    tar xvf archive.tar -C $TEMP_DIR/ File{1,2}
    ls  -l $TEMP_DIR
    read -pEnter
    tar xvf archive.tar -C $TEMP_DIR/ folder1/file{3,4}
    ls  -l $TEMP_DIR/folder1