[el7_blog]
/dev/urandom

archives and compressed files

The Tape ARchiver tar utility is used to archive files.

Creating Archives W/tar
To create an archive, you use the tar -cf ArchiveName.tar /FilesToBeArchived command. To see what’s happening, verbose output use the -v option; tar -cvf etc_directory.tar /etc.

Note:

  • It’s good practice to create archive files with an extension such as .tar or .tgz for easy recognition. Not everyone does this… If you think a file is a tar archive, use the file command.

# file etc_directory.tar
etc_directory.tar: POSIX tar archive (GNU)


While managing archives with tar, it is also possible to add a file to an existing archive, or to update an archive. To add files to an archive, you use the -r option. Use for instance tar -rvf etc_directory.tar /root/System_Administration/ to add /root/System_Administration/ to etc_directory.tar.

To update a currently existing archive file, use the -u option. So tar -uvf etc_directory.tar /root/System_Administration/ to write newer versions of all files in /root/System_Administration/ to etc_directory.tar.

Monitoring and Extracting tar Files
Before extracting a file, it’s good to know what might be expected. The -t option can be used to find out. Type for instance tar -tvf etc_directory.tar | less to see the contents of the etc_directory.tar archive.

To extract the contents of an archive, use tar -xvf ArchiveName.tar. The archive will be extracted into the current working directory.

Note:
*This might not be what you want to accomplish, two solutions exist to extract the contents where you want:

  • cd to the directory where you want to extract the archive
  • 
    mkdir extraction_directory
    cd extraction_directory/
    tar -xvf ../etc_directory.tar
    
    
  • Use the -C extraction_directory option to specify the target directory, in our case extraction_directory
  • tar -xvf etc_directory.tar -C /tmp/extraction_directory/


Apart from extracting an entire archive, it is also possible to extract one file out of the archive. Use tar -xvf etc_directory.tar etc/hosts.

Note:
*This might not be what you want to accomplish… To extract etc/hosts from etc_directory.tar to /tmp use:
tar -xvf etc_directory.tar -C /tmp etc/hosts
Using Compression
Originally, after creating an archive, it had to be compressed with a separate compression utility, such as gzip or bzip2. After creating etc_directory.tar, you can compress it with gzip etc_directory.tar replacing etc_directory.tar with etc_directory.tar.gz consuming significantly less space. As an alternative to using gzip, you can use the bzip2 utility.

To decompress files that have been compressed with gzip or bzip2, you can use the gunzip and bunzip2 utilities.

An alternative to using these utilities after tar would be to include the -z, gzip or -j bzip2 options when creating the tar archive. There is no reason to use these options when extracting. The tar utility will recognize the compression automatically and decompress it for you.

Overview of tar Options

Option Use
c Creates an archive.
v Shows verbose output while tar is working
f Used to specify the name of the tar archive that is to be used.
t Shows the contents of an archive.
z Compresses the archive using gzip.
j Compresses the archive using bzip2.
x Extracts an archive.
u Updates an archive, only newer files will be written to the archive.
C Changes the working directory before performing the command.
r Appends files to an archive.