Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 11,951 results
tar cvfz changes.tar.gz --exclude-vcs `svn diff -rM:N --summarize . | grep . | awk '{print $2}' | grep -E -v '^\.$'`
2009-07-13 19:39:10
User: jaysee
Functions: awk diff grep tar
2

Handy when you need to create a list of files to be updated when subversion is not available on the remote host. You can take this tar file, and upload and extract it where you need it. Replace M and N with the revisions specific to yours. Make sure you do this from an updated (svn up) working directory.

grep <pattern> -R . --exclude-dir='.svn'
fmiss() { grep -RL "$*" * }
2009-07-13 18:30:54
User: inkel
Functions: grep
Tags: grep
1

This one would be much faster, as it's only one executed command.

cat /dev/urandom|awk 'BEGIN{"tput cuu1" | getline CursorUp; "tput clear" | getline Clear; printf Clear}{num+=1;printf CursorUp; print num}'
2009-07-13 07:30:51
User: axelabs
Functions: awk cat printf
Tags: nawk awk clear tput
0

awk can clear the screen while displaying output. This is a handy way of seeing how many lines a tail -f has hit or see how many files find has found. On solaris, you may have to use 'nawk' and your machine needs 'tput'

cd /example/directory && unzip \*.zip
2009-07-12 21:45:13
User: Paaskehare
Functions: cd
-5

Simple but useful little command to unzip all files in a directory.

infile=$1 for i in $(cat $infile) do echo $i | tr "," "\n" | sort -n | tr "\n" "," | sed "s/,$//" echo done
2009-07-12 21:23:37
User: iframe
Functions: cat echo sed sort tr
Tags: cat bash sort sed tr
0

Save the script as: sort_file

Usage: sort_file < sort_me.csv > out_file.csv

This script was originally posted by Admiral Beotch in LinuxQuestions.org on the Linux-Software forum.

I modified this script to make it more portable.

pkill -9 -f program
2009-07-12 14:46:14
User: ioggstream
Tags: kill pgrep pkill
1

Kills a process matching program. I suggest using

$ pgrep -fl program

to avoid over-killings

Nice the following: kills all bash process owned by guest

$ pkill -9 -f bash -u guest

urxvtd -q -o -f
2009-07-12 14:09:06
4

- for .xsession use -

Advantages of running a urxvt daemon include faster creation time for terminal windows and a lot of saved memory.

You can start new terminals as childs of urxvtd by typing urxvtc. Another advantage is, that background jobs are always owned by the urxvtd and will survive as long the daemon is running.

lsof |awk ' {if ( $0 ~ /home/) print substr($0, index($0,"/home") ) }'|cut -d / -f 1-4|sort|uniq -c|sort -bgr
perl -e '$p=qr!(?:0|1\d{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)!;print((shift=~m/^$p\.$p\.$p\.$p$/)?1:0);' 123.123.123.123
2009-07-12 00:24:29
User: speaker
Functions: perl
0

This command will output 1 if the given argument is a valid ip address and 0 if it is not.

iconv -f437 -tutf8 asciiart.nfo
2009-07-11 23:50:05
User: speaker
Functions: iconv
8

Files containing ascii art (e.g. with .nfo extension) are typically not correctly reproduced at the command line when using cat. With iconv one can easily write a wrapper to solve this:

#!/bin/bash

if [ -z "$@" ]; then echo "Usage: $(basename $0) file [file] ..."

else iconv -f437 -tutf8 "$@"; fi

exit 0
while true; do beep -l66 -f`head -c2 /dev/input/mice|hexdump -d|awk 'NR==1{print $2%10000}'`; done
2009-07-11 12:01:27
User: 5z474n
Functions: awk hexdump
4

Beeps on mouse's every move. Bear in mind that, at least on Ubuntu, /dev/input/mice can be read only by root.

xdg-open http://gmail.com
2009-07-11 11:48:32
User: PLA
8

I rarely use the system menu to start applications. I use keyboard shortcuts instead. Keyboard shortcuts are convenient time savers. To configure the shortcuts I use gconf-editor or CompizConfig Settings Manager (if I have Compiz turned on). Lately I've been using Chromium as my browser. Since Chromium is under heavy development it occasionally becomes unusable and I have to switch back to Firefox. I was hard-coding the browser command in my keyboard shortcuts. This became problematic with frequent browser hopping. I now use the xdg-open command when defining my keyboard shortcuts. xdg-open opens a file or URL in the user's preferred application. In the metacity keybinging_commands I have xdg-open http://gmail.com where I used to have firefox http://gmail.com. Now when switching browsers I don't have to update each keyboard binding command I just change my default browser in Ubuntu by clicking through the menu: System / Preferences / Preferred Applications / Web Browser. xdg-open is more convenient for this browser hopper.

tokill=`ps -fea|grep process|awk '{ printf $2" "}'`; kill -9 $tokill;
tempo=33; slope=10; maxfreq=888; function sinus { echo "s($1/$slope)*$maxfreq"|bc -l|tr -d '-'; }; for((i=1;;i++)); do beep -l$tempo -f`sinus $i`; done
2009-07-10 21:24:11
User: 5z474n
Functions: bc echo tr
1

Infinitely plays beeps with sinusoidally changing sound frequency. Ideal for alarm on an event.

tar -C <source> -cf - . | tar -C <destination> -xf -
cat /dev/clipboard; $(somecommand) > /dev/clipboard
2009-07-10 18:48:21
User: sud0er
Functions: cat
Tags: windows cygwin
12

I spent a bunch of time yesterday looking for the xsel package in Cygwin- turns out you can use the /dev/clipboard device to do the same thing.

cat <<.>> somefilename
2009-07-10 17:45:42
User: tomlouie
Functions: cat
Tags: text
4

If you just want to write or append some text to a file without having to run a text editor, run this command. After running the command, start typing away. To exit, type . on a line by itself.

Replacing the >> with a single > will let you overwrite your file.

for i in `seq 1 3`; do scp finku@server$i:file.txt server$i-file.txt; done
ssh root@server 'tail --max-unchanged-stats=10 -n0 -F /var/log/auth.log ' | grep Accepted | while read l ; do kdialog --title "SSH monitor" --passivepopup "$l" 3; done
for y in $(seq 2009 2011); do cal $y; done
2009-07-10 10:07:46
Functions: cal seq
Tags: bash seq cal
3

print multiple increasing years using cal - calendar -. You can also try

seq Start Increment End
for y in 2009 2010 2011; do cal $y; done
echo -e "\e[31m"; while $t; do for i in `seq 1 30`;do r="$[($RANDOM % 2)]";h="$[($RANDOM % 4)]";if [ $h -eq 1 ]; then v="\e[1m $r";else v="\e[2m $r";fi;v2="$v2 $v";done;echo -e $v2;v2="";done;
2009-07-10 04:20:43
User: Patola
Functions: echo
Tags: color
3

It's the same command as submitted, but first with a command to make all characters green. It's the only way it looked "matrix-like" on my gnome-terminal.

while $t; do for i in `seq 1 30`;do r="$[($RANDOM % 2)]";h="$[($RANDOM % 4)]";if [ $h -eq 1 ]; then v="\e[1m $r";else v="\e[2m $r";fi;v2="$v2 $v";done;echo -e $v2;v2="";done;
/originalInstall/gem list | tr -d '(),' | xargs -L 1 sudo ./gemInst.sh
2009-07-09 21:46:06
User: snakerdlk
Functions: sudo tr xargs
Tags: bash Linux
0

gemInst.sh:

#!/bin/bash

for i in $@; do

if [ "$1" != "$i" ]

then

echo /newInstall/gem install $1 -v=\"$i\"

/newInstall/gem install $1 -v="$i"

if [ "$?" != "0" ]

then

echo -e "\n\nGEM INSTALL ERROR: $1\n\n"

echo "$1" > gemInst.err

fi

fi

done