Quickie

Superkill - A Quickie Script for Killing All Instances of an Application

Evolution crashed on me this morning and I kept getting an error message: "Evolution appears to have exited unexpectedly...". Every time I started the application, it provided that message then locked so I went to my trusty command line. I did a ps -ef | grep evo and saw that several evolution-based processes were running:
steven    7885     1  0 09:57 ?        00:00:08 evolution --component=mail
steven    7894     1  0 09:57 ?        00:00:00 /usr/lib/evolution/evolution-data-server-2.22
steven    7918     1  0 09:57 ?        00:00:00 /usr/lib/evolution/2.22/evolution-exchange-storage
steven    7931     1  0 09:57 ?        00:00:00 /usr/lib/evolution/2.22/evolution-alarm-notify 
steven   11346  7715  0 11:12 pts/1    00:00:00 grep evo
I then proceeded to kill each process manually. It's only 4 but it was enough to bring out my inner programmer and solve a repetitive task programatically. So I decided to add a couple more pipes. I looked at the results from the grep'd ps and noticed that all process IDs are in character slots 10-14 so I cut -c 10-14 the processes that were piped in. That printed out as expected so I fed that to kill via xargs and, voila!, all those processes were killed. Not content to have to type all the pipes every time I wanted to do it, I threw the pipe in a BASH script that I lovingly call 'superkill' and take the first argument as the string to grep and kill the returned processes. The code is below:
#!/bin/bash

prog=$1

#echo ${prog}
ps -ef | grep ${prog} | cut -c 10-14 | xargs kill
Simple enough but quite powerful. I then threw the script in my /usr/bin directory so my PATH would pick it up. Now I can just superkill evolution (sounds like a creationist book, I know) and have all the processes ended. Here is the file. Just extract it (tar xzvf superkill.tar.gz) and put the resulting file in your /usr/bin directory. Then superkill away! Of course this code comes with no warranty and you assume all risks. Before you run the script you may want to call a ps -ef | grep <processname> before doing superkill <processname> so you make sure there are no unexpected results. I could echo the resulting process and ask for confirmation, I suppose. Hold for that version (depends if I get feedback it works well enough for my purposes).

Quickie: Install Packages from FireFox Address Bar

If you're surfing around in FireFox and don't have a terminal fired up, why waste the extra keystrokes or mouse movements just to install a package? If you already know the package name, you can install from your browser simply by using:

apt:<package-name>

For example I want to install the super excellent diagramming software dia, I simply type:

apt:dia

And a GUI pops up much like it does when you select a package and hit apply in Synaptic.

Quickie: Deleting Old Files

Many people have the need to delete files older than a certain range. For instance, you may want to remove all podcasts that are older than a certain time period or delete other files that you receive regularly. Making use of the find command and mtime, you are able to delete them with ease.

find . -name *.ogg -mtime +7 -exec rm {} \;

The above one-liner removes all my ogg-based media files in my current directory that are more than a week old. It is best to use absolute paths -- ESPECIALLY IF RUNNING BY CRON. So the cron-safe code looks like this:

find /home/steven/music/ -name *.ogg -mtime +7 -exec rm {} \;

You can also use this with other command such as moving the file, batch renaming them (if you want all older files to have a .bak extension or whatnot), as well as many other uses. To move the files, simply:

find /home/steven/feeds/ -name *.csv -mtime +30 -exec mv {} /home/steven/feeds/old \;

With enough MAN pages and will, you can do all kinds of cool stuff.

Quickie: Access Remote GUI Programs Using SSH Forwarding

This is a cool tip from Linux Journal

Basically you do an ssh session, throw in the C and X switches and specify what program to execute. Something like:

ssh -C -X user@coderswasteland.com gnome-terminal

Sweet, huh?

Quickie: Data Mining on the Web with PERL

This is a simple web mining script using perl.

#!/usr/bin/perl

use LWP::Simple;

$numPages=$ARGV[0];

open OUTPUT,">/home/user/out.html";
for($i=1;$i<=$numPages;$i++){
	print $i."\n";
        $content=get("http://coderswasteland.com/node/".$i);
        print OUTPUT $content;
        print OUTPUT "******************\n";
}
close OUTPUT;

This code is useful to pull information from any Drupal website. It takes the number of pages to crawl as a command line argument and uses that to increment through the site, grabbing articles. The content is then all saved to a single output file, each site separated by a series of asterisks from which you may do whatever regex or parsing is necessary to achieve your desired result.

I have a much longer version of this which does parsing and builds feeds. You may also wish to forgo writing to an output file then later reading from it and just process the information from within $content, split it into logical pieces, etc. This script is merely to get you started. If you're looking into web mining, I assume you already know about parsing and other topics, and this is just a quick way to grab web content.

A spiffier version would be to follow links on a page and popping info onto a tree rather auto-incrementing a URL. this is left as an exercise for the reader :)

Quickie: Batch Renaming Files in Linux

For those wanting to be able to quickly and efficiently rename a bunch of files all at once, below is some example code to help you achieve that:

ls *.xml | sed 's/\(testing\)\(.*\)/mv \1\2 production\2/' | sh

The first piece ls *.xml simply lists the file types we want to change. You can, of course, alter this to anything you'd like.

The second part uses sed so search for a text and replace it with another. There are numerous regex tutorials around the net.
Within the second part, items are grouped so that I may reuse these items in the next part. Specifically, I am looking for (part1) the word "testing" and (part2) anything else. The mv command then takes as the first parameter the original two values to denote what file is to be changed and the second parameter of the command does not use the first variable but rather the word "production" and then appends whatever was in the second variable.

Part 3 simply executes this.

The end result makes a sample file named:
testing_1.xml
be
production_1.xml

Quickie: Burning Video DVDs Command Line Linux

Make sure you have dvdbackup and growisofs/mkisofs. (Your usual sudo apt-get install in Debian/Ubuntu).

Simply:

dvdbackup -i /dev/dvd -M -o </your/output/path>

growisofs -speed 1 -dvd-compat -Z /dev/dvdrw -dvd-video </your/output/path/videodir>

Example:

dvdbackup -i /dev/dvd -M -o /home/steven/Documents/VID/

growisofs -speed 1 -dvd-compat -Z /dev/dvdrw -dvd-video /home/steven/Documents/VID/MY_BACKUP_VIDEO/

The second path is where the first command wrote to. Basically it will be one folder deeper than that first path. It is the path that contains the VIDEO_TS directory.

Referenced from:
http://ubuntuforums.org/showthread.php?t=133642

Quickie: Multiple File Search and Replace

If you've ever run into the necessity to change the same data over multiple files you know how hard it is to go through each and every file and update it by hand or at best do an indiviual regex on each. Here's how you can make the changes all in on fell swoop.

perl -pi -w -e 's/searchregex/replaceregex/g;' *.fileextension

-w display any warnings
-i in place edit
-p loop over files
-e execute this line of code

Here's an example:

perl -pi -w -e 's/\x0D//g;' *.txt

This looks for those pesky ^M characters and replaces them with nothing.

Quickie: Strip ^M (Control-M) Characters from Input File with PERL

For anyone that does file I/O and has to sometimes work with Windows-generated files in Linux, I feel your pain. Windows has these little nuances that sometimes makes our GNU/Linux world a fun place to live. Luckily, PERL has a simple little system in place that allows us to remove control characters - Regular Expressions. Those not familiar will find great references at http://www.regular-expressions.info/ (a fantastic place to begin) and http://www.regextester.com/ (where you can test your brilliant work).

People who just want a quick piece of code, look no further:

If you want to do it all in one line from the CLI (of course replace *.txt with whatever extension):

perl -pi -w -e 's/\x0D//g;' *.txt

If you'd rather do it inline in a Perl script:

#Good Code
$yourLine =~ s/\x0D//g; #strips ^M characters

Simply trying to strip ^M characters with

#Bad Code
$yourLine =~ s/\^M//g; #strips ^M characters

unfortunately does not work. The previous hex value works great for me. I've run into this problem many times while taking third party data feeds which are sometimes generated in Windows and trying to preprocess them in my GNU/Linux environment. The ^M sends gets interpreted as a new line and wreaks havoc on feeds where you expect all the information to be on one line in a fixed number of columns.

For more information on control characters, please go to: http://www.cs.tut.fi/~jkorpela/chars.html where Jukka Korpela explains in-depth what control characters are, what issues you may have, and how you may go about resolving them.

Latest Video


only search Coder's Wasteland
Powered by Drupal, an open source content management system

Digg this