Re: remove files according to date format (125 Views)
Reply
Occasional Advisor
SPMK
Posts: 9
Registered: ‎04-12-2008
Message 1 of 8 (125 Views)
Accepted Solution

remove files according to date format


Can anyone tell me, how to remove files from a directry according to the date format?..like in a directry hundreds of log files is being created and I want to delete the files been created two days befor.

Its a 11.23ia64 version
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,879
Registered: ‎03-06-2006
Message 2 of 8 (125 Views)

Re: remove files according to date format

Do the logfiles include the date in their name?
If not, you can only remove files that were modified 2 (or more if you want) days ago.

This would be like:
find path-to-dir -mtime 2 -exec rm -f {} +

If you only want to remove files with a specific name:
find path-to-dir/file-name-pattern ...
Please use plain text.
Honored Contributor
Yogeeraj_1
Posts: 4,613
Registered: ‎11-25-2001
Message 3 of 8 (125 Views)

Re: remove files according to date format

hi,

try something like:

DIR=/data/export
find $DIR -xdev -type f -mtime +2 -name '*.dmp' -exec rm -f {}+


Also have a look to thread below:
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1218419


hope this helps!
kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 8 (125 Views)

Re: remove files according to date format

Hi:

It is important to understand that Unix does not maintain a "creation" timestamp for files.

Rather, there is a last modified ('mtime') that represents the last time a file's contents was updated. Thus, after the first instantiation of a file, its 'mtime' can be said to represent a creation moment, but any modifcation thereafter causes the knowledge of the first to be lost.

The 'stat()' structure contains an 'mtime' for the last modification; an 'atime' for the last access timestamp; and a 'ctime' for the last change in a file's inode (permissions, ownership, name).

Use 'find' to locate and remove your files. Use its '-xdev' argument to prevent 'find' from crossing mountpoints. This is very important if you are examining the '/' directory. Use '-type f' to limit your selection to FILES and not directories and files. The '-mtime +2' selects files that have been modified 2-DAYS ago or earlier. With 'find' the concept of a "day" is a 24-hour unit and granularily is to the second (60*60*24=86400).

# find /path -xdev -type f -mtime +2 -exec rm {} \+

If you want to limit the above selection to only files whose name ends in ".log", simply amend this to:

# find /path -xdev -type f -mtime +2 -name "*.log" -exec rm {} \+

Notice that we double quote the wildcard "*.log" so that 'find' evaluates the argument and your shell doesn't expand "*.log" and build a 'find' command with multiple '-name' arguments --- an error.

With the above pointers in mind, please read the 'find' manpages for more information.

Regards!

...JRF...
Please use plain text.
Honored Contributor
Rasheed Tamton
Posts: 965
Registered: ‎05-18-2002
Message 5 of 8 (125 Views)

Re: remove files according to date format

If you want to have files minus two days back specifically to overcome the -mtime, -atime -ctime issues, you can combine touch with -newer option of the find command.

> touch -t 04131200 file1 (Apr 13 1200)
> touch -t 04151200 file2 (Apr 15 1200)
> touch -t 04151200 file3 ( " )

> ll file*
-rw-rw-rw- 1 root sys 0 Apr 13 12:00 file1
-rw-rw-rw- 1 root sys 0 Apr 15 12:00 file2
-rw-rw-rw- 1 root sys 0 Apr 15 12:00 file3

> find . -type f -name "*il*" -newer file1

./file2
./file3

> find . -type f -name "*ile*" -newer file1 -exec ls -l {} \;
-rw-rw-rw- 1 root sys 0 Apr 15 12:00 ./file2
-rw-rw-rw- 1 root sys 0 Apr 15 12:00 ./file3

use *log instead of "*ile*" and rm instead of ls for removing. User -xdev for the FS.

Here I use rm -i (inter active for test)
> find . -type f -name "*ile*" -newer file1 -exec rm -i {} \>
./file2: ? (y/n) n
./file3: ? (y/n) n

Please use plain text.
Occasional Advisor
SPMK
Posts: 9
Registered: ‎04-12-2008
Message 6 of 8 (125 Views)

Re: remove files according to date format

thanks all for reply

I got that,

find . ! -newer /ref_file -exec rm {} \;

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,879
Registered: ‎03-06-2006
Message 7 of 8 (125 Views)

Re: remove files according to date format

If you want performance you should use "+":
find . ! -newer ref_file -exec rm {} +
Please use plain text.
Occasional Advisor
SPMK
Posts: 9
Registered: ‎04-12-2008
Message 8 of 8 (125 Views)

Re: remove files according to date format

v can use find . ! -newer ref_file -exec rm {} +

thanks.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation