0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Compress Files Older Than N Days

Use find /path/to/files -type f -mtime +n -exec gzip {} \; in a Bash script to compress files older than n days, where n is the number of days.
📋

Examples

Input/tmp/testfiles, n=0
OutputAll files in /tmp/testfiles modified more than 0 days ago are compressed with gzip, creating .gz files.
Input/var/log, n=7
OutputFiles in /var/log older than 7 days are compressed, reducing disk space usage.
Input/home/user/docs, n=30
OutputFiles older than 30 days in /home/user/docs are compressed, leaving newer files untouched.
🧠

How to Think About It

To compress files older than n days, first identify those files using find with the -mtime +n option, which selects files modified more than n days ago. Then, for each found file, run a compression command like gzip to compress it. This approach automates cleanup and saves space.
📐

Algorithm

1
Get the directory path and number of days (n) as input.
2
Use the find command to locate files older than n days in the directory.
3
For each file found, compress it using gzip.
4
Print a message for each compressed file.
5
End the script.
💻

Code

bash
#!/bin/bash

DIR="$1"
DAYS="$2"

if [[ -z "$DIR" || -z "$DAYS" ]]; then
  echo "Usage: $0 <directory> <days>"
  exit 1
fi

find "$DIR" -type f -mtime +$DAYS -exec gzip {} \; -print
Output
/tmp/testfiles/file1.txt.gz /tmp/testfiles/file2.log.gz
🔍

Dry Run

Let's trace compressing files older than 1 day in /tmp/testfiles.

1

Input parameters

DIR=/tmp/testfiles, DAYS=1

2

Find files older than 1 day

find /tmp/testfiles -type f -mtime +1

3

Compress each found file

gzip /tmp/testfiles/oldfile.txt

File FoundAction
/tmp/testfiles/oldfile.txtgzip /tmp/testfiles/oldfile.txt
/tmp/testfiles/oldlog.loggzip /tmp/testfiles/oldlog.log
💡

Why This Works

Step 1: Finding old files

The find command with -mtime +n selects files modified more than n days ago.

Step 2: Compressing files

The -exec gzip {} \; runs gzip on each found file, compressing it in place.

Step 3: Printing compressed files

The -print option outputs the name of each compressed file for confirmation.

🔄

Alternative Approaches

Using tar with find
bash
#!/bin/bash

DIR="$1"
DAYS="$2"

if [[ -z "$DIR" || -z "$DAYS" ]]; then
  echo "Usage: $0 <directory> <days>"
  exit 1
fi

FILES=$(find "$DIR" -type f -mtime +$DAYS)
if [[ -n "$FILES" ]]; then
  tar -czf old_files_$(date +%Y%m%d).tar.gz $FILES
  echo "Compressed files into old_files_$(date +%Y%m%d).tar.gz"
else
  echo "No files older than $DAYS days found."
fi
This method archives all old files into one compressed tarball, which is easier to manage but does not compress files individually.
Using find with xargs and gzip
bash
#!/bin/bash

DIR="$1"
DAYS="$2"

if [[ -z "$DIR" || -z "$DAYS" ]]; then
  echo "Usage: $0 <directory> <days>"
  exit 1
fi

find "$DIR" -type f -mtime +$DAYS -print0 | xargs -0 gzip

find "$DIR" -type f -name '*.gz' -print
Using xargs can be more efficient for many files by reducing the number of gzip executions.

Complexity: O(n) time, O(1) space

Time Complexity

The script runs find once over all files, which is O(n) where n is the number of files. Each matching file is compressed individually, so total time depends on the number of old files.

Space Complexity

The script uses constant extra space, compressing files in place without storing large data in memory.

Which Approach is Fastest?

Using find -exec gzip compresses files one by one, while find | xargs gzip can be faster by batching. Archiving with tar compresses all files at once but requires extra disk space.

ApproachTimeSpaceBest For
find -exec gzipO(n)O(1)Simple per-file compression
find | xargs gzipO(n)O(1)Faster batch compression
tar archiveO(n)O(n)Single archive of many files
💡
Always test your script with a small directory and few days before running on important data.
⚠️
Forgetting to escape the semicolon \; in the find -exec command causes syntax errors.