Hey everyone, this is the fourth article in the series on the most popular commandlinefu one-liners explained.
Here are the first three parts:
- Part I: Top Ten One-Liners from CommandLineFu Explained
- Part II: The Next Ten One-Liners from CommandLineFu Explained
- Part III: Another Ten One-Liners from CommandLineFu Explained
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:
- man 1 intro - a tutorial that gets you started with Linux if you have never used it.
- man 2 syscalls - lists all Linux system calls by kernel version.
- man 2 select_tut -
select()
system call tutorial. - man 3 string - lists all <string.h> string manipulation functions.
- man 3 stdio - lists and describes all <stdio.h> standard input/output library functions.
- man 3 errno - lists and describes all
errno
error numbers. - man 4 console_codes - Linux console escape and control sequences.
- man 4 full -
/dev/full
- a device that simulates a full device. - man 5 proc - lots of info about the
/proc
filesystem. - man 5 filesystems - lists various Linux filesystems.
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 want 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 want 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 want 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 code>you@gmail.com</code. 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!