010-160 - LPI Linux Essentials Archiving and Compression Questions and Answers — Questions and Answers
Question 1: A system administrator wants to package a directory named `/project_logs` into a single compressed file. They need to ensure the highest possible compression ratio, as storage space is limited. Speed is not a primary concern. Which command should they use?
- tar -czf project_logs.tar.gz /project_logs
- tar -cjf project_logs.tar.bz2 /project_logs
- tar -cJf project_logs.tar.xz /project_logs (Correct answer)
- zip -r project_logs.zip /project_logs
Correct answer: tar -cJf project_logs.tar.xz /project_logs
The `tar` command is used to create archives. The `-c` flag creates an archive, `-f` specifies the filename. The compression method is chosen by another flag: `-z` for gzip, `-j` for bzip2, and `-J` for xz. Among these options, xz (`-J`) generally provides the best compression ratio, which is the key requirement of the scenario.
Question 2: You have a compressed archive named `backup.tar.gz`. Before extracting it, you need to see the list of files it contains without modifying the archive. Which of the following commands will achieve this?
- tar -xvf backup.tar.gz
- tar -cvf backup.tar.gz
- gunzip backup.tar.gz
- tar -tvf backup.tar.gz (Correct answer)
Correct answer: tar -tvf backup.tar.gz
The `tar` command with the `-t` (or `--list`) option is used to list the contents of an archive without extracting them. The `-v` option provides a verbose (detailed) listing, and `-f` specifies the archive file. The `-x` option would extract the files, and `-c` would try to create a new archive. `gunzip` would decompress the file but not list the contents of the `.tar` archive within.
Question 3: By default, when you use the `gzip` command on a file (e.g., `gzip myfile.txt`), what happens to the original file?
- The original file is renamed to myfile.txt.gz.
- The original file is deleted and replaced by myfile.txt.gz. (Correct answer)
- The original file remains, and a new file named myfile.txt.gz is created.
- An error is produced, as an output file must be specified.
Correct answer: The original file is deleted and replaced by myfile.txt.gz.
The default behavior of the `gzip` command is to compress the specified file and replace it with the compressed version, which has a `.gz` extension. The original file is removed. To keep the original file, the `-k` (or `--keep`) option must be used.
Question 4: A user has an archive named `data.tar.bz2` and needs to extract only a single file named `config/settings.conf` from within it. Which command will accomplish this specific task?
- tar -xjf data.tar.bz2 config/settings.conf (Correct answer)
- tar -xjf config/settings.conf data.tar.bz2
- bunzip2 data.tar.bz2 && tar -xf data.tar config/settings.conf
- tar -tjf data.tar.bz2 | grep config/settings.conf
Correct answer: tar -xjf data.tar.bz2 config/settings.conf
To extract a specific file from a `tar` archive, you provide the path to that file as an argument after the archive name. The command `tar -xjf data.tar.bz2` correctly specifies the extraction (`-x`), indicates it's a bzip2 file (`-j`), and provides the archive filename (`-f data.tar.bz2`), followed by the specific member to extract. While the `bunzip2` and `tar` combination would work, it's an unnecessary two-step process. `grep` would only list the file, not extract it.
Question 5: Which of the following commands is used to decompress a file that has a `.bz2` extension?
- gunzip
- unzip
- bunzip2 (Correct answer)
- unxz
Correct answer: bunzip2
Files with a `.bz2` extension are compressed with the bzip2 algorithm. The corresponding command to decompress them is `bunzip2`. `gunzip` is for `.gz` files, `unzip` is for `.zip` files, and `unxz` is for `.xz` files.
Question 6: A user needs to create a single archive file from two separate files, `report.txt` and `data.csv`, and then compress it using `gzip`. Which single command can achieve this?
- gzip report.txt data.csv > archive.gz
- tar -cf archive.tar report.txt data.csv && gzip archive.tar
- zip archive.zip report.txt data.csv
- tar -czf archive.tar.gz report.txt data.csv (Correct answer)
Correct answer: tar -czf archive.tar.gz report.txt data.csv
The `tar` command is designed to bundle multiple files or directories into a single archive file. The `-c` option creates an archive, the `-z` option filters it through `gzip` for compression, and the `-f` option specifies the output filename. This single command correctly creates the archive and compresses it simultaneously. The `gzip` command itself can only operate on single files, not combine them.
A system administrator wants to package a directory named `/project_logs` into a single compressed file.
They need to ensure the highest possible compression ratio, as storage space is limited.
Speed is not a primary concern.
Which command should they use?