Five Tips for Easter

22 March 2008

Happy Easter everyone, I don't have enough chocolate eggs for you all, so instead, here are five tips.

1. Give your screens names

I often have processes running in a GNU screen for long periods. For example, I almost always have an IRC client running, perhaps a screen for Gentoo Linux's emerge command, and so on.

You can resume a screen using the process number, for example:

screen -r 18203

However, it is more fun to give the screens human readable names. To do this, use the -S argument when you create the screen:

screen -S chat

Then you can resume the screen using:

screen -r chat

2. Go on the rampage with xargs rm

Sometimes I have a lot of files that I need to sort in a hurry, without going into specifics, usually the issue for me is that after I have gotten out the information that I really need; I then want to have a quick look at the rest before throwing it away.

So at this point, we need to separate almost useless data from completely useless data. In this I am not too concerned about the individual files themselves, but I just want to separate the noise out and look at the rest, normally before chucking those away too.

If the aim is throwing away as much unwanted data as possible, I will make a backup of the files first (in case I type the command wrong), then use "xargs rm" to decimate the working copy. If you feed a list of filenames to "xargs rm" then it will delete them.

So for example, lets throw away every .txt file in the working directory and below that is newer than two days old:

find * -iname "*.txt" -mtime -2 -print | xargs rm

To take another example, the comments in this blog are stored as XML files. If I get spams on this blog, then they usually are predictable, one recent spammer always used the same string in the author field. This command will delete all his comments:

grep -l *.xml "<author>Spammer" | xargs rm

3. Generate temporary filenames uniquely

Temporary files that have predictable filenames could (in theory) be used in race condition attacks or denial of service attacks, it is better to let the operating system assign a unique filename to use as a temporary file.

The way to do this in scripts is to use the mktemp command, this generates a unique name which you can then use in scripts. mktemp started in OpenBSD and appeared for a while in Linux distributions as an individual package. It is now included in coreutils by default.

Run the following command as many times as you like:

mktemp /tmp/example.XXXXXXXXXX

Each time it will substitute the Xs with random letters followed by the process number. This gives a unique filename.

The number of Xs is up to you. The manual page tells us that "ten 'Xs' will result in mktemp testing roughly 26 ** 10 combinations", that is 141.2 trillion plus change.

For example, our imaginary script records a program output to a file.

Lets start by making an environment variable called TMPFILE. The marks below are not single quotes, they are back-ticks (left of the 1 key on a British keyboard). This assigns the output of mktemp to the variable TMPFILE.

TMPFILE=`mktemp /tmp/datewatch.XXXXXXXXXX`

Now we can pipe output to $TMPFILE:

date >> $TMPFILE

Now lets print the output to the screen:

cat $TMPFILE

4. Include an RSS feed as a static component

Django has not taken over the world yet, and sometimes sites do not need to be that complicated. Some websites just use server sides includes to put together static content, for example it might have a header and a footer and the central content.

If you want to display items from another site's RSS feed, then you could just create another include for the feed; and then have that include recreated using cron.

Download this python script that I created. Do not forget to change the URL variable to contain the URL of the feed you want to include.

5. View your wireless history

Sometimes you might want to look at all the wireless networks that you have connected to in the past. If your laptop's Linux distribution uses NetworkManager, for example Ubuntu, then you can use my wireless network history script.

Download this file and rename it so the file ends with .py. Now run the script.

You can do this like so:

python netwomb.py

Or you can make the script executable:

chmod +x netwomb.py

Then just use

./netwomb.py

You can also import it as a module and do far more with it. For example, start the python interpreter:

python

and type the following commands:

import netwomb

wifihistory = netwomb.Network()

You now have an object called wifihistory that contains all the information about your past wireless connections.

To see the networks:

print wifihistory

Now take one of the results, for example, say one is called BTOpenzone. We can see when we set up the connection:

print wifihistory.BTOpenzone.time()

The other information available will depend on the type of connection. Imagine this connection used a WPA Certificate, we could see what certificate we used by going:

print wifihistory.BTOpenzone.wpa_eap_ca_cert_file

Each network is a regular python dictionary. You can see all the information about that connection by going:

print wifihistory.BTOpenzone.__dict__

Please let me know if you do anything cool with it.

That's all folks. Happy Easter!

Discuss this post - Leave a comment

Digg entry

1 e says...

What's the use of naming your screen sessions? Why not put all the windows into one screen and name them in there (with ^a A)? Then if you only have one screen session, you don't need to supply an argument to screen -r. And having everything in the same one lets you use the paste buffer between them.

Posted at 5:20 p.m. on March 22, 2008


2 John Reese says...

Just as an FYI, Ubuntu Hardy includes a new tool called Network Manager Editor, which allows you to use a nice GUI to browse your past network,s change keys, delete entries, etc. It's actually quite nice and easy to use. Just my $0.02 (worthless these days...)

BTW, your text-box coloring isn't friendly to those of us with dark themes on FF3, and your human test isn't friendly to us Americans who are too lazy to know much about foreign politics and have to look it up on Wikipedia. ;)

Posted at 5:43 p.m. on March 22, 2008


3 Zeth says...

Oh dear, oh dear. If you don't know the difference between Mr Bean, Mr Brown and Mr Stalin; then I am afraid nothing I do is going to help much ;)

Posted at 6:04 p.m. on March 22, 2008


4 John Reese says...

Well, I obviously know who Stalin is, but my knowledge of UK politics is not good enough to where I can blatantly assume the last name of 'Bean' is only a joke... ;)

Posted at 6:40 p.m. on March 22, 2008


5 Dirk R. Gently says...

I've been using dtach lately instead of screen. Screen has a few key bindings that conflict with a couple of my programs. Using dtach is easy, create a dtach temp file and add the program:

dtach -n /tmp/irssi.dtach irssi

To attach to the program specify the dtach temp file:

dtach -a /tmp/irssi.dtach

and ctrl will release it.

Just thought I'd mention it ;)

Posted at 1:32 p.m. on March 24, 2008


6 Lingerance says...

find * -iname ".txt" -mtime -2 -print | xargs rm Is the same as find * -iname ".txt" -mtime -2 -delete There is usually never a real need to pipe find output to something else as find offers -exec as well.

Posted at 4:46 p.m. on March 24, 2008


7 Tom says...

find -exec is nice, but escaping can become complicated if you want to execute, say, awk using a weird pattern. My preferred way is not elegant at all but very robust and flexible:

find * -iname ".txt" | while read i; do echo rm "$i"; done

I run it once like that, and if I am sure it does what I meant I remove the echo. This also takes care of annoyances like spaces in filenames.

Another way of dealing with all sorts of strange characters is to tell find and xargs to use null bytes as characters for terminating:

find * -iname ".txt" -print0 | xargs -0 rm

Posted at 12:59 a.m. on June 17, 2008


8 John says...

Zeth,

The link to this file (for view wireless history) doesn't bring up a dialogue. Could you fix this?

Posted at 8:04 p.m. on August 20, 2008


What do you have to say?

Show Editing Help

About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

Zeth

November 29, 2009
Hi Jordan, yes that URL is gone now. I have a new contact form on this site.
Python CGI contact forms

Jordan

November 29, 2009
Zeth attention! Your form, http://zeth.me.uk/contact/, is not working The explorer says connecting ..but nothing happens Sorry for my poor English: I am Spanish Regards
Python CGI contact forms

Jordan

November 26, 2009
Sorry: tell me , not tellme (I'm spaniard) And http://zeth.me.uk/contact/ don't work
You got the touch, you got the power

David Jones

November 25, 2009
Your mad skillz are too l33t! for me. I specifically switched to Google Reader so that I could show people what blogs I read. But I couldn't work out how ...
How to find the fashionable blogs quickly

Brian R. Hickey

November 20, 2009
Symantec picked it up too.
How to bring down Internet Explorer with six words

Zeth

November 17, 2009
Thanks djm, I am the moose here. Christian, assuming one actually does Internationalise the countries, it should still work I guess, as the gettext stuff will happen before the list ...
Countries in Django

Phillip Temple

November 17, 2009
Good start, but: a) wouldn't I want None back rather than 'ZZ'? b) why not add a 'shortcut' boolean, then prepend flagged fields (plus usual '-----' separator) to the actual ...
Countries in Django

djm

November 17, 2009
Am I being a moose or did you mean: from whatever.countries import CountryField instead of from whatever.countries import CharField ? Good post though, cheers.
Countries in Django

Christian Joergensen

November 17, 2009
Wouldn't the ordering get messed up after i18n?
Countries in Django

Steve - Electronic Cigarettes Fan

November 17, 2009
Very well done. Is your blog just you writing? Nicely done, Steven.
Blogger vs Wordpress

vetetix

November 15, 2009
Sorry to bother you nearly two years after you wrote this blog article, but I can't manage to find how to modify an existing field. I am trying to change ...
Three Useful Python Bindings - ClamAV, Apt and Evolution

Manju

November 4, 2009
I am transferring some files using psftp to other device's FAT partition. But the filestamp of the file being transferred is modified to that of FAT device, after the transfer. ...
PuTTY Series: Using PSFTP

iki

November 2, 2009
or simpler: socket.gethostbyname_ex(socket.gethostname())[2]
How to find out your IP address in Python

iki

November 2, 2009
local_ip = set([ i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None) if i[0] == 2 ])
How to find out your IP address in Python

Fred

November 2, 2009
testing rst ------------- - point 1
An Introduction to ReStructuredText

Ano

October 27, 2009
"You simply found the license of the StumbleUpon Toolbar for Internet Explorer." That's possible. I've got some more interesting information to add. Firstly, go to this page: https://addons.mozilla.org/en-US/firefox/addon/138 - this ...
Are your Firefox extensions proprietary software?

Ken

October 21, 2009
Stumbled in here at lunch. This is the best find of the week. Thanks.
Three classic command line tips

Jim

October 19, 2009
Thanks for the rtsp:// post - that's something that has been bugging me for a while!
Three classic command line tips

Zeth

October 18, 2009
Thanks for the comments guys. Great to see the all the gang are still here!
Three classic command line tips

Bubba

October 18, 2009
Is there any way psftp can return the true transfer rates oberved during the actual transfer?
PuTTY Series: Using PSFTP