Bash script that finds and deletes files older than 30 days in specific subdirectories (error, processed) under a given base path.

Table of Contents#

  1. Overview
  2. Prerequisites
  3. Parameters
  4. The Script
  5. Usage
  6. How It Works
  7. Simplified Alternative
  8. Troubleshooting
  9. See Also
  10. Sources

1. Overview#

This script cleans up old files from error and processed subdirectories under an NFS-mounted file share. It recursively locates all directories named error or processed and deletes regular files within them that have not been modified in more than 29 days.

The typical use case is cleaning up processed transaction files, log outputs, or error dumps that accumulate over time and no longer need to be retained.

2. Prerequisites#

  • find - GNU findutils
  • Read/write access to the target directory tree
  • The base directory must be mounted and accessible (particularly if on NFS)

3. Parameters#

Edit the BEGIN variable at the top of the script:

VariableDescriptionExample
BEGINBase directory to search under/home/NFS/Files/

The retention period is controlled by the -mtime +29 value (files older than 29 days).

4. The Script#

Original version:

#!/bin/bash
BEGIN="/home/NFS/Files/"

for path in $(find $BEGIN \( -name error -o -name processed \) )
do
    find $path -type f -mtime +29 -delete
done

5. Usage#

Manual Execution#

chmod +x /opt/scripts/DeleteFilesOlderThan.sh
/opt/scripts/DeleteFilesOlderThan.sh

Cron Schedule#

Run daily at 04:00:

0 4 * * * /opt/scripts/DeleteFilesOlderThan.sh >> /var/log/cleanup.log 2>&1

Dry Run#

Preview what would be deleted without actually removing anything:

BEGIN="/home/NFS/Files/"
for path in $(find "$BEGIN" \( -name error -o -name processed \) -type d); do
    find "$path" -type f -mtime +29 -print
done

6. How It Works#

The script operates in two stages:

  1. Directory discovery - the outer find locates all directories named error or processed anywhere under BEGIN
  2. File cleanup - for each discovered directory, an inner find deletes regular files (-type f) with modification times older than 29 days (-mtime +29)

The -mtime +29 parameter means files modified more than 29 full 24-hour periods ago, effectively retaining roughly 30 days of files.

7. Simplified Alternative#

The nested find approach can be replaced with a single command that is more robust (handles spaces in paths and avoids subshell issues):

#!/bin/bash
set -euo pipefail

BEGIN="/home/NFS/Files/"

# Verify base path exists
if [[ ! -d "$BEGIN" ]]; then
    echo "$(date '+%F %T') - ERROR: Base directory not found: $BEGIN"
    exit 1
fi

# Single find command: match files in 'error' or 'processed' directories, older than 29 days
find "$BEGIN" -type d \( -name error -o -name processed \) \
    -exec find {} -maxdepth 1 -type f -mtime +29 -delete \;

echo "$(date '+%F %T') - Cleanup completed"

Or even simpler using path matching:

#!/bin/bash
set -euo pipefail

BEGIN="/home/NFS/Files/"

# Delete files older than 29 days in any error/ or processed/ directory
find "$BEGIN" -type f -mtime +29 \( -path "*/error/*" -o -path "*/processed/*" \) -delete

Key improvements:

  • set -euo pipefail - fail on errors and undefined variables
  • Quoted variables - handles paths with spaces
  • Base path validation - prevents silent failure if the NFS mount is down
  • Single find invocation - avoids word splitting issues from command substitution in for loops

Troubleshooting#

IssueCauseSolution
No files deletedNo files match the age thresholdVerify with -print instead of -delete; check file mtimes with stat
No such file or directoryNFS mount not availableVerify mount with mountpoint -q /home/NFS/Files before running
Files with spaces cause errorsUnquoted variable in for loopUse the simplified single-command version
Wrong files deletedfind matches files outside target dirsAdd -name "*.log" or similar pattern to limit scope
Permission deniedInsufficient privilegesRun as appropriate user or adjust directory permissions

See Also#

Sources#