Archiving and Removing Outdated Log Files
Your final task is a bit of housekeeping. The ~/project/logs directory is accumulating log files, and the ones from 2023 are no longer needed for daily operations. To save space and keep things tidy, you need to compress these old logs into a single archive file and then remove the original files.
Understanding the tar Command
The tar command is a powerful Linux tool for creating and manipulating archive files. "Tar" originally stood for "Tape Archive" because it was designed to write data to magnetic tapes, but today it's commonly used to create compressed archive files on disk.
When you use tar, you're essentially bundling multiple files together into a single file (called an archive), and you can optionally compress this archive to save space. The most common compression format is gzip, which adds the .gz extension to the filename.
The tar command uses different options (flags) to control its behavior:
c: Create a new archive
z: Compress the archive using gzip
f: Specify the filename of the archive
So tar -czf archive.tar.gz file1 file2 creates a new compressed archive named archive.tar.gz containing file1 and file2.
Tasks
- Navigate to the
~/project/logs directory.
- Create a compressed tar archive named
old_logs.tar.gz that contains all log files from the year 2023.
- After the archive is successfully created, delete the original 2023 log files that you just archived.
Requirements
- The final archive must be named exactly
old_logs.tar.gz.
- The archive must be located in the
~/project/logs directory.
- Only log files with
2023 in their name should be archived and subsequently removed.
- The log file from 2024 (
app_2024-05-01.log) must not be included in the archive and must not be deleted.
Examples
Before archiving, your logs directory contains:
~/project/logs/
├── app_2023-01-15.log
├── app_2024-05-01.log
└── db_2023-02-20.log
After completing the archiving task, your logs directory should look like:
~/project/logs/
├── app_2024-05-01.log
└── old_logs.tar.gz
When you run ls in the ~/project/logs/ directory, you should see:
app_2024-05-01.log old_logs.tar.gz
Hints
- Use the
tar command to create archives. The options -czf are a powerful combination: c (create), z (compress with gzip), and f (specify filename).
- You can use a wildcard (
*) to select multiple files that match a pattern. For example, *_2023-*.log will match all files that end with .log and have _2023- in their name.
- The
rm command is used to remove (delete) files. Be careful when using it with wildcards!