Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare dates 1

Status
Not open for further replies.
Mar 31, 2004
151
US
Hi is there a quick way to compare dates? I wanted to delete all files less than a day-before-yesterday
Any quick suggestions?

Code:
ls -l |  awk  ' NR>1 {print $6, $7, $9}' | while read Month Day FileName
do
  if [ "$Month" = $MON -o "$Day" -le $DeleteDate ]; then
   rm $FileName
  fi
done
 
watergrinder,

Your best bet would be to use the find command.

man find

I'm not sure exactly what you are looking for, but to find all files that were modified more than 1 day ago, try:

find -type f -mtime +1

Then if your results are OK, find -type f -mtime +1 | xargs rm

To find files that were modified today, find -type f -mtime -1.

You can also use the touch command with the -t option to create a file with a date and time stamp and use it to compare the date to see if the file should be deleted.

find . \( -newer oldest -a \! -newer newest \) -exec rm {} \;

Hope this helps.

John
 
man find (mtime and exec primaries)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Sorry, my find command above did not include a path name.

The correct syntax is:

find /path/to/search -type f -mtime +1

John
 
find . -type f -mtime +1 also checks for files in sub-directories. And | xargs rm will delete files in subfolders! Is there a way to make it check only the current directory? Thanks again for the suggestions.
 
Again man find (prune primary, and perhaps level depending of your *nix flavour)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If you find doesn't have depth, you can get it to stop recursing using:
Code:
find . \( -type d ! -name . -prune \) -o \( -type f -mtime +1 -print \) | xargs -p rm
Or something similar. Once you are confident this is working, remove the -p option from xargs part to stop the user prompts...
 
Thanks. I created a new file in the directory. But

find . \( -type d ! -name . -prune \) -o \( -type f -mtime -1 -print \)

displays that too. I think with mtime -1 option it should not.
 
Do you want to find files older (+1), or newer (-1) than a day ago? Cheers, Neil
 
Toolkit, one last question. This command seems to be working but not selecting all the old files. It is selecting some of them only.

Code:
-rw-r-----   1 oracle     oinstall   163578880 May 18 10:22 arch_0000038348.arc
-rw-r-----   1 oracle     oinstall   163578880 May 18 14:19 arch_0000038349.arc
-rw-r-----   1 oracle     oinstall   163578880 May 18 14:45 arch_0000038350.arc
-rw-r-----   1 oracle     oinstall   163578880 May 18 15:18 arch_0000038351.arc
-rw-r-----   1 oracle     oinstall   163578880 May 18 19:57 arch_0000038352.arc
-rw-r-----   1 oracle     oinstall   163578880 May 18 20:29 arch_0000038353.arc
-rw-r-----   1 oracle     oinstall   163578880 May 19 08:58 arch_0000038354.arc
-rw-r-----   1 oracle     oinstall   163578880 May 19 10:07 arch_0000038355.arc
-rw-r-----   1 oracle     oinstall   163578880 May 19 13:11 arch_0000038356.arc
-rw-r-----   1 oracle     oinstall   163578880 May 19 13:42 arch_0000038357.arc
-rw-r-----   1 oracle     oinstall   163578880 May 19 14:39 arch_0000038358.arc
-rw-r-----   1 oracle     oinstall   163578880 May 19 16:11 arch_0000038359.arc
-rw-r-----   1 oracle     oinstall   163578880 May 19 16:32 arch_0000038360.arc
-rw-r-----   1 oracle     oinstall   53182464 May 19 17:00 arch_0000038361.arc
-rw-r-----   1 oracle     oinstall   10446848 May 19 20:01 arch_0000038362.arc
-rw-r-----   1 oracle     oinstall   9449472 May 19 21:50 arch_0000038363.arc
-rw-r-----   1 oracle     oinstall   163578880 May 20 09:09 arch_0000038364.arc
-rw-r-----   1 oracle     oinstall   163578880 May 20 13:02 arch_0000038365.arc
-rw-r-----   1 oracle     oinstall   163578880 May 20 13:30 arch_0000038366.arc
-rw-r-----   1 oracle     oinstall   163578880 May 20 15:16 arch_0000038367.arc
-rw-r-----   1 oracle     oinstall   163578880 May 20 16:17 arch_0000038368.arc
-rw-r-----   1 oracle     oinstall   77202432 May 20 20:02 arch_0000038369.arc
-rw-r-----   1 oracle     oinstall   40960000 May 20 22:02 arch_0000038370.arc
fhnjdms:/u06/oradata/fhsop/arch/primary>find . \( -type d ! -name . -prune \) -o \( -type f -mtime +1 -print \)
./arch_0000038348.arc
./arch_0000038349.arc
./arch_0000038350.arc
./arch_0000038351.arc
./arch_0000038352.arc
./arch_0000038353.arc
./arch_0000038354.arc
./arch_0000038355.arc
 
Yep. This is because -mtime +1 acts as 'files older than 24 hours from when the command was invoked' This means that if you run the command at 2:30pm today, then files older than 2:30pm yesterday will be deleted.

To remove all files older than midnight of yesterday, you would need to create a 'timestamp' file, and then compare against this.
Code:
touch -t 200405210000 timestamp
find . \( -type d ! -name . -prune \) -o \( -type f ! -newer timestamp ! -name timestamp -print \) | xargs -p rm
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top