June 2025 | By Johnny @ LinuxEveryday.online
The Linux find
command is already a powerhouse for file discovery—but pair it with -exec
, and it transforms into an automation engine! Here are 5 practical find -exec
combos you’ll want in your sysadmin toolkit.
1. 🗃️ Find and Rename Files
Add a suffix or rename in bulk:
find ~/Downloads -type f -name 'ubuntu*' -exec mv {} {}_renamed \;
2. 🧮 Calculate and Store File Sizes
Create a disk usage snapshot:
sudo find /tmp/ -type f -exec du -sh {} \; > /root/du_data.out
3. 🧹 Delete Old or Large Files
Remove clutter with precision:
find ~/Desktop -size +100M -exec rm {} \;
sudo find /tmp/ -type f -mtime +10 -exec rm {} \;
4. 🔐 Bulk Permission Changes
Enforce consistent file permissions:
sudo find /var/www -type f -exec chmod 644 {} \;
5. 🔍 Search File Contents
Find matching strings inside specific files:
find . -type f -name "*.hbs" -exec grep -iH excerpt {} \;
💡 Final Tip:
Use {} \;
to execute per file and {} +
to batch them (when safe). This small syntax choice can make a big difference in performance and accuracy.
Leave a Reply
You must be logged in to post a comment.