bash commands: tar

tar for converting a group of files into one archive, or recovering files from an archive

common options

tar command cannot be used alone. It must be used with one of -c, -r, -t, -u, -x.

create a tar file

tar -cf eulerProjects.tar /Users/shao/Info/github/eulerProjects
tar -czf eulerProjects.tar.gz /Users/shao/Info/github/eulerProjects

options used here: c:create, z: zip. f:file

-z and -j have to be used to compress files into gzip, and bzip2 formats! When recovering from an archive, such options are not necessary.

tar format

  1. uncompressed


  2. compressed:

    tar.gz(.tgz), .tar.Z(.taz, .tz), ...

output to stdout

tar -c /Users/shao/Info/github/eulerProjects;  

This will output to stdout.


tar -cvf ...; 

v for verbose, to see the details of file progress

The bz2 format file extension is tar.bz2(or .tbz, .tb2). The format compresses into a smaller size than gz, but takes more time. You need j to use bz2 compression.

tar -cjf eulerProjects.tar.bz2 /Users/shao/Info/github/eulerProjects

extract from a tar

option: -x

tar -xf eulerProjects.tar.gz

or to extract to another location, use -C, or --directory. C means "change directory".

tar -xf eulerProjects.tar.gz -C /Users/shao

Note you don't have to use specific options for gz,gz2... this time.

extract some files

tar -xf eulerProjects.tar.gz file_name_including_path  file2_name_including_path ...

extract all .py files:

tar -xf eulerProjects.tar.gz --wildcards '*.py'; 

--wildcards is not suppored in mac.

list the contents of a tar

option: -t

tar -tf eulerProjects.tar.bz2

using together with -v will also gives you info on the permissions, creation date of the files.

update a tar

-r, -u will be used for updating. -r and -u are like -c, but only work for uncompressed format: -r to append new entries to the archive, -u to append new entries only if their modification date is newer.

append a file, or a folder:

tar -rvf eulerProjects.tar.gz
tar -rvf eulerProjects.tar.gz folder_name


option: -W only works for uncompressed file. not suppored in mac.

tar -tfW eulerProjects.tar