GNU/Linux

Linux tar Notes

2024/04/26

Tape ARchive (tar) is a tool for archiving files. It’s one of those utilities I use all of the time on Unix-like systems. Archiving is the process of combining multiple files into a single large file. As a system-administrator and a software developer, I make tarballs of projects, installations, uploaded file folders, etc several times a week.

Basic Usage

Options

Option Description
c Create an new archive
x Extract files from an archive
t List the contents an archive
r Append files to the end of an archive
f Set archive file
z gzip the archive
j bzip2 the archive
C change the directory before performing operations

Archive Creation and Extraction examples

Create archive file with the c option. the f option specifies the archive file

tar -c -f  uploads.tar /enc/uploads

tar cf  uploads.tar /enc/uploads

Adding v gives verbose output. This lets you see the paths as they get added to the archive.

tar cvf  uploads.tar /enc/uploads

Extract all contents from the archive file with the x option

tar xvf uploads.tar

You can list the contents of a archive file (without extracting) with the t option

tar tvf uploads.tar.bz2

You can specify which files to extract by adding their name to the file

tar xvf uploads.tar   some_file.txt  some_other_file.txt  *.jpg

You can compress the archive in gzip format with the z option

tar cvzf  uploads.tar.gz /enc/uploads

bzip2 can be used instead of gzip with with the j option

tar cvjf  uploads.tar.bz2 /enc/uploads

Modify Existing Archive Files

Use the r option to append new files to the archive. This will add the specified files the archive.

tar rvf  uploads.tar  new_file.txt

Use the r option to update existing files. This replaces the file that matches the one specified.

tar uvf  uploads.tar  update_file.txt

find

The output from find can be pipped into tar.

You can find all c files in a directory and combine them in an archive

find . -name "*.c" | tar cvf cfiles.tar -T -

Or if you want to archive all files found that are newer than 2 days

find . -mtime -2 -type f | tar cvf cfiles.tar -T -

Note you have to use -type f in the find command to prevent directories that may have been modified recently. If you don’t, the archive will include all of the contents of the recently modified directories.

-maxdepth can be used to limit the how far find goes when looking for files.

find aruco/ -maxdepth 2 -name "*cpp" -o -name CMake* | tar cvf aruco.tar -T -

git

Like find the output from git can be used by tar.

You can find all c files in a directory and combine them in an archive

git diff -w --name-only 2>/dev/null | tar cvf git_diff.tar -T -


tar cvf git_diff.tar `git diff -w --name-only`

Wildcards

The wildcards option lets you pattern match the files you wish to list or extract from an archive.

tar tvf  MegaDriving.tar --wildcards 'MegaDriving/*/src/*c'

Alternate Destinations

Directory

You can tar files to another directory instead of an archive file. Setting the file as - lets you pipe the archive as its created to another tar command. The second tar command extracts from - into the folder selected by cd.

  tar cBf - srcFileOrDir | (cd /dest/dir/ ; tar xvpBf -)

Networked

You can also use OpenSSH to tar between different machines. You pipe the tar output to the machine over SSH. This example copies the files and directories to a destination directory on a remote host. The second tar command extracts the files to the destination directory.

  tar cBf - srcFileOrDir | ssh -C user@example.com   "cd /dest/dir/ ; tar xvBf - "

You can also create an archive file on the remote host instead of extracting from the stream. This example uses dd to create an archive file on a remote host.

tar cBf - DIR1 DIR2  | ssh   user@dest.example.com   dd of=/databak/DIR1_DIR2.tar

About Me

Greg Gallardo

I'm a software developer and sys-admin in Iowa. I use C++, C#, Java, Swift, Python, JavaScript and TypeScript in various projects. I also maintain Windows and Linux systems on-premise and in the cloud ( Linode, AWS, and Azure )

Github

Mastodon

YouTube

About you

IP Address: 13.58.76.6

User Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

Language:

Latest Posts

Iowa City Weather

Today

-- ˚F / 61 ˚F

Sunday

71 ˚F / 54 ˚F

Monday

64 ˚F / 46 ˚F

Tuesday

76 ˚F / 54 ˚F

Wednesday

76 ˚F / 56 ˚F

Thursday

72 ˚F / 51 ˚F

Friday

67 ˚F / 47 ˚F