Results 1 to 4 of 4
Hi
I want to find the file with *.core extension in a directory.
If the Number of such files are > 50 then i want to delete all thos *.core ...
- 01-13-2009 #1Just Joined!
- Join Date
- Jul 2008
- Posts
- 22
delete oldest file
Hi
I want to find the file with *.core extension in a directory.
If the Number of such files are > 50 then i want to delete all thos *.core files whose modification time is oldest.
I don't want to use ls -t command because if the number of file are large say 3000 the it possible that this command gets hang.
I prefably want to use find
Thanks
- 01-13-2009 #2Linux Enthusiast
- Join Date
- Aug 2006
- Location
- Portsmouth, UK
- Posts
- 539
Not sure if you can use find to find the oldest file...
You could still use ls with using the '*' which will expand and may cause you problems as you've mentioned.
As a work around you could try something like
ls -t | grep "\.core$" | tail -n1
which would give you the file name of the oldest .core file.RHCE #100-015-395
Please don't PM me with questions as no reply may offend, that's what the forums are for.
- 01-13-2009 #3Linux User
- Join Date
- Jun 2007
- Posts
- 318
I'm sure there's probably a more eloquent way of doing this but this works.
What it does is:
List the date of each file found in the format yyyymmddhhmmss
Sort date in decreasing order
Extract the filename starting with the 51st file
Pass it to xargs to execute the rm command
Use at your own risk.
Before using it with 'xargs rm -fv' use 'xargs ls -l' to make sure it's selecting the correct files to delete.Code:find . -maxdepth 1 -type f -name "*.core" -exec ls -l --time-style=+%Y%m%d%H%M%S {} \; | \ sort -k6,6nr | awk 'NR>50 {print $7}' | xargs rm -fv
- 01-20-2009 #4
find is the right way to do...
use "find" command..


Reply With Quote