Linux shell script

Find Tips

Copy/Move files with find

find <path> -name "filename" -exec cp -prf {} /destination/{} \;
find /var/log/ -name "m*" -exec cp -prf {} /tmp/message/{} \;

This will create the same directory structure under /tmp/message, incase you want all subdirectory files under /tmp/message/ remove the {} .

Remove files older than certain days (using find/mtime)
find -name "" -mtime +N -exec rm -r {} \;

Eg : find /var/log/ -name "*.log" -mtime +5 -exec rm -r {} \;
This will remove the *.log files older than 5 days in directory /var/log/

Find with file type

directories : find / -type d -print0
files: find / -type f -print0

Leave a Reply

Your email address will not be published.