Delete Old Files Automatically in Linux

x32x01
  • by x32x01 ||
Over time, your Linux system can get cluttered with unused or outdated files 🗂️, especially in directories like Downloads or logs. Cleaning these up regularly saves disk space and keeps your system fast ⚡. In this guide, you’ll learn how to find and delete files older than 30 days using simple terminal commands.

💡 Why You Should Delete Old Files​

Removing old files:
  • Frees up valuable storage space 💾
  • Keeps your directories organized
  • Prevents performance slowdowns

⚠️ Important: Make sure you don’t need those files before deleting! These commands won’t ask for confirmation.



🧭 Understanding the mtime Command​

mtime stands for Modification Time - it refers to the last time a file’s content was changed.
  • -mtime +30 means “files modified more than 30 days ago.”
  • Each number equals 24 hours. So, -mtime +2 = 2 days.



🔍 Example 1: Find & Delete Files Older Than 30 Days​

First, let’s just list old files under /home/linuxgeek/Downloads to review them safely:
Code:
find /home/linuxgeek/Downloads -type f -mtime +30 -print

Once you confirm they’re not needed, delete them:
Code:
find /home/linuxgeek/Downloads -type f -mtime +30 -exec rm -f {} \;
or
Code:
find /home/linuxgeek/Downloads -type f -mtime +30 | xargs rm -f

🧩 Command Breakdown​

  • find: search command
  • -type f: only files
  • -mtime +30: files older than 30 days
  • -exec rm -f: execute delete command
  • {}: represents each found file
  • \;: ends the command



🎯 Example 2: Delete Files Older Than X Days by Name Pattern​

If you have specific log or temporary files (e.g., starting with “Trans_suc”), test first with:
Code:
find /home/linuxgeek/Downloads -type f -name "Trans_suc*" -mtime +30 -print

Then, once confirmed, delete them safely:
Code:
find /home/linuxgeek/Downloads -type f -name "Trans_suc*" -mtime +30 -exec rm -f {} \;
or
Code:
find /home/linuxgeek/Downloads -type f -name "Trans_suc*" -mtime +30 | xargs rm -f



🧾 Example 3: Delete Files Older Than 10 Days by Extension​

Need to clean up .log files older than 10 days in /var/log?
First, preview:
Code:
find /var/log -type f -name "*.log" -mtime +10 -print

Then remove them:
Code:
find /var/log -type f -name "*.log" -mtime +10 -exec rm -f {} \;
or
Code:
find /var/log -type f -name "*.log" -mtime +10 | xargs rm -f

🧠 You can replace .log with .jpg, .zip, .txt, etc. to target specific file types.

🧠 Pro Tip​

Want to automate cleanup? Add your find command to a cron job to run it weekly or monthly.
Example:
Code:
0 3 * * 7 find /home/user/Downloads -type f -mtime +30 -exec rm -f {} \;
That runs cleanup every Sunday at 3 AM ⏰



🧩 Final Thoughts​

Regularly deleting old files helps keep your system optimized and clutter-free. Whether you choose 10, 30, or 90 days, Linux’s find command gives you total control over file cleanup - fast and automatic 💪.
 
Last edited:

Related Threads

x32x01
  • x32x01
Replies
0
Views
1K
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
1K
x32x01
x32x01
x32x01
Replies
0
Views
1K
x32x01
x32x01
x32x01
Replies
0
Views
1K
x32x01
x32x01
x32x01
Replies
0
Views
1K
x32x01
x32x01
TAGs: Tags
automate file deletion bash automation cron job linux find mtime command linux cleanup script linux delete old files linux disk cleanup linux file management remove old logs linux system maintenance linux
Register & Login Faster
Forgot your password?

Latest Resources

Forum Statistics
Threads
745
Messages
750
Members
71
Latest Member
Mariaunmax
Back
Top