How to Fix a Cron Job Not Deleting Old .txt Files in a Specific Directory on Linux


2 views

You've set up a cron job to automatically clean up old .txt files in a directory, but despite the job running, the files aren't being deleted. This is a common issue that can stem from several causes.

First, let's verify your current cron job entry:

0 0 * * * bin/find /var/www/example.com/wp-content/targetdir -name \"*.txt\" -type f -mtime +7 -exec rm -rf {} \\;

There are a few potential issues here:

1. The path to 'find' might be incorrect (should typically be /usr/bin/find)
2. The escaping of quotes and semicolon might be problematic
3. Permissions could be preventing file deletion

Here's a more robust version of your cron job:

0 0 * * * /usr/bin/find /var/www/example.com/wp-content/targetdir -name "*.txt" -type f -mtime +7 -delete

Key improvements:

- Using absolute path to find
- Simpler command using -delete instead of -exec rm
- Proper quoting without unnecessary escapes

To troubleshoot why your cron job isn't working:

1. Check cron logs: grep CRON /var/log/syslog
2. Capture output by redirecting to a file:
   0 0 * * * /usr/bin/find /path/to/dir -name "*.txt" -type f -mtime +7 -delete > /tmp/cleanup.log 2>&1
3. Test the command manually in shell first

Remember that cron jobs run with limited environment variables. The user running the cron job needs:

- Execute permission on the directory
- Write permission on the files
- Proper ownership of the files

You can check permissions with:

ls -la /var/www/example.com/wp-content/targetdir

For systems with tmpwatch (or tmpreaper on Debian/Ubuntu):

0 0 * * * /usr/sbin/tmpwatch --mtime 7d /var/www/example.com/wp-content/targetdir

This tool is specifically designed for this purpose and handles edge cases better.

Here's the most reliable version I've used in production:

0 0 * * * /usr/bin/find /var/www/example.com/wp-content/targetdir -name "*.txt" -type f -mtime +7 -exec rm -f {} +

This uses:

- Full path to find
- Proper rm -f (force) without -r (recursive)
- + instead of \; for better performance with many files

I recently set up a cron job to automatically clean up old .txt files in a WordPress content directory, but despite the job appearing to run, the files remain untouched. Here's the cron entry I'm using:

0 0 * * * bin/find /var/www/example.com/wp-content/targetdir -name \"*.txt\" -type f -mtime +7 -exec rm -rf {} \\;

After investigating, I found several potential issues that could cause this behavior:

  • Path issues: The 'bin/find' path might be incorrect
  • Permission problems: The cron user might not have delete permissions
  • Syntax errors: The escaped characters might be causing issues
  • Time calculation: The mtime parameter might not work as expected

Here's how I approached troubleshooting:

# First, test the command manually
/usr/bin/find /var/www/example.com/wp-content/targetdir -name "*.txt" -type f -mtime +7 -exec ls -la {} \;

# Then check cron logs
grep CRON /var/log/syslog

# Verify permissions
sudo -u www-data /usr/bin/find /var/www/example.com/wp-content/targetdir -name "*.txt" -type f -mtime +7 -exec ls -la {} \;

After testing, here's the corrected cron entry that worked:

0 0 * * * /usr/bin/find /var/www/example.com/wp-content/targetdir -name "*.txt" -type f -mtime +6 -delete >/dev/null 2>&1

Key improvements:

  • Using full path to find (/usr/bin/find)
  • Simplified with -delete instead of -exec rm
  • Changed mtime to +6 for more reliable day counting
  • Added output redirection to prevent cron emails

For more complex deletion scenarios, consider this bash script approach:

#!/bin/bash
TARGET_DIR="/var/www/example.com/wp-content/targetdir"
LOG_FILE="/var/log/file_cleanup.log"

echo "$(date) - Starting cleanup" >> $LOG_FILE
/usr/bin/find "$TARGET_DIR" -name "*.txt" -type f -mtime +6 -print -delete >> $LOG_FILE 2>&1
echo "$(date) - Cleanup completed" >> $LOG_FILE

Then call this script from cron:

0 0 * * * /path/to/cleanup_script.sh