Welcome to Part 12 of the Linux Bash Course. In this section, we cover file compression, system task automation, and service management using the modern systemd supervisor daemon.
Chapter 34: Archiving & Compression
34.1 Understanding the Difference Between Archiving and Compression
- Archiving combines multiple files and directories into a single file container (like a
.tarfile) to preserve permissions, structure, and metadata. It does not reduce the file size. - Compression reduces the physical size of files using algorithms (like Deflate or LZMA), often acting upon an archive container to output compressed packages like
.tar.gz.
34.2 Creating and Extracting Tape Archives with tar
tar (Tape Archive) is the standard archiving utility in Linux.
-
Syntax:
tar [options] archive_name.tar target_files_or_directories -
Example Command:
tar -cvf backup.tar /home/suresh/documents -
Expected Output:
tar: Removing leading '/' from member names /home/suresh/documents/ /home/suresh/documents/notes.txt /home/suresh/documents/report.pdf -
Flag & Command Breakdown:
tar: Tape Archiver program.-c: Creates a new archive.-v: Verbose; lists each file processed.-f backup.tar: Specifies the target output archive filename./home/suresh/documents: Directory path to pack into the archive.
34.3 Compressing Files with gzip and gunzip
gzip uses the DEFLATE algorithm to compress individual files, replacing them with a .gz version. gunzip reverses this action.
-
Syntax:
gzip [options] filename gunzip filename.gz -
Example Command:
gzip large_file.log -
Expected Output: (No output on success. The file
large_file.logis replaced bylarge_file.log.gz) -
Flag & Command Breakdown:
gzip: GNU zip compression utility.large_file.log: Target file to compress.
34.4 High-Ratio Compression with bzip2 and bunzip2
bzip2 uses the Burrows-Wheeler block sorting text compression algorithm. It compresses better than gzip but is slower.
-
Syntax:
bzip2 filename bunzip2 filename.bz2 -
Example Command:
bzip2 database.sql -
Expected Output: (No output on success. Creates
database.sql.bz2) -
Flag & Command Breakdown:
bzip2: Block-sorting compressor.database.sql: Target file to compress.
34.5 Modern High-Efficiency Compression with xz and unxz
xz uses the LZMA2 compression algorithm. It provides the highest compression ratios but consumes the most CPU cycles and RAM.
-
Syntax:
xz [options] filename unxz filename.xz -
Example Command:
xz -9 software.tar -
Expected Output: (No output on success. Creates
software.tar.xz) -
Flag & Command Breakdown:
xz: High-efficiency data compression tool.-9: Sets compression level to maximum (slowest, best ratio).
34.6 Multi-Platform Archiving with zip and unzip
zip and unzip are common tools for compressing and packaging files compatible with Windows and macOS.
-
Syntax:
zip [options] archive.zip files unzip archive.zip -
Example Command:
zip -r project.zip /home/suresh/project -
Expected Output:
adding: home/suresh/project/ (stored 0%) adding: home/suresh/project/main.js (deflated 65%) -
Flag & Command Breakdown:
zip: Zip compression tool.-r: Recursive directory processing.project.zip: Output ZIP file./home/suresh/project: Target directory to package.
34.7 Archiving with Compression combinations (tar.gz, tar.bz2, tar.xz)
tar can compress archives directly using built-in flags that run gzip, bzip2, or xz.
-
Syntax:
tar [compression_flag]cvf archive.tar.ext targets -
Example Command:
tar -zcvf archive.tar.gz /var/log/nginx -
Expected Output:
tar: Removing leading '/' from member names /var/log/nginx/ /var/log/nginx/access.log -
Flag & Command Breakdown:
-z: Compresses the archive usinggzip(output:.tar.gz).-j: Compresses usingbzip2(output:.tar.bz2, if used instead of-z).-J: Compresses usingxz(output:.tar.xz, if used instead of-z).-c: Creates the archive.-v: Verbose output.-f: Output file designation.
34.8 Extracting Specific Files and Directories from Archives
You can extract individual items from a .tar archive without unpacking the entire container.
-
Syntax:
tar -xvf archive.tar specific_file_path -
Example Command:
tar -zxvf backup.tar.gz home/suresh/documents/notes.txt -
Expected Output:
home/suresh/documents/notes.txt -
Flag & Command Breakdown:
-x: Extracts files.-z: Decompresses with gzip (required because source is a.tar.gz).home/suresh/documents/notes.txt: Extracts only this path from the archive.
34.9 Listing Archive Contents without Extracting
To inspect the files inside a compressed archive without modifying your filesystem, use the list option.
-
Syntax:
tar -tvf archive.tar -
Example Command:
tar -tvf backup.tar.gz -
Expected Output:
drwxr-xr-x suresh/suresh 0 2026-06-12 08:00 home/suresh/documents/ -rw-r--r-- suresh/suresh 1024 2026-06-12 08:00 home/suresh/documents/notes.txt -
Flag & Command Breakdown:
-t: Lists files in the archive.-v: Shows permissions, ownership, sizes, and timestamps.-f: Target archive file.
34.10 Splitting and Joining Large Archives using split and cat
The split command breaks large archive files into smaller parts, and cat combines them back together.
-
Syntax:
split [options] input_file prefix cat prefix* > combined_file -
Example Command:
split -b 100M large_backup.tar.gz backup_part_ cat backup_part_* > restored_backup.tar.gz -
Expected Output: (Generates files:
backup_part_aa,backup_part_ab,backup_part_ac, etc.) -
Flag & Command Breakdown:
split: File partition utility.-b 100M: Splitting threshold of 100 Megabytes per segment.backup_part_: Prefix prepended to output segment files.
Chapter 35: Scheduling with Cron
35.1 Introduction to Task Automation in Linux: Cron vs Anacron
- Cron is a background daemon (
cronorcrond) that runs scheduled jobs at specific times. It assumes the host system is powered on continuously (e.g., servers). - Anacron runs commands at daily, weekly, or monthly intervals. If the computer is powered off when a job is scheduled, Anacron runs it when the system boots up again (e.g., laptops, workstations).
35.2 Understanding the Cron Tab (crontab) file format and layout
A crontab entry has 5 time parameters followed by the command:
* * * * * command_to_execute
┬ ┬ ┬ ┬ ┬
│ │ │ │ └─ Day of Week (0-6) (0 is Sunday)
│ │ │ └─── Month (1-12)
│ │ └───── Day of Month (1-31)
│ └─────── Hour (0-23)
└───────── Minute (0-59)
- Wildcards:
*: Matches every value.,: Multi-value separator (e.g.,0,30for minute 0 and 30).-: Value ranges (e.g.,1-5for Mon-Fri)./: Step values (e.g.,*/10for every 10 minutes).
35.3 Editing and Listing Cron Jobs: crontab -e and crontab -l
These flags configure a user’s crontab tasks.
-
Syntax:
crontab [options] -
Example Command:
crontab -l -
Expected Output:
# m h dom mon dow command 0 2 * * 1-5 /home/suresh/bin/backup.sh -
Flag & Command Breakdown:
crontab: Scheduled jobs editor.-l: Lists the active cron jobs for the current user.-e: (Alternative flag) Opens the crontab file in the default editor (likenanoorvi).
35.4 Using Cron time shortcuts (e.g. @reboot, @daily, @hourly)
Cron supports clean text placeholders that replace the five-star time format:
@reboot: Runs once when the system daemon starts up.@hourly: Shortcut for0 * * * *.@daily/@midnight: Shortcut for0 0 * * *.@weekly: Shortcut for0 0 * * 0.@monthly: Shortcut for0 0 1 * *.@yearly/@annually: Shortcut for0 0 1 1 *.
35.5 Scheduling One-Off Tasks with the at command
at schedules a single task to run once at a future time.
-
Syntax:
at time_expression -
Example Command:
echo "/home/suresh/bin/clean_tmp.sh" | at 2:30 AM tomorrow -
Expected Output:
warning: commands will be executed using /bin/sh job 4 at Sat Jun 13 02:30:00 2026 -
Flag & Command Breakdown:
at: One-off scheduling utility.2:30 AM tomorrow: The target time parser expression.
35.6 Managing System-Wide Cron Directories (/etc/cron.d/, /etc/cron.daily/)
In addition to user-specific crontabs, Linux administrators use root directories to schedule services:
/etc/crontab: System-wide cron configuration file (requires specifying user context)./etc/cron.d/: Directory for drop-in cron configurations./etc/cron.hourly/,/etc/cron.daily/,/etc/cron.weekly/,/etc/cron.monthly/: Scripts placed here run automatically at these intervals.
35.7 Restricting Cron usage: /etc/cron.allow and /etc/cron.deny
Access control files check which accounts can run crontab:
- If
/etc/cron.allowexists, only users listed within can edit their crontab. - If
cron.allowdoes not exist, cron reads/etc/cron.deny. Anyone listed incron.denyis blocked from runningcrontab. - If neither exists, only the superuser can use cron on some distributions, or all users can on others.
35.8 Capturing Cron output, redirects, and sending email notifications (MAILTO)
- By default, cron emails stdout and stderr to the owner’s local inbox.
- Setting
MAILTO="[email protected]"at the top of the crontab routes these outputs to an external address. - To discard outputs and prevent local mail generation, redirect stdout and stderr to
/dev/null:0 5 * * * /path/to/script.sh >/dev/null 2>&1
35.9 Troubleshooting Cron jobs, environment variables, and absolute paths
- Cron runs under a stripped-down environment with a minimal
PATH(typically only/usr/bin:/bin). - Best Practice 1: Use absolute paths for all commands inside cron files (e.g.
/usr/local/bin/nodeinstead of justnode). - Best Practice 2: Explicitly export your
PATHor source your shell configuration at the top of script files.
35.10 Logging Cron actions and auditing the system log files
Cron logs events to the system logs, which you can query using journalctl or by checking log files directly.
-
Syntax:
grep cron /var/log/syslog -
Example Command:
sudo grep -i cron /var/log/syslog | tail -n 2 -
Expected Output:
Jun 12 08:00:01 server cron[602]: (suresh) CMD (/home/suresh/bin/backup.sh) Jun 12 08:05:01 server cron[602]: (root) CMD ( [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi) -
Flag & Command Breakdown:
grep -i cron: Filters log entries matching “cron” (case-insensitive)./var/log/syslog: The location of main system log messages.tail -n 2: Shows only the last two lines.
Chapter 36: Systemd & Services
36.1 System Initialization: From SysV Init to Systemd
- SysV Init: The traditional initialization system. It starts scripts sequentially using runlevels (0-6). This is slow and prone to dependencies blockages.
- Systemd: The modern initialization daemon. It runs system startup tasks in parallel, manages services using descriptive configuration unit files, and tracks processes using kernel Control Groups (cgroups).
36.2 Managing System Services with systemctl start, stop, and restart
systemctl is the main tool to control systemd services.
-
Syntax:
systemctl [command] service_name -
Example Command:
sudo systemctl restart nginx -
Expected Output: (No output is printed on success)
-
Flag & Command Breakdown:
systemctl: System Control utility.restart: Stops the service and starts it again.nginx: The target web server service.
36.3 Enabling and Disabling Services on Boot: systemctl enable and disable
-
Enable tells the system to start the service automatically at boot. It creates symlinks under target directories in
/etc/systemd/system/. -
Disable stops the service from starting at boot, removing the symlinks. It does not stop a service that is currently running.
-
Syntax:
systemctl [enable|disable] service_name -
Example Command:
sudo systemctl enable docker -
Expected Output:
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service. -
Flag & Command Breakdown:
enable: Configures auto-start.docker: Target container service.
36.4 Inspecting Service Status, logs, and properties with systemctl status
Shows detailed runtime information, including PID, memory usage, cgroup trees, and the most recent log entries.
-
Syntax:
systemctl status service_name -
Example Command:
systemctl status ssh -
Expected Output:
● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2026-06-12 06:00:00 UTC; 2h ago Docs: man:sshd(8) Main PID: 781 (sshd) Tasks: 1 (limit: 4677) Memory: 5.4M CPU: 121ms CGroup: /system.slice/ssh.service └─781 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups" -
Flag & Command Breakdown:
status: Queries runtime state.ssh: Target SSH daemon service.
36.5 Reading System Logs with journalctl
journalctl queries the binary logs gathered by the systemd-journald service.
-
Syntax:
journalctl [options] -
Example Command:
journalctl -u nginx.service --since "1 hour ago" -n 5 -
Expected Output:
-- Journal begins at Fri 2026-06-12 00:00:00 UTC. -- Jun 12 07:15:30 server systemd[1]: Starting nginx.service... Jun 12 07:15:31 server systemd[1]: Started nginx.service. -
Flag & Command Breakdown:
journalctl: Journal log query utility.-u nginx.service: Limits output to logs from thenginxservice unit.--since "1 hour ago": Shows only logs written in the last hour.-n 5: Returns only the 5 most recent log entries.
36.6 Creating a Custom Systemd Service Unit File
Create unit files under /etc/systemd/system/ (e.g., app.service).
- Minimal configuration file structure:
[Unit] Description=My Node App After=network.target [Service] Type=simple User=suresh WorkingDirectory=/home/suresh/app ExecStart=/usr/bin/node server.js Restart=on-failure [Install] WantedBy=multi-user.target
36.7 Controlling System Runlevels, Targets, and Boot Modes
Systemd uses “targets” instead of legacy runlevels to define how the system boots:
poweroff.target(Runlevel 0): Shuts down the system.rescue.target(Runlevel 1): Starts a single-user recovery shell.multi-user.target(Runlevel 3): Multi-user console mode (non-graphical command-line interface).graphical.target(Runlevel 5): Full GUI system shell.reboot.target(Runlevel 6): Restarts the computer.
Command to change target dynamically: sudo systemctl isolate graphical.target
36.8 Listing and Analyzing Active and Failed Units with systemctl list-units
Lists the active and loaded units currently monitored by systemd.
-
Syntax:
systemctl list-units [options] -
Example Command:
systemctl list-units --state=failed -
Expected Output:
UNIT LOAD ACTIVE SUB DESCRIPTION * apache2.service loaded failed failed The Apache HTTP Server LOAD = Reflects whether the unit definition is loaded into memory. ACTIVE = The high-level unit activation state. SUB = The low-level unit activation state. -
Flag & Command Breakdown:
list-units: Lists active units.--state=failed: Filters output to show only services that failed to start or crashed.
36.9 Analyzing Boot Performance using systemd-analyze
systemd-analyze reports the boot performance details of your system startup.
-
Syntax:
systemd-analyze [command] -
Example Command:
systemd-analyze blame | head -n 3 -
Expected Output:
8.212s networkd-dispatcher.service 7.452s accounts-daemon.service 6.104s udev-trigger.service -
Flag & Command Breakdown:
systemd-analyze: Startup performance parser.blame: Lists services in order of how long they took to initialize, helping troubleshoot slow boot times.
36.10 Reloading Systemd Daemon configuration: systemctl daemon-reload
Whenever you edit or add a service unit file, you must reload the systemd manager configuration.
-
Syntax:
systemctl daemon-reload -
Example Command:
sudo systemctl daemon-reload -
Expected Output: (No output is printed on success)
-
Flag & Command Breakdown:
daemon-reload: Reloads all unit files, recreating the dependency trees.