Solved

unix delete the oldest file

Posted on 2008-06-09
5
381 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 Schröder earned 50 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 Schröder
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now