How to Delete Files in Linux by Date or Pattern (With Safe Examples)

Published On: October 3, 2025
Follow Us
How to Delete Files in Linux by Date or Pattern

Managing files in Linux becomes especially important when you’re dealing with logs, backups, or large numbers of auto-generated files. Instead of deleting files manually, Linux offers powerful tools to remove files based on name patterns, file age, or count — safely and efficiently.

This beginner-friendly guide covers:

  • Deleting files with matching names
  • Removing old files based on date
  • Keeping only the latest X files
  • Safety tips to avoid accidental deletion

🧹 Why Use Conditional Deletion?

If you work with scheduled backups or logs, you may end up with hundreds of files like:

backup-2023-09-01.tar.gz  
backup-2023-09-02.tar.gz  
...
backup-2025-10-03.tar.gz

Instead of removing each manually, let Linux handle it smartly.


🔸 Delete Files by Pattern (Using rm with Wildcards)

You can remove all files that match a certain name or pattern using *:

rm backup-2023*

This command deletes all files starting with backup-2023 in the current directory.

✅ Useful for deleting logs or backups by year/month
🛑 Caution: Wildcards are powerful. Double-check before hitting Enter.


🔸 Delete Files Older Than X Days (Using find)

The find command is ideal for deleting old files, especially based on last modification time.

📌 Syntax:

find [path] -type f -name "[pattern]" -mtime +[days] -exec rm {} \;

✅ Example: Delete all .log files older than 30 days:

find /var/log -type f -name "*.log" -mtime +30 -exec rm {} \;
FlagMeaning
-type fSearch for files only
-nameMatch files with a pattern
-mtime +30Files modified more than 30 days ago
-exec rm {} \;Delete each matched file

🔍 Just want to preview files before deleting?

Use:

find . -type f -name "*.log" -mtime +30

🔸 Keep the Most Recent Files (Using ls + xargs)

Let’s say you want to keep only the latest 5 backups and delete the rest.

ls -t backup-*.tar.gz | tail -n +6 | xargs rm
PartWhat It Does
ls -tSort files by modified time (newest first)
tail -n +6Skip first 5 files, show the rest
xargs rmDelete those files

✅ Safe way to limit file retention by count
💡 Adjust +6 to +11 if you want to keep 10 files, etc.


🔸 Practice: Try It Yourself

Create test files and try deletion without harming real data.

cd ~
mkdir test-cleanup
cd test-cleanup

# Create sample files with simulated dates
touch -d "2023-01-01" old1.log
touch -d "2023-06-01" old2.log
touch -d "2025-10-01" new1.log
touch -d "2025-10-03" new2.log

Now delete files older than 300 days:

find . -type f -name "*.log" -mtime +300 -exec rm {} \;

🛡️ Safety Tips Before Deleting Files in Bulk

TipWhy It Matters
Always test with ls or find firstSee what will be deleted
Use -i flag for confirmatione.g. rm -i * asks before each file
Avoid rm -rf unless 100% sureOne wrong path can delete everything!
Keep backupsEspecially for scripts or production systems

📌 Summary Table: Deleting Files in Linux

TaskCommand Example
Delete files matching a patternrm backup-2023*
Delete .log files older than 30 daysfind . -name "*.log" -mtime +30 -exec rm {} \;
Preview files to be deletedfind . -name "*.log" -mtime +30
Keep latest 5 files, delete the rest`ls -t backup-*
Delete files interactivelyrm -i *.txt

✅ Conclusion

Conditional deletion is a must-know skill for anyone working on Linux. It helps you:

  • Save disk space
  • Automate cleanup
  • Manage large volumes of files

Start with listing files safely using find, then work up to automating file cleanup with scripts.

sapan singh

👨‍💻 About Sapan Singh Hi, I’m Sapan Singh — a passionate software developer with a strong love for technology, gaming, and building useful digital tools.

Join WhatsApp

Join Now

Join Telegram

Join Now

Leave a Comment