delete files that are over 2 hours old

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers delete files that are over 2 hours old
# 1  
Old 06-30-2002
delete files that are over 2 hours old


I have a need for a script that will delete all files in a given directory that are over 2 hours old. I will set this up to run in cron. I'm having a little trouble coming up with the syntax that will identify these files. Is there a variation of the ls command that I can use to identify these files? Any help will be appreciated.

# 2  
Old 06-30-2002
Use the "date" command to get the current date and time. Then subtract 2 from the hours. This may require adjusting the date if you are close to midnight. Once you have the date and time of 2 hours ago, use the "touch" command to create a temp file with that mtime. Next, use the "find" command is get a list of files older than the temp file and delete them. Finally, remove that temp file.
# 3  
Old 06-30-2002
mmm tough one... I know how to do this for files that were modified 'n' number of days ago...but I'm not sure about hours....

find . -mtime +5 -exec rm {} \;

This will remove files over 5 days old..... but the sytax for mtime states days only.... i tried decimals..but no joy.... anyone know if mtime or an equivalent can be used for hours?
# 4  
Old 07-01-2002
I know the date command when formatted with give you the time.


phdb102 [ogd] >date +%H:%M

This is the using variables in a shell script:


the_hour=`date +"%H"`
the_min=`date +"%M"`
calc_time=`expr $the_hour - 2`
echo "The current time is $the_hour:$the_min"
echo "This is the calculated time $calc_time:$the_min"

The result:

phdb102 [ogd] >
The current time is 12:04
This is the calculated time 10:04

Knowing this I am not sure how to incorporate this into the find command. I will experiment and let you know what I come up with.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting Files Older than 1 hours.

How to Deleting Files Older than 1 hours. Base on SunOS. this file gen every 1 min. -rw-r--r-- 1 nobody nobody 4960 Jan 27 02:02 23_201301270201.log -rw-r--r-- 1 nobody amudu 2325 Jan 27 02:03 33_201301270202.log -rw-r--r-- 1 nobody amudu 3255 Jan 27 02:03... (2 Replies)
Discussion started by: ooilinlove
2 Replies

2. Shell Programming and Scripting

ls files newer than 6 hours

How do I list al files in a folder with a creation date/time newer than 6 hours? (2 Replies)
Discussion started by: locoroco
2 Replies

3. Shell Programming and Scripting

files older than few hours

Hi All I need to know the command which can be used to list the files which are 3 hours old so that it can be deleted. (3 Replies)
Discussion started by: mskalyani9
3 Replies

4. Shell Programming and Scripting

how to list files between last 6 hours to 3 hours

Hi Frens, I want to list some files from a directory, which contains "DONE" in their name, i am receiving files every minute. In this i want to list all the files which are newer than 6 hours but older than 3 hours, of current time i dont want my list to contain the latest files which are ... (4 Replies)
Discussion started by: Prat007
4 Replies

5. Shell Programming and Scripting

How to find files by hours old?

I need to be able to do the following: Find files in multiple directories that are 6 hours older than the current time? I am using KSH I tried mmtime but it was not a valid option Any help would be great. Thank you! (2 Replies)
Discussion started by: llsmr777
2 Replies

6. UNIX for Dummies Questions & Answers

Delete logs every 3 hours

Hi, I want to setup a cronjob that will delete logs every 2 hours. I have script that delete logs per day. but logging is too big and i want to run a conjob that will delete every 2 hours. this is my current command but it deletes on a per day basis. find . -name "*.log*" -o -name... (3 Replies)
Discussion started by: tungaw2004
3 Replies

7. Shell Programming and Scripting

Files created in last 24 hours

I need a script which list the files which is starting with the word heap*** and that is created before past 24 hours.I need the script using find command. please help me on this. (1 Reply)
Discussion started by: jayaramanit
1 Replies

8. Shell Programming and Scripting

removing files after 6 hours or older

What is the command to remove files that are generated 6 hours or older? The find and remove tells only how to remove if the file is one day old or more. Appreciate quick reply. Thanks (3 Replies)
Discussion started by: gthokala
3 Replies

9. Shell Programming and Scripting

move log files over 12 hours old...

Hi, I know I can use touch and find's "! -newer" option to list files that are older than a specific time, but what is a good way to get a list of files that are over 12 hours old? The log pruner will run throughout the day, twice an hour. So I can't easily use a cronjob touch command to generate... (1 Reply)
Discussion started by: Thomas Pluck
1 Replies

10. Shell Programming and Scripting

Finding files older than 2 hours

I want to write a sh script that will find files older than 2 hours and tar them. I've had a look at the find man page but can't see how to do it by hours. Help please. Thanx (1 Reply)
Discussion started by: ianf
1 Replies
Login or Register to Ask a Question