Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

Rep: Reputation: 0
Deleting old files


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.


Directory 'c' @'a/b/c contains:

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.


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

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

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,

Last edited by Guttorm; 06-21-2006 at 07:22 AM.
Old 06-21-2006, 07:26 AM   #3
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:


(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


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 10:33 AM
Deleting files! a_priebe47 Slackware 6 10-17-2004 09:58 PM
Uninstall / deleting files Clemente Linux - Newbie 6 08-26-2003 12:00 PM
Deleting files ShakyJake Linux - Newbie 12 05-08-2003 03:07 PM
Deleting files Tingle Linux - Newbie 4 01-16-2003 04:19 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:42 PM.

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