System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

single command to delete the different file by moth wise

HP UNIX Professionals
Frequent Advisor

single command to delete the different file by moth wise

Hi

Anybody know the single command to delete the different file by moth wise in HPUNIX.

For example i want to delete file created in January 2009
9 REPLIES
Johnson Punniyalingam
Honored Contributor

Re: single command to delete the different file by moth wise

Hi HP - UNIX Professional ,

>>Anybody know the single command to delete the different file by moth wise in HPUNIX.

For example i want to delete file created in January 2009<<

Yes , You can , But many ways to do.

find command, "man find"

Example:-

DESTDIR="/abcd"
find $DESTDIR -mtime +5 -exec ll {} \

Above command will find files create last five last and list

you can change ll (to) rm will delete , before you run command check with listing than proceed delete

HTH,

Johnson
Problems are common to all, but attitude makes the difference
Johnson Punniyalingam
Honored Contributor

Re: single command to delete the different file by moth wise

or you just do

cd /abc
ll |grep -i Jan

Once confirmed "you change the ll to rm"
to delete
Problems are common to all, but attitude makes the difference
Raj D.
Honored Contributor

Re: single command to delete the different file by moth wise



>>Anybody know the single command to delete the different file by moth wise in HPUNIX.

- What is moth wise ? not sure what you are looking for. pls provide more data.


-Why not use regular expressions:
Suppose you have some differen files say 5 files, aa1 aa2 aa3 aa4 aa5 and you want to delete 5 files , but not these aa1a aa6 aa7 etc.

List:
# ls -l aa[1-5]

Delete:
# rm aa[1-5]



Hth,
Raj.


" If u think u can , If u think u cannot , - You are always Right . "
Dennis Handly
Acclaimed Contributor

Re: single command to delete the different file by moth wise

>I want to delete file created in January 2009

You can't. Unix doesn't track creation, only modification. So if you want to delete files last modified in Jan, you can.

If you want to delete anything last modified in Jan or before, you can use the -mtime +N, where you have to count the number of days from today to Jan 31.
Or you can set up a reference file with touch(1) and use ! -newer.
Raj D.
Honored Contributor

Re: single command to delete the different file by moth wise

sorry , unable to understand the question in first shot in a hurry,

Well, you can use mtime function with find command to delete files based on file inode modification time:

You can find and delete files whose time stamps are within a certain age range, or compare them to a reference file time stamp as mentioned above,



ex:
-Files modified in the last 24 hours:

find /dir -mtime -1 -print


- Files modified in last 7 days: ( 7 *24 hrs)
# find /dir -mtime -7 -exec ls -l {} \;



-find files that were last modified 60 days ago under /dir, use
# find /dir -type f -mtime -60 -exec ls-l {} \;

- Delete files that were last modified 60 days ago, under /dir, use
# find /dir -type f -mtime -60 -exec rm {} \;




Also check out few link:
http://forums11.itrc.hp.com/service/forums/questionanswer.do?&threadId=485287




You can also use: -newer option:
http://docs.hp.com/en/B9106-90007/find.1.html

-newer[tv1[tv2]] file
True if the indicated time value (tv1) of the current file is newer than the indicated time value (tv2) of file. The time values tv1 and tv2 are each selected from the set of characters:


# find /dir -newer ref_file -exec ls -l {} \;
# it will print files more recent than the ref_file




Per your question:
>>For example i want to delete file created in January 2009

Current month is Oct09. so you may use (-240 days in mtime). However this will be true only if the files created in january , and has not been modified since then:
- to check:
# find /dir -type f -mtime -240 -exec ls -l {} \;

- To delete:
# find /dir -type f -mtime -240 -exec rm {} \;


Hth,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Arturo Galbiati
Esteemed Contributor

Re: single command to delete the different file by moth wise

Hello,
really strange question by an expert in this area!
It's possible to delete files per month based on the modified date but to get more help you should provide more detail about what you want really to do.
Rgds,
Art
Arturo Galbiati
Esteemed Contributor

Re: single command to delete the different file by moth wise

Hello,
please, since people spent time to provide you help, please, spent your time to assign points:
http://forums13.itrc.hp.com/service/forums/questionanswer.do?threadId=1387538

Rgds,
Art
mvpel
Trusted Contributor

Re: single command to delete the different file by moth wise

The Perl stat()/lstat() functions are capable of directly querying the ctime, atime, and mtime of a file, so you can use that to identify your target files.

For example,

$mtime = (lstat($file))[9]

You can derive the range of timestamps which constitute January 2009 using the timegm() or timelocal() functions found in the Time::Local module.

timelocal($sec,$min,$hour,$mday,$mon,$year);

So the first second in January 2009 in your local time zone is:

use Time::Local;
$jan09begin = timelocal(0,0,0,1,0,109);

And the last second in January 2009:

use Time::Local;
$jan09end = timelocal(59,59,23,31,0,109);

You can then compare these numbers with the time obtained from lstat() to determine whether a file's timestamp falls within that range.

Note that Jan..Dec = (0..11) and the year is based in 1900, the same as the gmtime() and localtime() functions.

I'm not sure if it'll croak on February 29 in a non-leap-year and the like, you'll have to try it out.

So probably the most UNIX-y way to do this would be to write the Perl script as a filter, so that you could do:

find . -type f -print | timein 200901 | xargs rm

And maybe command line arguments to specify mtime, atime, or ctime.
mvpel
Trusted Contributor

Re: single command to delete the different file by moth wise

If timelocal() does, in fact, choke on incorrect month-end dates, you could just subtract one second from the first second of the following month:

$feb09end = timelocal(0,0,0,1,2,109) - 1;

This also means you don't have to code in the whole "Thirty days hath September..." rhyme, and the every four years but not every 400 leap year rule.