tar command examples
Reginald is studying to be a GNU/Linux System Administrator one day, hopefully at Red Hat. Give and fully explain 10 examples uses of the tar command.
For Reginald and anyone aiming to become a proficient GNU/Linux System Administrator, mastering the tar
command is essential due to its flexibility in handling archives. Here are 10 example uses of the tar
command, each with a full explanation:
1. Creating a Basic tar Archive
tar -cvf documents.tar /home/reginald/documents
-c
creates a new archive.-v
enables verbose mode, showing files as they are added.-f
specifies the filename of the archive. This command creates an archive nameddocuments.tar
of the/home/reginald/documents
directory.
2. Extracting a tar Archive
tar -xvf documents.tar
-x
extracts files from an archive. This command extracts the contents ofdocuments.tar
into the current directory.
3. Viewing Contents of an Archive Without Extracting
tar -tvf documents.tar
-t
lists the contents of the archive without extracting them. This is useful for checking the archive's contents before extraction.
4. Creating a gzipped tar Archive
tar -czvf documents.tar.gz /home/reginald/documents
-z
uses gzip to compress the archive, making the file smaller. This command creates a compressed archive nameddocuments.tar.gz
.
5. Extracting a gzipped tar Archive
tar -xzvf documents.tar.gz
This extracts a gzipped archive. The -z
option is used to handle gzip compression.
6. Creating a bzip2 Compressed tar Archive
tar -cjvf documents.tar.bz2 /home/reginald/documents
-j
uses bzip2 for compression, typically offering better compression than gzip but is slower. This command results in adocuments.tar.bz2
compressed archive.
7. Extracting a bzip2 Compressed tar Archive
tar -xjvf documents.tar.bz2
This extracts a bzip2 compressed archive, using -j
to process the bzip2 compression.
8. Creating an Archive with Exclusions
tar -cvf documents.tar --exclude='*.mp4' /home/reginald/documents
--exclude
specifies patterns to exclude from the archive. This command creates an archive of thedocuments
directory, excluding all.mp4
files.
9. Appending Files to an Existing Archive
tar -rvf documents.tar /home/reginald/new_document.txt
-r
appends files to the end of an existing archive. This command addsnew_document.txt
to thedocuments.tar
archive.
10. Extracting Specific Files from an Archive
tar -xvf documents.tar home/reginald/documents/special_document.txt
This command extracts only special_document.txt
from the documents.tar
archive, making it useful for retrieving individual files without extracting the entire archive.
Understanding and utilizing these commands will greatly enhance Reginald's capability to manage files and directories efficiently as a future GNU/Linux System Administrator, especially in environments like Red Hat, where such skills are invaluable.