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

Europython

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

gutes Qualitätscasino

July 3, 2009
The paragraph is the most basic block in a reST document. Paragraphs are simply chunks of text separated by one or more blank lines. As in Python, indentation is significant ...
An Introduction to ReStructuredText

sreejith

July 3, 2009
I want to download a file from remote server in binary format. Can anyone let me know the command to do so? Thanks in advance
PuTTY Series: Using PSFTP

jythlkedl;rg

July 2, 2009
????? ??? ????????? ?????? ?? ???????? ? ????????? ???, ? ??????? ??? ??????? ??????? ? ??? ?? ???? ?? ?? ????? ???????????????? ??????????????????. ??????????????????? ????? ?? ?? ????, ?, ???, ...
Burning an iso to CD on Windows

gbi-service-ru

July 1, 2009
???? ?????????, ?????????? ?? ?? ? ???"??? ??????, ?????????? ??? ?? ???? ? ????, ? ? ?????. ?? ??? ???? ???? ??? ???. ?? ?????????? ???? ?? ?. ???????? ?, ...
Burning an iso to CD on Windows

seo techniques

July 1, 2009
I would like to thank you for the inforamtion you have put on this article no matter.
Only the penitent man will pass - on captchas and cotton wool

Online Craps lernen

July 1, 2009
I would like to thank you for the making these clarifications in such a detailed manner to rebuilt the communication and enhancing the strategies of the organization which could be ...
Disclaimer: NO WARRANTY

ZK@Web Marketing Blog

July 1, 2009
Django is an amazing web framework; we built a lot of features in a very short period of time and Django [mostly] stayed out of our way. Last night as ...
Baby Steps with Django - Part 4 Django Applications and flow

overnight payday loans

July 1, 2009
I found commandline.org.uk very informative. The article is professionally written and I feel like the author knows the subject very well. commandline.org.uk keep it that way.
Only the penitent man will pass - on captchas and cotton wool

Drogo

June 30, 2009
Gotta agree with your sentiments about many modern games. The cost of a new game is prohibitive, especially for consoles (although I've noticed that PS2 games have crashed in price ...
Retro British Gaming - Part 3: Amstrad CPC Games

pppiohooddd

June 29, 2009
Free vadult video site! http://crech.us/ 1000 free video every day!
OpenSolaris, Gobuntu, and be careful who you kiss

Tesyimasystus

June 29, 2009
...Love this dude!!! http://www.esnips.com/doc/79c22395-7bd6-4299-92db-cf392e381698/kutiman---this-is-what-it-became Peace
5 Homebrew Python Games

Simon Tite

June 28, 2009
twitterfall is still there, I just tried it, and to me it beats Visible Tweets hands down. Problem with Visible Tweets: * Extremely **irritating** animations! (There are three available, but ...
Visualising your favourite keywords in Twitter

piffAltetle

June 28, 2009
??? ??? ???? ???????????? ??????,?????????? ???? ?????? ??????????? ???????,??????????? ????? mp3,??????? ??????????? ??????.
Encrypt your /home this Christmas: part three - moving your data to the encrypted partition

idhyougjdsyhfr

June 26, 2009
SMS Trap is something that never fails to help you get your partner off guard? Our software will make reading other people?s SMS as easy as ABC. Ready for some ...
Burning an iso to CD on Windows

Sozdanie-saitov-com

June 26, 2009
???? ???????? ? ?????????????????? ????????????? - ??? - ???? ?? ????? ?????. ???? ?? ??? ? ??????? ?????? ????????? ?????! ???, ?????23126 sozdanie-saitov.com@mail.ru
Burning an iso to CD on Windows

gameskillz

June 26, 2009
Killzone 2 - the best PS3 game yet?Still LittleBigPlanet for me, but Sony's new shooter is mightily impressive. What you think about my web? http://www.easyfaxlesspaydayloan.com/payday-loans-online.html
Email Syntax Check in Python

Anish

June 25, 2009
hey Moritz, Check this http://commandline.org.uk/python/my-merry-five-minutes-with-bazaar/
Setting up a bazaar server

gbi zavod 177

June 24, 2009
???? ?????????, ?????????? ?? ?? ? ???"??? ??????, ?????????? ??? ?? ???? ? ????, ? ? ?????. ?? ??? ???? ???? ??? ???. ?? ?????????? ???? ?? ?. ???????? ?, ...
Burning an iso to CD on Windows

vettone

June 24, 2009
??? ????? ????? ????,??????? ?? ???,????? ???? ???.????? ?? ??????????? ????,?????????? ?????? ???????? ?? ????.???? ????????: http://euro-football.ucoz.com ????? ???? ??????????.
Burning an iso to CD on Windows

tuegjhg78kjfhuey

June 23, 2009
? ???????????????? ???? ??? ???, ?? ?????? ?? ?????????, ???, ???????????????? ??? ??????????, ???? ????? ??? ??? http://remont.ucoz.ua/
Burning an iso to CD on Windows