The tar command is most often used to archive software.
The format of the tar command is
whereoptions
![]()
file1
![]()
file2
...
fileN
![]()
For example, the command
#tar cvf backup.tar /etcwould pack all of the files in /etc into the tar archive backup.tar. The first argument to tar-``cvf''-is the tar ``command''. ``c'' tells tar to create a new archive file. The ``v'' option forces tar into verbose mode-printing each filename as it is archived. The ``f'' option tells tar that the next argument-backup.tar-is the name of the archive to create. The rest of the arguments to tar are the file and directory names to add to the archive.
The command
#tar xvf backup.tarwill extract the tar file backup.tar in the current directory. This can sometimes be dangerous-when extracting files from a tar file, old files are overwritten.
Furthermore, before extracting tar files it is important to know where the files should be unpacked. For example, let's say you archived the following files: /etc/hosts, /etc/group, and /etc/passwd. If you use the command
#tar cvf backup.tar /etc/hosts /etc/group /etc/passwdthe directory name /etc/ is added to the beginning of each filename. In order to extract the files to the correct location, you would need to use the following commands:
#cd /because files are extracted with the pathname saved in the archive file.
#tar xvf backup.tar
If, however, you archived the files with the command
#cd /etcthe directory name is not saved in the archive file. Therefore, you would need to ``cd /etc'' before extracting the files. As you can see, how the tar file is created makes a large difference in where you extract it. The command
#tar cvf hosts group passwd
#tar tvf backup.tarmay be used to display an ``index'' of the tar file before unpacking it. In this way you can see what directory the filenames in the archive are stored relative to, and can extract the archive from the correct location.