Operating System - HP-UX
1830194 Members
9248 Online
109999 Solutions
New Discussion

Command to remove files based on specfic dates?

 
Gulam Mohiuddin
Regular Advisor

Command to remove files based on specfic dates?

I need to delete files & sub-directories based on specific dates.

Example: rm all files/dir older than 2005/10/26 in /u20 mount point.

What command would do this?

Thanks,

Gulam.
Everyday Learning.
8 REPLIES 8
Pete Randall
Outstanding Contributor

Re: Command to remove files based on specfic dates?

You need to use a combination of the touch command and the find command. First use touch to create a reference file with the desired date (see "man touch"). Then use find to search out all files older than that reference file:

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

-or-

find /start_dir ! -newer /path/ref_file | xargs rm

You should probably test with an ll in place of the rm first.


Pete

Pete
paolo barila
Valued Contributor

Re: Command to remove files based on specfic dates?

jamesps
Regular Advisor

Re: Command to remove files based on specfic dates?

Check the "find" command man page. You would simply have to find them and remove them based on your requirement, something like:

#rm -i /u20 `find....`

hth,
james
Arturo Galbiati
Esteemed Contributor

Re: Command to remove files based on specfic dates?

Hi Gulam,

touch -t 0510260000 tt
rm $(find /u20 ! -newer tt)

This will remove all file in /u20 older than the file tt (including tt itself).

touch format:
touch -t yymmdd0000

if you want to check files to remove before delete them use rm -i instead.

HTH,
Art
Muthukumar_5
Honored Contributor

Re: Command to remove files based on specfic dates?

Try as,

touch -t 20051026 /file

find /u20 ! -newer /file -exec rm -rfi {} \;

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Command to remove files based on specfic dates?

Hello,

# touch -t 0602101235 sample
# find /directory -newer sample-exec ls -l {} \;

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Yogeeraj_1
Honored Contributor

Re: Command to remove files based on specfic dates?

hi gulam,

this is an adaptation to one of my scheduled purging script.

Steps:

1. Create a reference file corresponding to your reference date:
touch -t 0602160000 /tmp/referencefile

2. Verify that the list of files to be deleted is OK.
find /u20 -type f ! -newer /tmp/referencefile | xargs ls -l > /tmp/res.log

3. If list of files to be deleted is OK, then delete the files using:
find /u20 -type f ! -newer /tmp/referencefile | xargs rm


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)
Muthukumar_5
Honored Contributor

Re: Command to remove files based on specfic dates?

Use yogee method and also Include -type d with -type f in his reply.

--
Muthu
Easy to suggest when don't know about the problem!