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


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

Omar Zabaneh

July 25, 2008
Zeth, Thank you for this post, very helpful. I used it as a basis for my own email validation function that i wish to share with you, in a selfish ...
Email Syntax Check in Python

Double Booting Bastard

July 24, 2008
I agree with Nui, Linux is great for many things but not everything. A lot of, less mainstream, hardware is a time consuming and often fruitless task to install and ...
Give Linux a chance

John

July 23, 2008
Duncan, sadly the permissions are stored with the data (inode), not with the directory entries (hard-links). Zeth needs ACLs -- no way to do this with basic unix permissions.
Advanced Unix Groups

Garrick

July 21, 2008
I do love my iPhone. That being said, I would trade it in a heartbeat for a STABLE Openmoko FreeRunner.
This week - iPhone vs a can of compressed air, and Django NewFormsAdmin

Daniel Davies

July 21, 2008
With regards to your last paragraph, you are certainly correct. Right now Django is a nightmare to use across multiple sites... we have some sites running the newformsadmin branch, others ...
This week - iPhone vs a can of compressed air, and Django NewFormsAdmin

Nui

July 18, 2008
Hmm, this would be more persuasive as an argument with some evidence. I am a happy admin of Windows and a novice user of Linux, so I have taken the ...
Give Linux a chance

Paddy3118

July 18, 2008
Hi, I too work with Electronic Design Automation tools, where Tcl is used extensively. I tend to only occasionally have to write in Tcl and so find the TclTutor utility: ...
Python and TCL

Cliff Wells

July 17, 2008
I personally cannot live without the Web Developer extension or Firebug. Unfortunately these are probably both among the more difficult to port extensions. Given how poorly Firefox functions on Linux ...
Will Epiphany be able to compete with Firefox's extensions?

Åke Forslund

July 13, 2008
I'm pretty much a novice in both of these languages but I find them both easy to use and preform the tasks I give them. However I rarely use them ...
Python and TCL

Christopher Thoday

July 12, 2008
A single test is not sufficient to give you confidence that the algorithm is working. You should make 'number' an argument of 'main' so that you can test some boundary ...
Python and TCL

paul21

July 10, 2008
Shame on Mozilla. They should make developers specify the extension license before hosting it. They should show the license next to download button as well.
Are your Firefox extensions proprietary software?

Tris

July 8, 2008
Justin - You say they had not heard of Linux? That doesn't sound very professional to me!
Give Linux a chance

michael

July 8, 2008
what about Galeon? in Gnome i use Galeon mostly. it is fast and stable and has a nice portal with search masks for Debian, FSF, Freshmeat and so on. wtf ...
Will Epiphany be able to compete with Firefox's extensions?

vermin

July 7, 2008
> Eventually, after a bit of digging and Googling, I found their Toolbar-License... You simply found the license of the StumbleUpon Toolbar for Internet Explorer. This is another product, much ...
Are your Firefox extensions proprietary software?

Andrew West

July 6, 2008
Both the Python and the Tcl example could do with error checking. While at first this may not seem on topic with the post I think it better shows the ...
Python and TCL