SHIFT

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

User Tools

Site Tools


Sidebar

Recently Changed Pages:

View All Pages


View All Tags


LinkedIn




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.


Pages with comments

View All Comments

dscriptbashfind

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

Error

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: 2021/09/24 00:24 (external edit)