Archiving and compressing are two different concepts. Archiving is basically collecting files in a single package and a popular tool for that is called tar. Compressing is actually reducing file size by means of an algorithm and popular ones include bzip2 and gzip. I will talk about those, but first I would like to talk about another tool for creating archives: cpio.

cpio

The cpio command can be used to create a backup of the /home/ directory for instance:

$ find /home/ | cpio -o > /backup/home.cpio

The find command was used to specify every single file that should go as an input to the cpio command. The .cpio file is basically an archive file and we can unpack it by using the -i option:

$ cpio -i < /backup/home.cpio

The cpio -o command created a complete backup of the /home/ directory and it can help a user to avoid losing important files as one can recover them from the package.

Another interesting option is the -F that allows the user to add another directory to the package:

find /root/ | cpio -o -F /backup/home.cpio

To just check what files are currently in the package:

$ cpio -i --list < /backup/home.cpio

tar for archiving and compression

The tar command has a couple of options and it is important to make sure we know what we are doing:

OptionObjective
-cCreater a new tar
-pMaintains original permissions
-rAdds a file to the tar
-tShows content
-vVerbose mode
-xExtract files from tar
-zuses gzip compression
-juses bzip2 compression
-Cchange directory
-fspecify archive file

Given these options, let’s try out some commands:

# archives folder1 and folder2 in t1.tar
$ tar -cvf /backup/t1.tar folder1 folder2
# compress using gzip
# creates file t1.tar.gz from folder1
# and 2 using gzip
$ tar -czvf /backup/t1.tar.gz folder1 folder2
# use bzip2
$ tar -cjvf sbt3.tar.bz2 SBT1/

Pay extreme attention to the order of the options! I had a hard time receiving an error message for this sole reason!

$ tar -cfz backup/ataias.tar.gz ataias-personal-website
tar: backup/ataias.tar.gz: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors.

When I try -cfz, things don’t work! It works if I use -czf though. This makes me extremely annoyed as I think it should just work either way. Other sequences may work, but this one didn’t.

Other examples:

# check the contents of a tar or
# tar.bz2/gz file
$ tar -tf test.tar.bz2
# add file to a .tar file
$ tar -rf test.tar

Notice that adding a file with -r to a .tar file doesn’t work with a compressed archive. In that case, you need to decompress it then add something and compress back. Talking about that, we haven’t tried decompressing anything yet, so let’s try it out:

# decompress bz2 file in folder /tmp
$ tar -xjf test.tar.bz2 -C /tmp/
# decompress gz file in folder /tmp
$ tar -xzf test.tar.gz -C /tmp/

That’s all about archiving, compressing and decompressing for now. Thanks for reading!