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

User Tools

Site Tools



Would you like to sponsor this site?
Or buy me a beer?:

Recently Changed Pages:

View All Pages
View All Q Pages

View All Tags

Sign up for Q to post comments.

WIKI Disclaimer: As with most other things on the Internet, the content on this wiki is not supported. It was contributed by me and is published “as is”. It has worked for me, and might work for you.
Also note that any view or statement expressed anywhere on this site are strictly mine and not the opinions or views of my employer.

Terms And Conditions for Q users

Pages with comments

2019/06/22 10:36 1 Comment
2019/03/15 16:02 1 Comment
2019/03/15 16:02 1 Comment
2019/03/15 16:02 3 Comments
2017/04/20 15:28 1 Comment
2017/04/20 15:23 1 Comment
2017/04/19 14:44 1 Comment
2017/04/17 20:10 1 Comment
2017/04/17 20:07 1 Comment
2017/04/17 19:58 1 Comment
2017/04/17 19:52 1 Comment

View All Comments


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.

find /tmp/archive/. -type f -mtime +31 -exec rm {} \;
  • -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


I got an error while running the line:

find /tmp/archive/* -prune -type f -mtime +31 -exec rm {} \; 
/bin/sh: /usr/bin/find: Argument list too long

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 '*':

find /var/data/* -prune -type f -name '*.txt2011*' -mtime +3 -print
You could leave a comment if you were logged in.
dscriptbashfind.txt · Last modified: 2013/05/16 11:58 by sjoerd