LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2006, 02:29 AM   #1
shiroh_1982
LQ Newbie
 
Registered: May 2006
Posts: 9

Rep: Reputation: 0
Deleting old files


Hi,

I have a directory which contains files.This Directory keeps getting in new files from time to time.I want to maintain only 15 files in that directory at any time and the old files should be deleted.

Eg:

Directory 'c' @'a/b/c contains:
1_a
2_a
3_a...

I want to delete all the old files and maintain only 15 such files.Please note the title of the files is in this format "1_a"...

I know I have to use the command :
find /a/b/c -type f -name '*.a' -mtime +10 -exec rm {} \;

I just want toincorporate that 15 files limit in that directory.

Thanks.

Last edited by shiroh_1982; 06-21-2006 at 03:13 AM.
 
Old 06-21-2006, 08:19 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,261

Rep: Reputation: 297Reputation: 297Reputation: 297
Hi

Try a "ls -t |tail -n +16" - it should give all files in a directory except the 15 newest files. (ls -t means sort by time, the "tail -n +16" means skip the 15 first lines.

So a
rm $(ls -t |tail -n +16)
would do the trick I think.

Hope this helps,
Guttorm.

Last edited by Guttorm; 06-21-2006 at 08:22 AM.
 
Old 06-21-2006, 08:26 AM   #3
SlackDaemon
Member
 
Registered: Mar 2006
Distribution: RedHat, Slackware, Experimenting with FreeBSD
Posts: 222

Rep: Reputation: 30
Script to keep 15 files

Hi shiroh_1982,

The following script can be run from cron to control your target directory:

#!/bin/bash

(ls -1t /target/dir | sed 1,15d |xargs rm) 2> /dev/null


Replace '/target/dir' with the directory you want to maintain. This will keep the last 15 files modified in the directory and delete everything else.

Note that the ls command is using a one '1' numeral option and not a small L 'l' option.
Let me know if this solves your problem.

Best Regards
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting files in C monil Programming 4 03-13-2005 11:33 AM
Deleting files! a_priebe47 Slackware 6 10-17-2004 10:58 PM
Uninstall / deleting files Clemente Linux - Newbie 6 08-26-2003 01:00 PM
Deleting files ShakyJake Linux - Newbie 12 05-08-2003 04:07 PM
Deleting files Tingle Linux - Newbie 4 01-16-2003 05:19 PM


All times are GMT -5. The time now is 01:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration