Review your favorite Linux distribution.
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 12-15-2011, 09:18 AM   #1
LQ Newbie
Registered: Jul 2011
Posts: 16

Rep: Reputation: Disabled
Delete all .log files and .bat files except the most recent?

I have a directory with multiple .log files and multiple .bat files.

1. I want to delete all .log files.

2 I want to delete all .bat files except the most recently created one.

Any ideas on how i could do this?
Old 12-15-2011, 09:52 AM   #2
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 128

Rep: Reputation: 30
You haven't mentioned whether you need to do this on a repeated basis (script), so this assumes you don't. Also assumes you are in the directory containing the files.

1. rm *.log

ls -latr *.bat
2b. This will give a list of all bat files, most recent is listed last
for i in `find ./*.bat | grep -v "whatever bat file you want to keep"`; do rm $i; done
Or, you could just temporarily rename/move the one bat file you want to keep, delete the remaining ones, and move it back. Before I run something like 2c I usually put "echo" or "ls" in place of "rm" to see that it's giving me what I expect. Then, once absolutely sure it's right, run it with the "rm" (since once you hit Enter on the rm there's no going back).
Old 12-15-2011, 10:11 AM   #3
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian except for netbook still on Slackware 13.37 (and I have to support Ubuntu)
Posts: 8,572
Blog Entries: 30

Rep: Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182Reputation: 1182
Is this a homework question?


Never mind, an answer has already been given.

An alternative for the .bat files:
echo rm "$( /bin/ls -1t *.bat | head -n-1 )"
  1. That's a number 1 in ls -1t, not a letter l.
  2. When you are confident it is generating the correct rm command, remove the echo.

Last edited by catkin; 12-15-2011 at 10:15 AM.
Old 12-15-2011, 10:17 AM   #4
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Or there is always logrotate to manage log files

man logrotate


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
[SOLVED] How to find duplicate files and delete all except most recent version rjo98 Linux - Newbie 4 08-17-2010 11:13 AM
Best way to delete old unwanted log files?? xmdms Linux - Newbie 6 10-27-2008 10:36 AM
Delete Log Files raysr Mandriva 2 01-31-2006 12:15 PM
recent attack via phpBB, log files deleted? accessed file system outside /home/ enzo250gto Linux - Security 8 12-17-2004 02:51 PM
log what files users create, delete etc. Goma_2 Linux - General 3 09-26-2003 07:12 PM

All times are GMT -5. The time now is 06:59 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