Bash Script to Delete Files Older Than N Days
Use the command
find /path/to/files -type f -mtime +n -exec rm {} \; in a Bash script to delete files older than n days.Examples
Inputn=7, directory=/tmp/test
OutputDeletes all files in /tmp/test older than 7 days.
Inputn=0, directory=/var/log
OutputDeletes all files older than today (i.e., files modified before current day) in /var/log.
Inputn=30, directory=/home/user/downloads
OutputDeletes files older than 30 days in /home/user/downloads.
How to Think About It
To delete files older than n days, you look for files with modification times older than n days using
find and then remove them. The -mtime +n option finds files modified more than n days ago. Then you delete those files safely.Algorithm
1
Get the directory path and number of days (n) as input.2
Use the <code>find</code> command to locate files older than n days in the directory.3
For each found file, delete it using <code>rm</code>.4
Print a message confirming deletion or errors.Code
bash
#!/bin/bash # Directory to clean DIR="$1" # Number of days DAYS="$2" if [[ -z "$DIR" || -z "$DAYS" ]]; then echo "Usage: $0 <directory> <days>" exit 1 fi # Find and delete files older than DAYS find "$DIR" -type f -mtime +$DAYS -exec rm {} \; echo "Deleted files older than $DAYS days in $DIR."
Output
Deleted files older than 7 days in /tmp/test.
Dry Run
Let's trace deleting files older than 7 days in /tmp/test.
1
Input parameters
DIR=/tmp/test, DAYS=7
2
Find command runs
find /tmp/test -type f -mtime +7 -exec rm {} \;
3
Files deleted
All files older than 7 days in /tmp/test are removed.
| File Path | Modified Days Ago | Action |
|---|---|---|
| /tmp/test/file1.txt | 10 | Deleted |
| /tmp/test/file2.log | 8 | Deleted |
| /tmp/test/file3.tmp | 5 | Kept |
Why This Works
Step 1: Finding files older than n days
The find command with -mtime +n selects files modified more than n days ago.
Step 2: Deleting files safely
The -exec rm {} \; part runs rm on each found file to delete it.
Step 3: Using variables for flexibility
Passing directory and days as variables makes the script reusable for any folder and time period.
Alternative Approaches
Using find with -delete option
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 -delete echo "Deleted files older than $DAYS days in $DIR."
This method is simpler and faster but <code>-delete</code> is not supported on all systems.
Using find with xargs
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 rm echo "Deleted files older than $DAYS days in $DIR."
Using <code>xargs</code> can be more efficient for many files but requires careful handling of filenames with spaces.
Complexity: O(m) time, O(1) space
Time Complexity
The script runs find which scans all files in the directory tree, so time depends linearly on the number of files (m).
Space Complexity
The script uses constant extra memory since it deletes files in place without storing lists.
Which Approach is Fastest?
Using -delete is fastest as it avoids spawning rm for each file, but -exec rm is more portable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| find with -exec rm | O(m) | O(1) | Portability and safety |
| find with -delete | O(m) | O(1) | Speed on supported systems |
| find with xargs rm | O(m) | O(1) | Efficiency with many files |
Always test with
-mtime +n -print first to see which files will be deleted before running the delete command.Forgetting to escape the semicolon
\; in the -exec command causes syntax errors.