• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

unix delete the oldest file

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
sdruss
Asked:
sdruss
  • 2
  • 2
1 Solution
 
TintinCommented:

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

Open in new window

0
 
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
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
 
sdrussAuthor Commented:
Great good stuff.
0
 
TintinCommented:
The answer accepted doesn't match your request.
0
 
Hanno P.S.IT Consultant and Infrastructure ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now