tar command in Linux

In order to archive files and directories into a single file, you use the ‘tar’ command in Linux. Here are a couple of uses of the tar command :

Using the tar command on the directory ./mydata/.

Suppose you have a directory ./mydata in your home folder.

To tar everything in “mydata” directory, to create a “.tar” file, execute the following command :

$ tar -cvf mydata.tar mydata

Which will create an archive called “mydata.tar“.

Using the tar command to create a “.tar.gz” of ./mydata

$ tar -czf mydata.tar.gz mydata

List the files in the archive

$ tar -tzf mydata.tar.gz

or

$ tar -tf mydata.tar

A way to list specific files

Note, pipe the results to a file and edit.

$ tar -tzf mydata.tar.gz > mout

Then, edit mout to only include the files you want

$ tar -T mout -xzf mydata.tar.gz

The above command will only get the files in mout.
Of course, if you want them all

$ tar -xzf mydata.tar.gz

Encrypt your tar files

$ tar -zcvf - mydata|openssl des3 -salt -k secretpassword | dd of=mydata.des3

This will create stuff.des3…don’t forget the password you put in place of “secretpassword”. This can be done interactively as well.

$ dd if=mydata.des3 |openssl des3 -d -k secretpassword|tar zxf -

NOTE: above there is a “-” at the end… this will extract everything.

Create an archive of numerous files residing in multiple locations

You can maintain a list of files that you want to backup into a file and tar it when you wish.

$ tar czvf tarfile.tar.gz -T list_file

where list_file is a simple list of names of files and directories you want to include into the archive – one per line. The -T option indicates the files have to be read from the list_file.

For example, my file contains the following data.

/etc/smb.conf
/root/myfile
/etc/ppp                (all files into the /etc/ppp directory)
/opt/gnome/html/gnome-dev-info.html
0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Related posts: