SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


dscriptbashfind
Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dscriptbashfind [2013/05/16 11:58] (current)
sjoerd created
Line 1: Line 1:
 += Script: Bash: Find and Remove Old Files =
 +This is a short special page just dedicated to one command or script. The purpose here is just for me to learn scripting. I think most people will think these snippets are too simple too take notice from.
  
 +<code bash>
 +find /​tmp/​archive/​. -type f -mtime +31 -exec rm {} \;
 +</​code>​
 +
 +* -type f makes sure only files are listed
 +* -mtime +31 makes sure only files that are modified more than 31*24 hours are listed
 +* -exec rm {} removes the files that find found
 +
 +> Note: If you have directories beneath the directories where you do your search take a look at the option -depth
 +
 += Error =
 +I got an error while running the line:
 +<code bash>
 +find /​tmp/​archive/​* -prune -type f -mtime +31 -exec rm {} \; 
 +
 +/bin/sh: /​usr/​bin/​find:​ Argument list too long
 +</​code>​
 +
 +Turns out, find doesn'​t like '​*',​ it needs the '​.',​ as used in the example above.
 +
 += Using Prune =
 +If you don't want find to be recursive you can use the -maxdepth option. However, this option is not available in every version of find. I'm running AIX right now, with of course, a find that doesn'​t support that option. There is another option, but that just makes it completely non-recursive. From the man page:
 +> To ignore a directory and the files under it, use -prune
 +
 +But than, when I use the "find . -prune"​ prune will ignore the entire directory since '​.'​ actually means the current directory. So to use this option you're forced to use '​*':​
 +<code bash>
 +find /var/data/* -prune -type f -name '​*.txt2011*'​ -mtime +3 -print
 +</​code>​
 +
 +{{tag>​scripts}}
dscriptbashfind.txt ยท Last modified: 2013/05/16 11:58 by sjoerd