This article is part of the article series "CommandLineFu One-Liners Explained."
<- previous article next article ->
CommandLineFu Explained

Hey everyone, this is the fourth article in the series on the most popular commandlinefu one-liners explained.

Here are the first three parts:

And here are today's one-liners:

31. Quickly access ASCII table.

$ man 7 ascii

Ever forgot a keycode for some ASCII character or escape code? Look no further, man ascii contains the 7-bit ASCII table. Take a look at it online.

Linux man pages are full of gems like these. One day I actually went through all the man pages to find the most interesting ones. An article about them is upcoming but before I get it published, here are a few interesting ones:

And finally the section 7 man pages that are most packed with wild and cool info,

  • man 7 bootparam - a tutorial and reference of Linux kernel boot parameters.
  • man 7 charsets - a Linux programmer's view of character sets and internationalization.
  • man 7 glob - how pathname globbing works.
  • man 7 hier - description of the Linux file system hierarchy.
  • man 7 operator - C language operator precedence and associativity table.
  • man 7 regex - basic and extended regular expression overview.
  • man 7 suffixes - lists common Linux file suffixes and the associated file types.
  • man 7 time - overview of time and timers.
  • man 7 units - kilo, kibi, mega, mebi, giga, gibi, ... - decimal (SI) and binary system of units.
  • man 7 utf8 - description of UTF-8 encoding.
  • man 7 url - description of URIs, URLs and URNs.

There are a lot more interesting man pages but these stood out from the rest.

32. Simple timer.

$ time read

This one-liner can be used 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 the event ends. It will output the time the event took to finish.

Here is how this one-liner works. First the time command times any command that is supplied to it. In this case the command supplied to it is read that reads a line from the standard input. As soon as you press enter, read finishes and time reports how long it took.

If you get annoyed by having to press enter, you can specify that read should return after having read 1 character,

$ time read -N 1

Now you can press any key to stop the timer.

If you wish to run a timer for a specific number of seconds, you can add -t flag for timeout,

$ time read -t 60

This would stop the timer after 60 seconds.

33. Shutdown a Windows machine.

$ net rpc shutdown -I IP_ADDRESS -U username%password

Everyone knows the net command, right? We all used to net use \\ip\ipc$ *, right? :)

Anyway, the net command comes as part of Samba, which allows you, for example, to use Linux in a Windows workgroup.

This particular one-liner executes the shutdown command on a Windows computer located at IP_ADDRESS with as the user username with password password.

To reboot a machine use the -r switch to net rpc:

$ net rpc shutdown -r -I IP_ADDRESS -U username%password

If you're on an unsecured network, don't forget about the good old nmblookup and smbclient tools that come with Samba.

34. Execute a command independently from the current shell.

$ (cd /tmp && ls)

This one-liner illustrates subshells. Here the commands cd /tmp and ls are executed but they do not affect the current shell. If you had done just cd /tmp && ls, your current shell would have changed directory to /tmp but in this one-liner it happens in a subshell and your current shell is not affected.

Surely, this is only a toy example. If you wanted to know what's in /tmp, you'd do just ls /tmp.

Actually, talking about cd, be aware of pushd and popd commands. They allow you to maintain a stack of directories you want to return to later. For example,

/long/path/is/long$ pushd .
/long/path/is/long$ cd /usr
/usr$ popd 
/long/path/is/long$

Or even shorter, passing the directory you're gonna cd to directly to pushd,

/long/path/is/long$ pushd /usr
/usr$ popd 
/long/path/is/long$

Another cool trick is to use cd - to return to the previous directory. Here is an example,

/home/pkrumins$ cd /tmp
/tmp$ cd -
/home/pkrumins$

35. Tunnel your SSH connection via intermediate host.

$ ssh -t reachable_host ssh unreachable_host

This one-liner creates an ssh connection to unreachable_host via reachable_host. It does it by executing the ssh unreachable_host on reachable_host. The -t forces ssh to allocate a pseudo-tty, which is necessary for working interactively in the second ssh to unreachable_host.

This one-liner can be generalized. You can tunnel through arbitrary number of ssh servers:

$ ssh -t host1 ssh -t host2 ssh -t host3 ssh -t host4 ...

Now catch me if you can. ;)

36. Clear the terminal screen.

$ CTRL+l

Pressing CTRL+l (that's small L) clears the screen leaving the current line at the top of the screen.

If you wish to clear just some line, you can use argumented version of CTRL+l - first press ESC, then the line you want to clear, let's say 21 (21st line), and then press the same CTRL+l. That will clear the 21st line on the screen without erasing the whole screen.

$ ESC 21 CTRL+l

This command outputs a special "clear-screen" sequence to the terminal. The same can be achieved by tput command,

$ tput clear

Another way to clear the terminal (usually when the screen gets garbled) is to use the reset command,

$ reset

37. Hear when the machine comes back online.

$ ping -a IP

Ever had a situation when you need to know when the system comes up after a reboot? Up until now you probably launched ping and either followed the timeouts until the system came back, or left it running and occasionally checked its output to see if the host is up. But that is unnecessary, you can make ping -a audible! As soon as the host at IP is back, ping will beep!

38. List 10 most often used commands.

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

The person who wrote it has the Unix mindset right. He's combining several shell commands to get the result he/she wants.

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.

If you want to see more than 10 commands (or less), change head to head -20 for 20 commands or head -5 for 5 commands.

39. Check gmail for new mail.

$ curl -u you@gmail.com --silent "https://mail.google.com/mail/feed/atom" |
  perl -ne \
  '
    print "Subject: $1 " if /<title>(.+?)<\/title>/ && $title++;
    print "(from $1)\n" if /<email>(.+?)<\/email>/;
  '

Gmail is cool because they offer an Atom feed for the new mail. This one-liner instructs curl to retrieve the feed and authenticate as you@gmail.com. You'll be prompted a password after you execute the command. Next it feeds the output to perl. Perl extracts the title (subject) of each email and the sender's email. These two items are printed to stdout.

Here is a the output when I run the command,

Subject: i heard you liked windows! (from gates@microsoft.com)
Subject: got root? (from bofh@underground.org)

40. Watch Star-Wars via telnet.

$ telnet towel.blinkenlights.nl

Needs no explaining. Just telnet to the host to watch ASCII Star-Wars.

And here is another one,

$ telnet towel.blinkenlights.nl 666

Connecting on port 666 will spit out BOFH excuses.

That's it for today.

I hope you enjoyed the 4th part of the article. Tune in next time for the 5th part.

Oh, and I'd love if you followed me on Twitter!

This article is part of the article series "CommandLineFu One-Liners Explained."
<- previous article next article ->

Comments

f00li5h Permalink
June 02, 2010, 07:58

meow!

ikbear Permalink
June 02, 2010, 12:59

I come from twitter, it is very useful. Thank you!

June 02, 2010, 13:09

Really great :)

I'm not use of Windows but with XP you can use "shutdown /i" and you got an interface or directly shutdown with different arg form the command line !

I love your ping -a :)

Vincent RABAH
http://www.it-wars.com

Rich Russon (FlatCap) Permalink
June 02, 2010, 14:41

In the example for 34, you "pushd .", then "cd /usr". It's quicker to just "pushd /usr". pushd will put the current directory on the stack, then cd for you.

June 02, 2010, 15:09

Great tip!

June 02, 2010, 15:03

Awsome and very useful, my favorite is $ ping -a IP.

June 02, 2010, 15:25

A variation on #38. I use the "sort | uniq -c | sort -rn" chain for a lot of things.

$ history | awk '{print $2}' | sort | uniq -c | sort -rn | head

June 02, 2010, 15:45

Good one!

Guru Permalink
June 02, 2010, 18:10

Peteris..curl is not available in HP or SUN. What is alternative for curl in HP or SUN?

Kozo Permalink
June 02, 2010, 19:37

Guru: on SUN (solaris) or HP (HP-UX) you can try wget.

Kent Fredric Permalink
June 03, 2010, 00:15

wget --user you@gmail.com -q -O - # this should work in place of the curl call.

Kent Fredric Permalink
June 03, 2010, 00:09

Theres a handy trick with SSH. If you are inside a network with an anally retentive firewall, but you need *inbound* ssh support from a given box ( for something like git+ssh ), you can do a bit of magic by forwarding your ssh port to the remote box.

ssh -R 32123:127.0.0.1:22 $remotebox
$remotebox:> git clone foo@127.0.0.1:32123/path/on/your/machine.

its essentially, forwarding an ssh server over ssh, and then sshing back the other way.

Kent Fredric Permalink
June 03, 2010, 00:35

You can do that awk command just as easily with perl, but its a bit longer.

history | 
  perl -nle '$_=~s/\s*\d*\s*//; 
             $cache{$_}++ 
            }{ 
              @foo = sort { -( $a->[0] <=> $b->[0] ) } 
                      map { [ $cache{$_}, $_ ] } 
                       keys %cache ; 
              print "@{$foo[$_]}" for 0..10 
'

I love that perl-butterfly operator :)

( line feeds added for clarity. )

June 03, 2010, 08:27

Excellent post, thanks :-)

Steve Permalink
June 03, 2010, 17:42

Does anyone else find it disturbing that so many packages no longer have "man" pages. They either rely on info or online help or have no documentation whatsoever.

alan Permalink
June 04, 2010, 16:08

"The same can be achieved by tput command,

$ tput clear"

Or you can just use the "clear" command

$ clear

It's also worth mentioning that this doesn't really clear the screen on terminal windows, just scrolls the window by enough lines to make it look cleared. If you scroll back up, you'll still see the old content.

June 04, 2010, 18:06

It actually depends on your terminal. clear, which internally does the same as tput clear, outputs the CSI 2 J escape sequence that instructs the terminal to erase the data on the screen. There may or may not be line scrolling depending on terminal capabilities.

Roman Permalink
June 07, 2010, 19:41

Heh, you should watch Star Wars in color over IPv6 - it's much better. ;=]

kangu Permalink
June 09, 2010, 14:47

Very inspiring! thanks for the all beautiful tips.
One little attention, I have always used arping/arpscan (based on hwd address resoultion) in complicated LANs, in order to verify the presence of specific machines. Most of the time the firewalls might be configured to only accept some specific ICMP packets.
Another subtle thing, I find it to some extent easier to use wts (windows terminal service) if executing remote commands on windows machines is being involved. samba isn't that easy to configure on most of unix flavors (at least for me). rdesktop comes to fulfil this need. Here is a simple trick emphasising it: rdesktop host_machine -u catonmat -p meow -s "cmd.exe /C command"

June 10, 2010, 20:26

Nice post, Peteris.

Referring the one-liner #31 and its info about man commands, here's a related one-liner that is helpful:

$ man keyword | col -bx > keyword.m

will filter the man output through the col tool, and the -bx options cause all the control characters (such as Ctrl-H in particular) to be stripped out. This makes the output much more readable on many terminals. I've used this trick a lot on many versions of UNIX for years, and even on Linux in its earlier versions. Nowadays, though, I've seen that on many modern Linux distributions, the man output does not have those control characters that mess up the display, or, it is automatically piped to the less command, which seems to filter them out. However, this command may still be useful if you want to redirect the man output to a file as in the example I gave above, because then you can open the file in a text editor like vim and not have the screen messed up by the control characters. The main control character which is used in man output is Ctrl-H for backspace, because man output uses that to repeat characters (of a word) many times with backspaces in between the repetitions, which cause bold text output on printers, but messes up screen displays.

- Vasudev

Sekhar Permalink
June 12, 2010, 22:14

hi.....please send me the latest hp0-p20 dumps to my mail id.
I have no money to buy it so please send it to me.I am waiting for your kind support.
Thanks in advance.

Sam Permalink
June 18, 2010, 17:30

Thanks for another excellent post!

Which shell are you using? In bash 3.2.48, the builtin read takes a lowercase -n.

I have the following in my .bashrc:

alias timer='(stty -echo; time read -n 1; stty echo) 2>&1 \
              | grep real | sed "s/real/Elapsed time:/"'

Alternatively, one could use:

command time -f "Elapsed time: %es" bash -c "read -n 1"

Not sure why, but stty doesn't seem confined to the sub-shell; ergo calling it the second time.

June 29, 2010, 04:10

Dude, the Star Wars is cracking me up. Thanks for the great post, these are my fav so far.

argv Permalink
July 16, 2010, 07:57

The "reset" command does not work for me if my TTY gets hosed. Nor does "stty sane".

But this always works:
printf "\033c"

In addtion to the manpage, my UNIX also comes with a copy of the ascii table in /usr/share/misc. Not sure if Linux has this.

@vasudev- It may be piped from troff/nroff to less. col is extremely useful.

September 21, 2010, 19:50

Great info even for new programmers.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "server_222": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements