Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

unix delete the oldest file

Posted on 2008-06-09
5
Medium Priority
?
394 Views
Last Modified: 2013-12-26
In unix environment need csh or ksh script that will mangae directory of files.  Only need the latest 10 log files, thus need script that removes the oldest file.
0
Comment
Question by:sdruss
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 21748612

ls -t|sed 1,10d|xargs rm -f

Open in new window

0
 
LVL 16

Accepted Solution

by:
Hanno P.S. earned 200 total points
ID: 21749248
Display all file ending on ".log", ordered with oldest last, filter out
all but the last one:
# ls -t *.log | tail -1
Now, use the command from above and take it's output as the
argument for remove command
# rm -f `ls -t *.log | tail -1`
0
 
LVL 1

Author Comment

by:sdruss
ID: 21801644
Great good stuff.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 21807060
The answer accepted doesn't match your request.
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 21811261
Tintin,
as far as I can see, he wanted to get "the oldest" file deleted.

Your solution deletes all but the first ten files. Your solution is
much more enhanced and quite elegant -- but unfortunately
seems a bit overcrafted for the original Q.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question