Operating System - HP-UX
1832085 Members
2923 Online
110037 Solutions
New Discussion

Question about deleting files by date

 
Kevin Farrell_4
Frequent Advisor

Question about deleting files by date

Hello, I'm looking for the syntax with rm to remove all files in a directory older than a specific date. Any help is appreciated.

Thanks, Kevin
7 REPLIES 7
Peter Godron
Honored Contributor

Re: Question about deleting files by date

Kevin,
rm does not have a option for this.
My suggestions would be to use find -newer
You can then use the exec part to remove the files.
man find should get you started.
Regards
Pete Randall
Outstanding Contributor

Re: Question about deleting files by date

Or you can use other find options like -mtime:

find /start_dir -type f -mtime +10 |xargs rm

There are good examples in the find man page.


Pete

Pete
Muthukumar_5
Honored Contributor

Re: Question about deleting files by date

You can do with find command as,

touch /filecheck

find -type f -newer filecheck -exec rm -i {} \;

or you can use,

find -type f -mtime -10 | xargs rm -f

-mtime is used to check modified time over 10 days.

hth.
Easy to suggest when don't know about the problem!
Kevin Farrell_4
Frequent Advisor

Re: Question about deleting files by date

What will the find command do? I need to delete them. The problem is this. Oracle Concurrent manager files. There is a program to delete the old logs, based upon number of days old. Say, delete all logs and clean concurrent manager table for all requests older than 20 days. However the log deleting portion doesn't work.

I can't work with the directory on FTP. Reflections just goes kaput. So there is no way in unix to delete files in a directory older than a certain date? I need to keep some of the files so users can view past logs of past requests, otherwise I'd just blow away the whole directory and recreate it.

Kevin
Bill Hassell
Honored Contributor

Re: Question about deleting files by date

As mentioned, find is the command to locate files by last modification date. An alternate way to run find (instead of xargs) is to use -exec as in:

find /someplace -type f -mtime -10 -exec rm {} \;

HOWEVER: if you run this command as root, you have the ability (with a spelling error) to destroy all files in the entire computer!! As an example, if you accdently type / someplace rather than /someplace (see the extra space?) then find locates all files in the / directory--on every mountpoint--in every directory--including NFS! So always run a test command by using echo first:

find /someplace -type f -mtime -10 -exec echo rm {} \;

Also, the default behavior for find is to descend into all subdirectories. If you're cleaning out a flat directory, no problem. Or if you are cleaning out everything in all subdirectories, the above is still OK. But if you don't want to pickup anything in sub-directories, you need to do two things: add the -only option (which prevents looking at any directories), and add the * after your pathname:

find /someplace/* -only -type f -mtime -10 -exec echo rm {} \;

-only is not quite as simple as it would seem. When find is given a directory name, the first test is on that starting directory (not the contents) and -only says "don't look in any directories" - including this one). So to get rid of files in a specific directory, you have to give find the list of files to examine. As with all shells, //* will be expanded into all the filenames in /.


Bill Hassell, sysadmin
Peter Godron
Honored Contributor

Re: Question about deleting files by date

Kevin,
as Bill above explains :
find - finds the files
exec rm or xargs - removes the files

Regards
Joseph Loo
Honored Contributor

Re: Question about deleting files by date

hi kevin,

as the rest has mentioned, use the find command. besides the concurrent (log) logs, take note of the Oracle output (out) files as well, it grows very fast as well.

# find -mtime + -exec rm {} \;

u may like to write a script which deletes these files and run it as a cron.

also, last thing, u may also like to assign points to show your appreciation to those who have assisted u in finding your answers. 0 out of 28 (and counting) is not good.

regards.
what you do not see does not mean you should not believe