How to delete files which are 7 days old

Hi all,
how to write a script that will indentify the files in a directory which are 7 days old and delete those files.
Thanks in advance
Cheers
Arunava

find /path/to/dir -ctime +7 -exec rm -f {} \;

Can u explain this command. please!

I use a slight variation on
find /path/to/dir -ctime +7 -exec rm -f {} \;

find /path/to/dir -mtime +7 -type f -exec rm -f {} \;

I recommend the "-type f" because this restricts to regular files.

One of my coworkers had coded -rf on the rm statement with out the -type f & ended up deleting directories.

We use this code to purge files from an application archive directory (logs, data, etc.)

ps - Do a "man find" to find out what the command does.

Hi Maven,
Thanks for the solution.but recently i am having a problem with deleting the files.if i go to the backup directory and just give the ls command to how many files are there and then run this command its not working .on the other hand if i don't go to the directory and run this command its working perfectly.
Urgently require help
cheers
Thanks in advance
Arunava

BEFORE I issue an "rm" command inside a find -exec, I always test it extensively to be sure it's working properly.

There are a few variations:

find /path/to/dir -mtime +7 -type f -exec ls -l {} \;
This will give a long listing of the files which satisfy the find criteria. i.e. the ones to be deleted.

You can also try:

find /path/to/dir -mtime +7 -type f -print

If you use "." in place of /path/to/dir - ALL directories below your starting point will be searched.

Typical:
cd /dir/to/purge
find . -mtime +7 -type f -exec ls -l {} \;

Good luck

What if you wanted to remove only files from today

I use
find . -type f -mtime 0

I seem to recall some issues with this - I needed to ID files which arrived via ftp & were processed today. Needed to copy them from the production archive directory to our Test environment - The above ended up picking up files which were within 24 hours old i.e. running at 8am would pick up files which arrived from yesterday after 8am. My solution was to delete "yesterday's" files before I ran my process....

I've used other variations to ID files - use ls -l and examine the file's timestamp

Well I have been able to get the files in the last 12 hour time frame by using the option mtime -1

I just wanna grab the newest file in the directory, regardless if it was in the past 5 mins or past 1 week in the directory