Overview
Unchecked application logs can swiftly consume filesystem blocks, leading to critical service disruptions across production servers. To keep disk utilization predictable and prevent runaway log expansion, system administrators rely heavily on logrotate. This hands-on tutorial walks through the foundational CLI flags, configuration directives, and automated rotation lifecycles directly within an Ubuntu environment.
Validating logrotate Installation and Setting Up the Testbed
Verify whether logrotate is already installed on your system, or install it via the package manager if absent, by executing which logrotate || (apt-get update -qq && apt-get install -y logrotate):
⚙️ [Primary Command-Line Flags]
-d : Runs a dry-run debug pass to validate configuration syntax without applying changes or touching active logs.-f : Forces an immediate rotation cycle regardless of configured schedule or minimum size requirements.-v : Enables verbose diagnostic logging during rotation processing.-s : Directs rotation state tracking to a custom status file instead of the default location.-m : Specifies an alternate mail transfer binary when delivering archived log bundles.

Set up an isolated directory hierarchy for running the rotation experiments via mkdir -p /tmp/logrotate_demo/logs:

Populate the dummy application log /tmp/logrotate_demo/logs/app.log with five timestamped lines using a bash loop:

Inspect file size, ownership, and metadata for the newly created log using ls -lh /tmp/logrotate_demo/logs:

Validating Directives in Simulation Mode (-d)
Construct a custom rotation rule set defining retention, size criteria, and compression by executing cat << 'EOF' > /tmp/logrotate_demo/test.conf:

Review the configuration file to confirm all directives are properly staged using cat /tmp/logrotate_demo/test.conf:

Trigger a dry run to evaluate the target log state against your policy rules without modifying disk data via logrotate -d /tmp/logrotate_demo/test.conf:
Operating under -d displays diagnostic evaluation steps on-screen, detailing whether thresholds match and which actions would execute.

Forcing Immediate Rotation (-f) with Custom State Tracking (-s)
Bypass rotation interval checks and output state updates to an explicit path by running logrotate -f -s /tmp/logrotate_demo/custom.status /tmp/logrotate_demo/test.conf:
Here, -f overrides scheduling constraints to force execution, while -s diverts state recording to your custom tracking file.

Check the target directory to confirm the rotation took effect using ls -lh /tmp/logrotate_demo/logs:
Notice how the initial log data was moved into a compressed gzip archive (app.log.1.gz), leaving behind a fresh, empty app.log ready to receive new writes.

Inspect the recorded timestamps within the designated status ledger using cat /tmp/logrotate_demo/custom.status:
The output displays the exact date and time the rotation finished for each tracked log target.

Monitoring Verbose Output (-v) and Inspecting Compressed Archives
Simulate incoming web or service activity by appending an additional line to /tmp/logrotate_demo/logs/app.log:

Trigger another forced rotation while observing each operational stage in real time with logrotate -f -v -s /tmp/logrotate_demo/custom.status /tmp/logrotate_demo/test.conf:
Passing -v streams granular notifications covering file renaming, compression calls, and permission assignments directly to the terminal.

Review the directory once more to see how archive numbers increment using ls -lh /tmp/logrotate_demo/logs:
The latest log data has been encapsulated into app.log.1.gz, while the former archive was automatically bumped to app.log.2.gz.

Read directly from the compressed archive without extracting it by running zcat /tmp/logrotate_demo/logs/app.log.1.gz | head -n 3:
The console output verifies that all appended entries were preserved securely inside the gzip archive without truncation.

This practical walkthrough demonstrated how to evaluate log rotation configurations, force operational runs, and maintain tidy archives in Ubuntu. Adopting these proactive housekeeping practices ensures your hosts maintain consistent storage headroom while preserving critical auditing trails.