Silly one-liners

Posted on February 22, 2006, under general.

I saw this list of Linux (actually linux + bash) one-liners linked from digg, and two in particular really annoyed me because they are terrible examples. I don’t mind when people simplify matters or skip over some depth when trying to teach something, but this should never happen at the cost of teaching something with is plain wrong or unneccessarily complicated.

date | awk -F : '{ print $1 ":" $2 }' | awk '{ print $1 " " $2 " " $3 }' 

should be:

date +"%a %b %d"

and this one:

ps -ef | grep ppp | awk '{ print $2 }' | xargs kill -9

should be:

pkill -9 ppp

Ahhh, now that’s a load off of my mind. Some thing they didn’t mention on the page was the most common one-liner error – which is over-use of cat. Many people forget that you can input a file to any command using input redirect ( < filename) and that many commands accept filenames as arguments too.

This isn’t a one-liner, but I thought I’d contribute it anyway. If your system is really overloaded, and fork() is blocking badly, and you need to kill processes in a hurry. You can do it entirely using bash built-ins:

cd /proc/
for pid in [0-9]* ; do
      read name < $pid/cmdline
      case $name in ppp*
              kill -9 $pid

(kill is a bash built-in too). Not a single fork() call. Anyone who has ever had to use “echo *” as a poor-man’s ls will appreciate the above.

5 Replies to "Silly one-liners"


Jon  on February 22, 2006

Me too – most articles about one-liners are poor, popular ones more so. Here’s another example that annoyed me:

grep -i href Bash.html | grep -v ‘#’ | awk -F\” ‘{ print “” $2 }’ | xargs -l1 wget -U mozilla

…the -B argument to wget would simplify the cumbersome awk component. Using xargs with -l1 is almost pointless too (while read i… do … would be one process shorter) and using -U for wget is unnecessary and immoral if used all the time.


David Malone  on May 29, 2006

Banging on people for not knowing pkill seems a bit unfair – it’s only been around since Solaris 7 and I’m not even sure it is in SUS/Posix. Though, even if you don’t have pkill

ps -ef | grep ppp | awk '{ print $2 }' | xargs kill -9

still isn’t a great one liner. I guess the traditional varient would have been:

 kill `ps -ef | awk '/ppp/ {print $2}'`

Assuming you don’t have *lots* of ppp processes running.


coder  on July 13, 2007

Heh, nice post, but you missed the classic one:

cat file | grep pattern


cat file | cut|sort|whatever


Peteris Krumins  on September 28, 2008

Hi. I wanted to let you know that I just wrote a blog post about Awk one-liners.

In this post I explain all the famous (you’ll see what I mean) Awk one-liners.

The post is here:
Famous Awk One-Liners Explained


Leave a Comment