[SOLVED] Using bash script to purge rotating tcpdump files
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to figure out a way of writing a script that would keep the last three versions of tcpdump files.
Due to the version of tcpdump I must use -C and cannot use -G.
Using -C generates a new file after X MB's have been written and adds a .x after each new one.
The problem is that these files are filling up the disk too quickly. The main part of the script will kill tcpdump when a certain condition is met but in the meantime I need to purge and only keep say the three last iterations of the dump file.
So for example, there is dump.pcap.1, dump.pcap.2, dump.pcap.3, dump.pcap.4 and dump.pcap.5. I'd like the script to look at the datestamps and delete dump.pcap.1 and dump.pcap2 since the other three are the three newest files.
Can anyone think of a way of comparing files based on dump.pcap.*, check the dates and only keep the three 'youngest' files?
I think I've described what I'm after, but if I'm unclear - please post follow-up queries.
Here's a little chunk of code I use to clean all but the single newest logfile out of a certain directory. I accumulate a new logfile for every time I start my window manager, and deleting old ones manually was horrible.
You'll want to change the search path (/home/sasha/.i3) to whatever directory you have your files in.
You'll want to change the -name "argument" to match your filenames, maybe "dump\.pcap\..*" would work.
And, since you wish to save the last 3 files instead of only one, change the -1 in the `head` command, to a -3.
Code:
#!/bin/sh
case $1 in
demo)
find /home/sasha/.i3 -maxdepth 1 -name "logfile-*" -printf "%T+ %f\n" | sort | head -n -1 | awk '{print $NF}' | xargs -I {} echo rm -f {}
;;
real)
find /home/sasha/.i3 -maxdepth 1 -name "logfile-*" -printf "%T+ %f\n" | sort | head -n -1 | awk '{print $NF}' | xargs -I {} rm -vf {}
;;
*)
echo "This script clears away all but the newest i3 logfile in /home/sasha/.i3"
echo "It takes one of two arguments: 'demo' or 'real'"
echo "demo: shows what files will be deleted, but don't really delete them."
echo "real: actually deletes the files."
;;
esac
So, when you run it, give it either the "demo" option, or the "real" option.
If you have problems, post the code as you have adjusted it to suit your needs, and tell us what's not working.
Good luck!
Last edited by GrapefruiTgirl; 10-20-2010 at 10:58 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.