[H-GEN] chestnut
Tony Nugent
tony at linuxworks.com.au
Wed Mar 26 20:08:22 EST 2003
[ Humbug *General* list - semi-serious discussions about Humbug and ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]
On Thu Mar 27 2003 at 10:13, Greg Black wrote:
> Sarah Hollings wrote:
>
> > Anyone know of a simple one-[or-two]-liner (in bash or perl -e) for
> > deleting the oldest file in a directory?
>
> This one will work in /bin/sh or any Bourne-style shell:
>
> rm `ls -tr | tail -1`
Two observations here...
- "-tr" will list them in the wrong order (tail will find the
youngest file not the oldest), so only "-t" is needed. Doh! :)
- what if the oldest listed item is not a file? You would need to
check for that, so it would need to be smarter, perhaps something
like this?
rm `/bin/ls -lt | grep ^- | tail -1 | cut -b57-`
If you wanted to include links, then:
rm `/bin/ls -lt | grep -E ^-\|^l | tail -1 | cut -b57-`
Warnings:
- the cut will prune off the leading "ls -l" output to give you
only the filename, but the "57" may need to be tuned to match the
local output of /bin/ls. There might be a better way to do this
that works on any standard "ls -l" format.
- if the filename contains spaces, then boom! I like bash, you can
glob spaces cleanly:
rm "$(/bin/ls -lt | grep -E ^-\|^l | tail -1 | cut -b57-)"
One issue to consider: "oldest file" -- by creation time, last
modify time, or last access date? Options to /bin/ls can be used to
fine-tune this.
> Greg
Cheers
Tony
--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'. See http://www.humbug.org.au/
More information about the General
mailing list