Categories

Our Sponsors

Helpful Unix commands

A thread dedicated to those useful Unix commands we all know and love.  Ya right. Shocked

I need a really tricky find command the other day so I came up with the following.  I wanted to save the command some where as it really shows the power of find

Find all files that are not in the images or languages directory that are older than seven days that end with .php or .doc and delete them also leave a log file of what was deleted.

find ./  \( -name images -o -name languages \) -prune -o \( -name ‘*.php’ -o -name ‘*.doc’ \) -mtime +7 -exec rm {} \; -print > /var/tmp/deleted_files

Sorry the command is all on one line.  Because the find statement is utilizing regular expressions and complex statements, ie “\(“, so you can’t use the “\” and take the statement to the next line.

To read the command better take it one step at a time.
Find all files or directories that have the name “images” OR the name “languages” and prune/remove those items found from the find results.   Using the -prune option is a great way to setup exclusions.  The next “-o” OR says if the files found are not named “images” or “languages” find the files that end in “.php” or “.doc” that are older than 7 days.  If anything is found that meets the conditions then remove those files and “print” the file name out to the log file.

  • Share/Bookmark

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes