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

Essex Web Design

September 3, 2010
A lot of contract providers give you free internet usage now, but if you have Pay As You Go, then you are going to be paying heavy prices.
Calling time on mobile internet nonsense?

Krasochka

September 2, 2010
Hack again?!
Adding more terminals to your function keys

GenryFlorist

September 2, 2010
<b>Cheap flowers delivery around the world!</b> Celebrate summer with our gorgeous flowers. They?re the perfect gift for any summer occasion. From birthdays to anniversaries, we offer beautiful flowers, lush plants, ...
Burning an iso to CD on Windows

auto-financing.co.cc

September 2, 2010
auto-financing
ReStructuredText tables and doctests

rubaxa

September 1, 2010
FTP = NOT RANDOM software Dominated hands postflop suckout often on all-ins. EX. AK vs. A9 or KQ vs. K6. Both players hit top pair. Bad player goes all in ...
Burning an iso to CD on Windows

empodayaddelm

September 1, 2010
Sorry admin - my post is test
This Week: Heroes and Monsters

increase synthroid dosage

September 1, 2010
Latest world news: 1 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/viagra-cialis-buy-no>buy cheap cialis generic levitra viagra</a> Viagra 2 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/buy-viagra-online-at-lowest>rainbowpush discussion board buy viagra</a> Viagra 3 <a target="_blank" class="ext" href=http://www.maktabti.org/profiles/blogs/how-to-get-generic-brand>search viagra ...
SFTP in Python: Paramiko

Lacilslaw

September 1, 2010
HYUN JAIMIE enniless and homele JAMILA
This Week: Heroes and Monsters

domaserisk

August 31, 2010
who was shaking his head back and forth knowingly Grissom shifted his eyes over at Brass,
How I Removed Windows from my Laptop

get ready loan

August 30, 2010
Though, by the you kill the legitimate PC user from visiting the site. Also, think about the dynamic IP's issue.
Only the penitent man will pass - on captchas and cotton wool

Packers and movers in pune

August 30, 2010
The topic you disscussed here is very amazing, informative and useful in future...
On Comment Spam

serhanters1

August 30, 2010
?? ???????? ??... ??????...... ??. ????????? ??? ??? ???????D ???????? ??. ????? ????? ???? ??? ???=) ?? ?????- http://letitbit.net/download/8746.894a84bc20f38f1661895aeee0/stereokartinki.html ???http://f-zona.ru ? ? ?? ????????????
Burning an iso to CD on Windows

strona startowa

August 29, 2010
Thanks For This Post, was added to my bookmarks.
Python CGI contact forms

lerexottori

August 29, 2010
?????????????? ??????????
Adding more terminals to your function keys

KelpAugmeme

August 29, 2010
aofaapsymp, http://forums.quark.com/members/jennaq.aspx online stock trading broker, rdgofzary
PuTTY Series: Adding PuTTY to your system path

Cheeday

August 28, 2010
What flowers do you like?
This Week: Heroes and Monsters

magfcvb

August 28, 2010
??????? ?????????????? ?????? - ????? ?????? ?????????????? ??????, ?????????????? ?????? ???????, ?????????????? ?????? crosman, ???? ??????????????? ??????, ?????????????? ?????? ?????? ????????. ???? magazin-oruzhie.ru
Include ODF support in the Linux Standard Base?

noni

August 28, 2010
I find myself coming to your blog more and more often to the point where my visits are almost daily now!
On Comment Spam

Latenadsfes

August 28, 2010
http://mynewblog.for-breastcancer.com/ http://mynewblog.photoblogcentral.com/ http://ilovezebras.thechicks.org/ http://mynewblog.cyberbardsymposium.com/ http://wewphost.com/ilovezebras/
Burning an iso to CD on Windows

LeupoldEst

August 28, 2010
pretty cool stuff here thank you!!!!!!!
OOXML Vote Coverage