#22. Run the previous shell command but replace every "foo" with "bar"

$ !!:gs/foo/bar

I explained this type of one-liners in one-liner #5 already. Please take a look for a longer discussion.

To summarize, what happens here is that the !! recalls the previous executed shell command and :gs/foo/bar substitutes (the :s flag) all (the g flag) occurrences of foo with bar. The !! construct is called an event designator.

#23. Top for files

$ watch -d -n 1 'df; ls -FlAt /path'

This one-liner watches for file changes in directory /path. It uses the watch command that executes the given command periodically. The -d flag tells watch to display differences between the command calls (so you saw what files get added or removed in /path). The -n 1 flag tells it to execute the command every second.

The command to execute is df; ls -FlAt /path that is actually two commands, executed one after other. First, df outputs the filesystem disk space usage, and then ls -FlAt lists the files in /path. The -F argument to ls tells it to classify files, appending */=>@| to the filenames to indicate whether they are executables *, directories /, sockets =, doors >, symlinks @, or named pipes |. The -l argument lists all files, -A hides . and .., and -t sorts the files by time.

Special note about doors - they are Solaris thing that act like pipes, except they launch the program that is supposed to be the receiving party. A plain pipe would block until the other party opens it, but a door launches the other party itself.

Actually the output is nicer if you specify -h argument to df so it was human readable. You can also join the arguments to watch together, making them -dn1. Here is the final version:

$ watch -dn1 'df -h; ls -FlAt /path'

Another note - -d in BSD is --differences

#24. Mount a remote folder through SSH

$ sshfs name@server:/path/to/folder /path/to/mount/point

That's right, you can mount a remote directory locally via SSH! You'll first need to install two programs however:

sshfs client that uses FUSE and sftp (secure ftp - comes with OpenSSH, and is on your system already) to access the remote host.

And that's it, now you can use sshfs to mount remote directories via SSH.

To unmount, use fusermount:

fusermount -u /path/to/mount/point

#25. Read Wikipedia via DNS

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

This is probably the most interesting one-liner today. David Leadbeater created a DNS server, which when queried the TXT record type, returns a short plain-text version of a Wikipedia article. Here is his presentation on he did it.

The one-liner uses dig, the standard sysadmin's utility for DNS troubleshooting to do the DNS query. The +short option makes it output only the returned text response, and txt makes it query the TXT record type.

This one-liner is actually alias worthy, so let's make an alias:

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

Try it out:

$ wiki hacker
"Hacker may refer to: Hacker (computer security), ..."

It works!

If you don't have dig, you may also use host that also performs DNS lookups:

host -t txt hacker.wp.dg.cx

#26. Download a website recursively with wget

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

This one-liner does what it says. Here is the explanation of the arguments:

#28. Execute a command without saving it in the history

$ <space>command

This one-liner works at least on bash, I haven't tested other shells.

If you start your command by a space, it won't be saved to bash history (~/.bash_history file). This behavior is controlled by $HISTIGNORE shell variable. Mine is set to HISTIGNORE="&:[ ]*", which means don't save repeated commands to history, and don't save commands that start with a space to history. The values in $HISTIGNORE are colon-separated.

#29. Show the size of all sub folders in the current directory

$ du -h --max-depth=1

The --max-depth=1 causes du to summarize disk usage statistics for directories that are depth 1 from the current directory, that is, all directories in the current directory. The -h argument makes the summary human-readable, that is, displays 5MB instead of 5242880 (bytes).

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

$ du -sh *

#30. Display the top ten running processes sorted by memory usage

$ ps aux | sort -nk +4 | tail

This is certainly not the best way to display the top ten processes that consume the most memory, but, hey, it works.

It takes the output of ps aux, sorts it by 4th column numerically and then uses tail to output the last then lines which happen to be the processes with the biggest memory consumption.

If I was to find out who consumes the most memory, I'd simply use htop or top and not ps.

Bonus one-liner: Start an SMTP server

python -m smtpd -n -c DebuggingServer localhost:1025

This one-liner starts an SMTP server on port 1025. It uses Python's standard library smtpd (specified by -m smtpd) and passes it three arguments - -n, -c DebuggingServer and localhost:1025.

The -n argument tells Python not to setuid (change user) to "nobody" - it makes the code run under your user.

The -c DebuggingServer argument tells Python to use DebuggingServer class as the SMTP implementation that prints each message it receives to stdout.

Absolutely excellent one-liners. I've been using Linux for a long time, and it still delights me when I see a command or option I didn't know. Somehow all these years I've missed the 'column' command. And the max-depth option to du? Genius. Thanks for putting this together

A few problems with these one-liners:
not every 'mount' formats options like (opt1,opt2), some use (opt1, opt2), which impacts column -t output on filesystems with lots of options;
the event indicator !! is a bash-ism;
watch seems to be linux/GNU only, not available elsewhere, and on FreeBSD it is a totally different command:
watch(8) - snoop on another tty line;
--max-depth is a GNU du extension;
sort's -k option takes a initial number, i.e. '4', not '+4' which is an invalid field specification (but I guess it works with GNU sort).

I have used du -sh * in the past for listing the sizes of the subdirectories and files under the current directory. The same effect (of listing files too) can be achieved with the given --max-depth argument by adding --all also (du -s --all --max-depth=1), but we are lazy and less typing is better, isn't it? :)

Nice article! I was wondering what the default values are for $HISTIGNORE. This is not defined by default on my system, (Debain Sid), but does ignore a space by default. I was wondering if there are any other characters ignored by default without having to dig through the source. Can't find any documentation.

What he's doing is first he sets PATH to itself (eval PATH=$PATH), which leaves him with something like
PATH=/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
in his history. Then he just uses the editor to modify this and run it again, thus setting PATH to the modified value.

#21
The ( ) force a new subshell, where { } doesn't.
The && test seems pointless. I'd use ;
nicemount() is a function, not an alias. It could be made an alias.
The echo stuff can be put into awk with a BEGIN{} block and a print.

I promise it will not be a waste of time at all. Terse. Stack machine. Scriptable. Very powerful. And it probably will give you ideas.

My understanding is that FORTH will be part of the laptops to be given to underprivileged children in developing nations. If kids start learning FORTH, it could change everything. FORTH is, in effect, "assemblylanguagescript". It is as malleable as is possible. As we know, all other languages are just ways of avoiding the tedium of assembly. But if young people start using assembly again (i.e. "rediscover it" through FORTH), all other languages will be inevitably viewed as slow and limited. HL languages cannot compete with well-written assembly.

There are over *2000* computer languages and still more being created. Hardware performance acclerates via Moore's Law but software lags behind. Much of the power of today's CPU's is not utilised. How long can this go on?

Your writing is lucid and uncoloured by bias. Please write about FORTH.