tar
Andreas is a RHEL Sysadmin studying for his LFCS exam. Please give him detail command examples to help him to master the tar command.
Certainly! Here are some detailed command examples to help Andreas master the tar
command:
Creating a tar archive:
tar -cvf archive.tar file1 file2 directory/
This command creates a tar archive named "archive.tar" and includes "file1", "file2", and "directory/" in the archive. The
-c
flag indicates the creation mode, and-v
enables verbose output.Extracting files from a tar archive:
tar -xvf archive.tar
This command extracts all files from the "archive.tar" archive. The
-x
flag specifies the extraction mode, and-v
enables verbose output.Extracting files from a compressed tar archive (e.g., tar.gz):
tar -xzvf archive.tar.gz
This command extracts all files from the compressed "archive.tar.gz" archive. The
-z
flag is used to specify gzip compression, and the-x
and-v
flags function as in the previous example.Viewing the contents of a tar archive:
tar -tvf archive.tar
This command displays a list of files included in the "archive.tar" archive. The
-t
flag indicates the "list" mode.Adding files to an existing tar archive:
tar -rvf archive.tar newfile
This command adds "newfile" to the existing "archive.tar" archive. The
-r
flag specifies the "append" mode.Extracting a specific file from a tar archive:
tar -xvf archive.tar specific/file.txt
This command extracts only the "file.txt" file from the "archive.tar" archive. The
specific/file.txt
argument specifies the path to the file within the archive.Extracting a tar archive to a specific directory:
tar -xvf archive.tar -C /path/to/directory
This command extracts the contents of the "archive.tar" archive to the "/path/to/directory" directory. The
-C
flag is used to specify the target directory.
These are just a few examples to get started. The tar
command offers many more options and functionalities. For further details and advanced usage, refer to the tar
command's manual page (man tar
). Good luck to Andreas on his LFCS exam!