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,605 results
gem list | cut -d" " -f1 | grep --invert-match "test-unit\|psych\|io-console\|rdoc\|json\|bigdecimal\|rake\|minitest" | xargs gem uninstall -aIx
2014-01-10 10:58:59
User: gdelfino
Functions: cut grep xargs
-1

This command removes all ruby gems except the default ones that can not be removed. It is based on http://geekystuff.net/2009/01/14/remove-all-ruby-gems/

WID=xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}' xdotool windowfocus $WID xdotool key ctrl+shift+t wmctrl -i -a $WID
2010-03-22 08:34:24
User: drunkcoder
Functions: awk grep
-1

open a new tab in active gnome-terminal instance

needs sudo apt-get install xdotool

and sudo apt-get install wmctrl

sed -i 's/\r\n//' file.txt
perl -MModule::Name\ 9999 -e 1
2013-01-15 22:51:39
User: three18ti
Functions: perl
Tags: bash perl zsh
-1

This attempts to load a Perl Module (-M flag) and use version 9999, since no module has a version this high, Perl exits either a) telling you the version of the module installed or b) tells you it can't find the module.

du -sm *|sort -rn|head -10
season=1; for file in $(ls) ; do dir=$(echo $file | sed 's/.*S0$season\(E[0-9]\{2\}\).*/\1/'); mkdir $dir ; mv $file $dir; done
2009-05-27 03:30:58
User: lonecat
Functions: echo file mkdir mv sed
-1

It happened to me that I got a season of a tv-show which had all files under the same folder like /home/blah/tv_show/season1/file{1,2,3,4,5,...}.avi

But I like to have them like this:

/home/blah/tv_show/season1/e{1,2,3,4,5,...}/file{1,2,3,4,5,...}.avi

So I can have both the srt and the avi on one folder without cluttering much. This command organizes everything assuming that the filename contains Exx where xx is the number of the episode.

You may need to set:

IFS=$'\n'

if your filenames have spaces.

xwinwrap -ni -argb -fs -s -st -sp -nf -b -- /usr/libexec/xscreensaver/atlantis -count 20 -window-id WID &
2009-09-23 05:29:21
User: dstahlke
Tags: xwinwrap
-1

xwinwrap allows you to run any program in the root desktop pane. I like to run atlantis, it goes well against Fedora 10's blue sun background. At one time this also served a useful purpose for me as a workaround for a problem that would happen when the video card went into sleep mode.

wget -O xkcd_$(date +%y-%m-%d).png `lynx --dump http://xkcd.com/|grep png`; eog xkcd_$(date +%y-%m-%d).png
awk -vRS="</Tag2>" '/<Tag2>/{gsub(/.*<Tag2>/,"");print}' file
while true; do /bin/ping -q -c1 -w3 8.8.8.8 2>&1 > /dev/null || echo "8.8.8.8 ping failed at $(date +%d/%m/%y) $(date +%H:%M:%S)" >> /var/log/ping.log; sleep 10; done &
2011-05-12 01:51:07
User: Arkey
Functions: echo sleep
-1

This command are using by me for checking connection with Internet.

fping -g 192.168.1.1 192.168.1.10 -r 1 | grep -v unreachable | awk '{print $1}'
Find files that have been changed by a Chef run today.
$ apg -m 40 -a 1
2013-07-26 14:13:49
User: blz
-1

apg generates several random passwords. It uses several password generation algorithms (currently two) and a built-in pseudo random number generator.

find -L /home/sonic/archive -name '*gz' -type f
2013-10-07 14:32:22
User: sonic
Functions: find
Tags: find
-1

If /home/sonic/archive/ was a symlink to /backup/sonic/archive it would follow the links and give you the file listing. By default find will NOT follow symbolic links. The default behavior for the find command is to treat the symlinks as literal files.

I discovered this when trying to write a script run via cron to delete files with a modification time older than X days. The easiest solution was to use:

/usr/bin/find -L /home/sonic/archive -name '*gz' -type f -mtime +14 -exec rm '{}' \;

IFS=$(echo -en "\n\b"); mkdir Epub; for i in `find . | grep epub`; do echo "epub: $i"; mv -v "$i" Epub; done
C:\> shutdown /f /r /t 0
2014-04-02 22:35:00
User: mpb
Functions: shutdown
-1

Today, I needed to reboot a Windoze machine on another continent which had no shutdown or restart options via "Start" in the remote desktop (the only options available were: "Logoff, Disconnect, or Lock").

Fortunately, I found how to shutdown and restart from the command line.

getarray(){ a=$1;b="${a[$2]}";eval "c=$b";echo "${c[$3]}";return 0;};a[0]="( a b c )";a[1]="( d e f )";getarray a 1 2
whereis command
cat file | sed -n -r '/^100$|^[0-9]{1,2}$/p'
2010-05-15 19:15:56
User: voyeg3r
Functions: cat file sed
-1

-r to use extended regex

^ begin line

| alternative

get 100 or 0-9 one or two times

STA=KILCHICA30 PAG=http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=${STA} D=($(curl -s $PAG | sed -n 's/.*<\(temp_f\|wind_dir\|wind_mph\)>\(.*\)<\/.*/\2/p')) echo ${D[1]}@${D[2]}mph ${D[0]}F
2010-10-28 00:40:28
User: sud0er
Functions: echo sed
-1

Uses the Wunderground web API to get weather data. Change the STA variable to a station near you.

cat search_items.txt | while read i; do surfraw google -browser=firefox $i; done
2011-05-12 09:27:08
User: bubo
Functions: cat read
-1

tired of opening tabs and fill in search forms by hand? just pipe the search terms you need into this surfraw loop. you can use any browser you have installed, but a graphical browser with a tabbed interface will come in handy. surfraw can be found here:

http://surfraw.alioth.debian.org

wget --no-check-certificate https://code.google.com/p/msysgit/downloads/list -O - 2>nul | sed -n "0,/.*\(\/\/msysgit.googlecode.com\/files\/Git-.*\.exe\).*/s//http:\1/p" | wget -i - -O Git-Latest.exe
2012-11-14 08:17:50
User: michfield
Functions: sed wget
Tags: git windows wget
-1

This command should be copy-pasted in Windows, but very similar one will work on Linux.

It uses wget and sed.

cd -
perl -e 'printf "%vd\n",pack "N",rand 256**4'