Search code examples
bashextracttararchivegunzip

Extract file from TARBall archived from console


Next problem is taking place for me:

There is TAR archive i had to compile with file-roller for a while as script routine using file-roller command. Now I had to change my script to be more "technical" and cross-platform referring directly to TAR command: For this i just cd to specific directory and using this command:

tar -cvzf "path/to/archive.tar.gz" "." > /dev/null 2>&1

The problem that "." directory is transformed into path inside the archive and files are stored under subdirectory "." in next manner:

archive.tar.gz
-> ./
   -> file1.ext
   -> file2.ext

I've managed to use --transform option to get the files back into root folder of archive this way:

tar -cvzf "path/to/archive.tar.gz" --transform "s,.,," "." > /dev/null 2>&1

The result is right organized archive with files moved to archive root folder:

archive.tar.gz
-> file1.ext
-> file2.ext

The outcoming archive readable by file-roller, while has broken functionality on file extraction by tar:

tar -x -f "path/to/archive.tar" -C "$TEMP_DIR/" file1.ext

TAR has immediately to report: No such file or directory in archive or similar.

tar -x -f "path/to/archive.tar" -C "$TEMP_DIR/"

Still works normally and extracts all files from archive into "$TEMP_DIR/".


Solution

  • You need to get rid of the first dot and slash :

    tar -cvzf "path/to/archive.tar.gz" --transform "s,./,," "." > /dev/null 2>&1
    

    Or you can simply do (if you don't have hidden files)

    tar -cvzf "path/to/archive.tar.gz" * > /dev/null 2>&1
    

    For extraction, you can run directly on tar.gz :

    tar zxf "path/to/archive.tar.gz" -C "$TEMP_DIR/" tarred-file.ext