LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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: 172

Rep: Reputation: 56
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: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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: 244Reputation: 244Reputation: 244
Or there is always logrotate to manage log files

Code:
man logrotate
 
  


Reply



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 anon091 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:36 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
Open Source Consulting | Domain Registration