Some tar commands

The hard drives have limited space to use. I have many small files which take bigger space. I use tar to compress them to save space. The following are common useful command. I tested in my iMac. It should also work in Linux.

Estimate the tar, tar.gz, tar.bz2 file size in KB.
tar -cf – /directory/to/archive/ | wc -c
tar -czf – /directory/to/archive/ | wc -c
tar -cjf – /directory/to/archive/ | wc -c

List the tar file
tar tvf archive_name.tar
tar tvfz archive_name.tar.gz
tar tvfj archive_name.tar.bz2

Extract all the files from tar
tar xvf archive_name.tar
tar xvfz archive_name.tar.gz
tar xvfj archive_name.tar.bz2

Extract selected files from tar
tar xvf archive_file.tar ‘*.pl’

Add file to tar file #can not add to zipped tar file
tar rvf archive_name.tar newfile

Extract files to specific directory
tar xf archive.tar -C /target/directory

Find specific type and estimate the tar size
find . -name “*out” | xargs tar -cf – | wc -c
find . -name “*out” | xargs tar -czf – | wc -c
find . -name “*out” | xargs tar -cjf – | wc -c

Invert select specific type to tar
find . ! -name “*gz” | xargs tar cvjf test.tar.bz2

Delete the terms after tar.
find . -iname “*.txt” | xargs tar cvf txt.tar
find . -iname “*.txt” -delete

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s