IT Engineer, coder, dreamer.


Index


Bind a key to a command in Bash

You can bind, for example, the command ls -l to CTRL+l using:

bind -x '"\C-l":"ls -l\n"'

It may be useful to append this line in your .bashrc.

Now, every time you press CTRL+l, even in the middle of another command, a directory listing will appear! :)

Run the last command as root

$ sudo !!

We all know what the sudo command does - it runs the command as another user.

If no other user is specified, it runs the command given as argument as superuser.

But what's really interesting is the bang-bang !! part of the command. It's called the event designator. An event designator references a command in shell's history. In this case the event designator references the previous command. Writing !! is the same as writing !-1. The -1 refers to the last command. You can generalize it, and write !-n to refer to the n-th previous command. To view all your previous commands, type history.

Serve the current directory at http://localhost:<PORT>/

$ python -m SimpleHTTPServer <PORT>

Replace <PORT> with the desiderd port. If none specified, the default is 8000.

One of the many reasons *nix is wonderful: you can have everything, as simple as it gets.

This one-liner starts a web server on a desired port with the contents of current directory on all the interfaces (address 0.0.0.0), not just localhost. If you have "index.html" or "index.htm" files, it will serve those, otherwise it will list the contents of the currently working directory.

It works using Python standard module SimpleHTTPServer. The -m argument makes python to search for a module named SimpleHTTPServer.py in all the possible system locations (listed in sys.path and $PYTHONPATH shell variable). Once found, it executes it as a script.

Start an SMTP server

$ python -m smtpd -n -c DebuggingServer localhost:<PORT>

This one-liner starts an SMTP server on a chosen port > 1024 (only root can listen to ports <= 1024).

Change to the previous working directory

$ cd -

Quickly backup or copy a file

$ cp filename{,.bak}

This one-liner copies the file named filename to a file named filename.bak. Here is how it works. It uses brace expansion to construct a list of arguments for the cp command. Brace expansion is a mechanism by which arbitrary strings may be generated. In this one-liner filename{,.bak} gets brace expanded to filename filename.bak and puts in place of the brace expression. The command becomes cp filename filename.bak and the file gets copied.

Brace expansion

Talking more about brace expansion, you can do all kinds of combinatorics with it. Here is a fun application:

$ echo {a,b,c}{a,b,c}{a,b,c}

It generates all the possible 3-letter strings from the set {a, b, c}:

aaa aab aac aba abb abc aca acb acc
baa bab bac bba bbb bbc bca bcb bcc
caa cab cac cba cbb cbc cca ccb ccc

And here is how to generate all the possible 2-letter strings from the set {a, b, c}:

$ echo {a,b,c}{a,b,c}

Output:

aa ab ac ba bb bc ca cb cc

Capture video of a *nix desktop with ffmpeg

$ ffmpeg -f x11grab -s 800x600 -r 25 -i :0.0 -sameq /tmp/capture.mpg

Quick options overview (see man ffmpeg [good luck!] for more info):

Wipe a file

$ > file.txt

This command either wipes the file file.txt or creates a new file called file.txt.

Note that touch file.txt creates file.txt if it doesn't exist, but if it exists, it simply updates its access and modification timestamps.

Tweet from the shell using curl

$ curl -u user:pass -d status='Tweeting from Bash!' http://twitter.com/statuses/update.xml

This one-liner tweets your message from the terminal. It uses the curl program to HTTP POST your tweet via Twitter's API.

Find out which programs listen on which TCP ports

# netstat -tlnp

Paramaters overview:

Display currently mounted file systems formatted in columns

$ nicemount() { (echo "DEVICE PATH TYPE FLAGS" && mount | awk '$2=$4="";1') | column -t; }

You can add the alias definition on your .bashrc, so that you can call nicemount every time you need it, directly.

Example output:


DEVICE                    PATH   TYPE   FLAGS
/dev/root / ext3 (rw)
/proc /proc proc (rw)
/dev/mapper/lvmraid-home /home ext3 (rw,noatime)

Read Wikipedia via DNS

$ dig +short txt <keyword>.wp.dg.cx

This is very interesting!

David Leadbeater created a DNS server, which when queried with a TXT record type, returns a short plain-text version of a Wikipedia article. Here is his presentation.

This allows us to query Wikipedia even behind a restrictive firewall which blocks TCP requests, but not DNS queries (i.e. WiFi in hotels).

Here is an example, let's find out what "Penguin" means:

$ dig +short txt penguin.wp.dg.cx
"Penguins (order Sphenisciformes, family Spheniscidae) are a group of aquatic, flightless birds living almost exclusively in the Southern Hemisphere. Highly adapted for life in the water, penguins have countershaded dark and white plumage, and their wings " "have become flippers. Most penguins feed on krill, fish, squid, and other forms of sealife caught while swimming underwater... http://a.vu/w:Penguin"

Let's make a handy alias to append to our .bashrc:

wiki() { dig +short txt $1.wp.dg.cx; }

From now on, you can simply type wiki Penguin to see results.
Enjoy! :)

Download a website recursively with wget

$ wget --random-wait -r -l 10 -p -e robots=off -U Mozilla www.example.com

Here is the explanation of the arguments:

Show the size of all sub folders in the current directory

$ du -h --max-depth=1

If you are interested in both subfolder size and file size in the current directory, you can use the shorter:

$ du -sh *

Display the top ten running processes sorted by memory usage

$ ps aux | sort -nk +4 | tail

Quickly access ASCII table

$ man 7 ascii

A simple timer

$ time read

This one-liner can be used as a simple timer. For example, if you wish to time something, you can execute it when the event starts and press the return key when it ends. It will output how much time has passed between the instant you fired it up and the instant you pressed return.

List 10 most often used commands

$ history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

First, history outputs all the commands the person has executed. Next, awk counts how many times the second column $2 appears in the output. Once history has output all the commands and awk has counted them, awk loops over all the commands and outputs the count a[i] separated by space, followed by the command itself. Then sort takes this input and sorts numerically -n and reverses the output -r, so that most frequent commands were on top. Finally head outputs the first 10 most frequent history commands.