Categories
guides

TAR Guide

To Compress:

sudo tar -czf '/target/to/filename.tar.gz' 'foldername'

note: tar stores path relative to foldername above, best to run command from parent folder. Consider sudo to ensure permissions to all files intended for compression.

To Compress Excluding Folder:

sudo tar 
--exclude='path/to/folder' -czf '/target/to/filename.tar.gz' 'foldername'

--exclude='path/to/folder' excludes folder, must follow tar command before source/dest or other options, exclude multiple folders by repeating --exclude option with new folder value

To Extract:

sudo tar -xf 'filename.tar.gz' -C '/target/for/extraction'

To Extract Single File:

sudo tar -xf 'filename.tar.gz' -C '/target/for/extraction' '/path/to/file/inTAR.txt'

Note1: Extraction target is the base, directory structure as in tar will still be created at that location. Note2: In order to know the path/to/file/inTAR.txt, you may want to browse first

To Browse:

sudo tar -tvzf 'filename.tar.gz'

NOTE -f switch must immediately preceed filename

Switch Reference:

-c create tar
-x extract from archive
-f file?
-p preserve permissions
-v verbose
-z gzip (omit if tar is not gzipped)
-j bzip (omit if tar is not bzipped)
-t list contents (no extract)
--same-owner preserve ownership as exists in archive (shouldn’t be required if sudo)
-C specify Extract target directory

TAR Created On MacOS?:

MacOS’ tar sometimes include ._ to include metadata about files or other items MacOS natively keeps in FS, but is not necessarily compatible with other FS types. When extracting a MacOS tar‘d archive on MacOS, this is no problem, but when using a different OS’ tar to extract a MacOS tar archive, you end up with these ._ being visible in git repos, file browsers, etc on those other OS/FS types. To avoid, consider trying (unverified) the --disable-copyfile switch (src).

Alternately, if you forgot to do this, and you’re already on the destination filesystem, you can probably just run find . -name "._*" -delete to get rid of them.

By bo.

I'm @boyEatsSteak in a lot of places. I have been wanting 🍕 for weeks. I like gadgets and technology and keeping notes and tips for my future self since all I can remember is that I forget things.