LinuxQuestions.org
Review your favorite Linux distribution.
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 12-15-2011, 08:18 AM   #1
alancampbell6
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, 08:52 AM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 126

Rep: Reputation: 28
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

2a.
Code:
ls -latr *.bat
2b. This will give a list of all bat files, most recent is listed last
2c.
Code:
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, 09:11 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

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

EDIT:

Never mind, an answer has already been given.

An alternative for the .bat files:
Code:
echo rm "$( /bin/ls -1t *.bat | head -n-1 )"
Notes:
  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 09:15 AM.
 
Old 12-15-2011, 09:17 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

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

Code:
man logrotate
 
  


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


All times are GMT -5. The time now is 11:48 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration