more tar and untar

As already hinted at in the tar and untar article, there is much more to archiving your files and folders than compressing and uncompressing.

We know how to extract a single file from the archive, but what about adding a single file or folder? What about deleting files without extracting the whole archive? This article concentrates on some advanced tar and untar techniques.

Adding files and folders

We'll start with a common requirement which is 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.

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'.

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'.

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

tar tvf dest.tar

Deleting files and folders

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 optional.

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

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 from an archive

As outlined in the tar and untar article, creating an tar archive is fairly simple. However, the situation often occurs where you have a directory of files you want to archive but there are some 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 the original command to create an archive. However, this time we excluded the file called 'myfile.txt' which is located in the 'myfolder' directory.

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 you want to exclude.

The file may look like this:

myfile.txt
myfile2.txt
.config

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 more options available when using the tar command and, as you can see, it is very flexible and used for more than 'just' creating simple archives.

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

man tar

PickledOnion.

Digg it | del.icio.us | reddit | StumbleUpon

Article Comments:

Jonathan Andrew Wolter 20 Sep, 2007

This is another helpful article. Thanks. Keep up the great work, I look forward to your contributions to slicehost!

-JAW

p.s. an article about rsync might fit nicely, to another option of backing up.

Comments are closed for this article.