tar and untar - part deux

Now we've got the basics of tar and untar completed, we can move onto some more advanced (and useful) techniques.

So let's see how to add a single file or folder to existing backups. What about deleting individual files without extracting the whole archive?

Let's start with a common requirement by adding a file or folder to an existing tar archive.

Why would we do this? Well, it's a lot easier adding a file to an existing archive than extracting the archive, adding the file and then tarring it again, especially if the archive is large.

Add a file

To add a file to an existing archive:

tar rvf dest.tar myfile.txt

The command uses the 'r' option which is the short form of 'append'. In this case, the file called 'myfile.txt' is added (appended) to the tar archive named 'dest.tar'.

Add a directory

Exactly the same procedure is used to add a directory:

tar rvf dest.tar myfolder/

In that case, the directory name 'myfolder' was added to the archive named 'dest.tar'.

Listing contents

Don't forget that you can check the procedure worked by listing the contents of the archive:

tar tvf dest.tar

Deleting files

In the same way as wanting the ability to add files to our archive, we want to be able to delete files and folders.

As you would imagine, this is pretty simple:

tar --delete -vf dest.tar myfile.txt

Note the syntax of the command. The delete option is followed by the '-vf' options. Where as the 'v', meaning 'verbose' is optional, the 'f' option, meaning that this is not actually a tape drive but a file system, is not.

So that command would delete the file named 'myfile.txt' from the archive named 'dest.tar'.

Deleting folders

To delete a folder from the archive, append the folder name to the command:

tar --delete -vf dest.tar myfolder/

As expected, this removes the directory named 'myfolder' from the archive.

Excluding files

We know that creating an archive is fairly simple. However, the situation often occurs where you have a directory of files you want to archive but there are some files you want to leave out.

This is where the 'exclude' option comes in:

tar cvf dest.tar --exclude='myfile.txt' myfolder/

The format is very similar to creating an full archive. However, this time we excluded the file named 'myfile.txt'.

Exclude list

It can be convenient to create a list of files to exclude from the archive, especially when using tar for regular backups.

To do this, create a file named 'exclude.txt' and enter each filename to be excluded.

The file may look like this:


Now when issuing the archive command you would use the 'X' option:

tar cvf dest.tar -X exclude.txt myfolder/

As you may expect, the archive name 'dest.tar' is created from the contents of 'myfolder' but the list of files in 'exclude.txt' have not been included.


There are, of course, more options available with the tar command and, as you can see, it's very flexible and can be used for more than creating 'simple' backups.

Ask the man about tar. He'll tell you more:

man tar


Article Comments:

Anon commented Sun Apr 05 10:26:32 UTC 2009:

Is there a way to untar a specific file to a specific destination folder? I tried the -C option but the file is untarred to the folder where the tar file is existing? Here is the command I tried: tar -zxvf <tarfilename> <filetobeextracted> -C <destinationfolder>

cc commented Wed Sep 15 12:28:37 UTC 2010:

Is there a way to exclude a specific directory/

Jered commented Wed Sep 15 20:32:54 UTC 2010:

cc: Try adding an "--exclude" option to the tar command, as in, "--exclude=/home/demo/dir/notthisdir".

Want to comment?

(not made public)


(use plain text or Markdown syntax)