Important info
Key RHCSA information
There are four important tasks which must be completed for the RHCSA exam:
tar -cf archivename.tar /files-to-include
- 'c' creates and 'f' specifies the file name. You can also add v
to enable 'verbose' mode. This will show you the actions that the command is taking.
Original
tar
command did not use dashes for options - the new tar command supports options with and without dashes.
It is also possible to add files to a tar file. You can do this by using the -r
option. e.g. tar -rvf add-to-me.tar /file-to-be-added
In addition, you can also update files inside the tar archive. This can be achieved by using the -u
option. The update option will write newer versions of files in to the tar archive. e.g. tar -uvf add-to-me.tar /updated-files
When creating archives, it is a good practice to add an appropriate file extension (.tgz or .gar etc). If you are unsure what compression has been used, do
file unsure.tar
If desired, a 'dry run' of an archive extraction can be done. This will confirm what files will be extracted. You can do this using the -t
option. e.g. tar -xvf tar-file-to-be-extracted.tar
In order to actually extract the contents, do tar -xvf tar-file-to-be-extracted.tar
. This will extract to a directory called "tar-file-to-be-extracted" in the current working directory. If, however you wish to output elsewhere then you can use the -C
option (NOTE: This is UPPER case 'c'). e.g. tar tar-file-to-be-extracted.tar -C /target-directory
.
It is also possible to extract a single file from an archive. You can do this by running the following command tar -xvf archivename.tar file-to-be-extracted
. e.g. tar -xvf /root/etc.tar etc/hosts
will extract the 'hosts' file.
Tar archives can be compressed to save space. The two usual compression algorithms are gzip or bzip2 (to decompress use gunzip and bunzip2 respectively). There is little difference in the resultant file sizes (historically bunzip2 had the better compression). In order to use gzip with the tar command add the -z
option and bunzip2 add the -j
option.
Important info
tar
Command OptionsOption | Use |
---|---|
c |
Creates an archive |
v |
Shows the verbose output while tar is working |
t |
Shows the contents of an archive |
z |
compress/decompress the archive whilst creating it using gzip |
j |
Compress/Decompress the archive whilst creating it using bzip2 |
x |
Extracts an archive |
u |
Updates an archive |
C |
Changes the working directory before performing the command |
r |
Appends files to an archive |
Command | Explanation |
---|---|
tar -xvf testfile1.tar.gz |
eXtracts Verbosely the File - i.e. this will extract testfile1.tar.gz and show all the output as files are extracted. |
tar -xf testfile.tar.gz -C test |
eXtracts Verbosely the File to 'test' directory. Note that the "test" directory must be created prior to running the command |
tar -xf testfile.tar.gz -C $(mkdir test | ls -1 | grep test) |
This will do as above, but will create the "test" directory in a one liner. There's probably a neater way to achive this, but this works |