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

Commands by sputnick from sorted by
Terminal - Commands by sputnick - 42 results
for i in *.flv *.mkv *.avi; do mplayer -ao null -vo null -ss 0 -endpos 1 >/dev/null "$i" 2> >(grep -qi error && echo >&2 "$i seems bad"); done
2013-08-20 22:23:11
User: sputnick
Functions: echo grep
0

If you want avoid to be annoyed when playing your favourite video files with your video player, first run this command to stash wrong files (and test tricks to play these wrong files).

grep -q '\<lm\>' /proc/cpuinfo && echo 64 bits || echo 32 bits
2013-02-09 13:01:36
User: sputnick
Functions: echo grep
1

This command tell you if your hardware is 32 or 64 bits even if you install a 32bits OS on a 64 bits hardware.

If your distro don't support the -q switch, try doing :

grep &>/dev/null '\<lm\>' /proc/cpuinfo && echo 64 bits || echo 32 bits
pdfcrack <FILE>
sed -i '$a\FOOBAR' *
mech-dump --links --absolute http://www.commandlinefu.com
2011-11-19 03:40:52
User: sputnick
Tags: perl html parsing
2

You need to install WWW::Mechanize Perl module with

# cpan -i WWW::Mezchanize

or by searching mechanize | grep perl in your package manager

With this command, you can get forms, images, headers too

var=:foobar:; echo ${var:1:-1}
curl sputnick-area.net/ua
2011-10-24 09:57:50
User: sputnick
Tags: curl headers
-3

That's useful when you're doing some web scraping http://en.wikipedia.org/wiki/Web_scraping and you're trying to test your possibly fake user-agent.

ftp-latest <<< "cd /; cls -1 | tail -1 | xargs -I% echo get % | /PATH/TO/ftp-latest"
2010-10-07 17:59:45
User: sputnick
Functions: ftp
0

Require lftp and this script to work (adapt path and credentials as needed):

echo $'#!/bin/bash\n# coded by sputnick under GPL 20101007\nlftp -u user,passwd remoteuser@host.tld -e "$(cat)"' > /PATH/TO/ftp-latest; chmod +x !$
passpdf(){ for i; do qpdf --password=<YOUR PASSWD> --decrypt "$i" "new$i"; done; }
2010-02-25 01:21:00
User: sputnick
Tags: pdf qpdf
-2

You should install qpdf.

That way, you can have a copy without any password required.

xrandr -q | awk -F'current' -F',' 'NR==1 {gsub("( |current)","");print $2}'
vlc -I ncurses <MEDIA_DIR>
man <COMMAND> | less +'/pattern'
sox *.mp3 -t wavpcm - | lame - > bunch.mp3
2010-01-20 01:27:12
User: sputnick
Tags: lame sox
0

That way, you can make a mix on the fly with the mp3 files in the current directory to make a bunch mp3 file.

( shopt -s extglob; rm !(<PATTERN>) )
2010-01-13 16:43:15
User: sputnick
Functions: rm
Tags: files rm glob shopt
7

Remove everything except that file with shell tricks inside a subshell to avoid changes in the environment.

help shopt
vimdiff scp://root@server-foo.com//etc/snmp/snmpd.conf scp://root@server-bar.com//etc/snmp/snmpd.conf
2010-01-06 18:12:45
User: sputnick
Tags: vim remote scp
17

You can use

vim scp://root@example.com//file

too in a simple case.

perl -le'print"-"x50'
2010-01-06 17:06:07
User: sputnick
Functions: perl
Tags: jot
4

Perl is installed by default on most modern OS when jot is not.

python2 <<< $'import json\nf = open("sessionstore.js", "r")\njdata = json.loads(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")'
2009-12-28 21:56:05
User: sputnick
Tags: firefox python
3

You have to do that before :

cd ~/.mozilla/firefox/<YOUR PROFILE>

Require bash. If you use something else, you may use

echo <CODE> | python

Forked from ArkSeth python script.

xvkbd -xsendevent -text $(date +%Y%m%d)
2009-12-23 12:01:07
User: sputnick
Functions: date
13

That works in all softs, CLI or GUI... I don't want to waste time to all the time typing the same stuff . So, I have that command in my window manager shortcuts ( meta+l ). All the window managers have editable shortcuts AFAIK. If not, or you don't want to use it that way, you can easily use the xbindkeys soft.

I you're using kde4, you can run :

systemsettings

then open "inputs actions" and create a new shortcut.

For Gnome take a look there : http://www.cyberciti.biz/faq/howto-create-keyboard-shortcuts-in-gnome/

A more advanced one, with strings and newlines :

xvkbd -xsendevent -text "---8<-----\nToday date is: $(date +%Y%m%d)\n---8<-----"

For complicated or long paste, you can feed xvkbd with a file :

xvkbd -xsendevent -file <file>

You can simulate ^C ( control+c ) too or others combinations of keys :

xvkbd -text "\C\Ac"

There's no man page nor help ( On my Archlinux distro ), but you can see online doc there : http://homepage3.nifty.com/tsato/xvkbd/

getent shadow | while IFS=: read a b c; do grep -q '!' <<< "$b" && echo "$a LOCKED" || echo "$a not locked"; done
2009-12-18 15:24:19
User: sputnick
Functions: echo getent grep read
0
man 5 shadow

I think it's more reliable, because

passwd -S

dont show "locked" but "L" as second field on my Archlinux for a particular user.

( unixhome alternative ).

tcpdump -v -i <INTERFACE> -s 0 -w /tmp/sniff.pcap port <PORT> # On the remote side
2009-12-17 22:08:30
User: sputnick
Functions: tcpdump
Tags: tcpdump pcap
4

Then hit ^C to stop, get the file by scp, and you can now use wireshark like this :

wireshark /tmp/sniff.pcap

If you have tshark on remote host, you could use that :

wireshark -k -i <(ssh -l root <REMOTE HOST> tshark -w - not tcp port 22)

The last snippet comes from http://wiki.wireshark.org/CaptureSetup/Pipes

COL=$(( $(tput cols) / 2 )); clear; tput setaf 2; while :; do tput cup $((RANDOM%COL)) $((RANDOM%COL)); printf "%$((RANDOM%COL))s" $((RANDOM%2)); done
perl -lne 'print for /url":"\K[^"]+/g' $(ls -t ~/.mozilla/firefox/*/sessionstore.js | sed q)
2009-12-14 00:51:54
User: sputnick
Functions: ls perl sed
0

If you want all the URLs from all the sessions, you can use :

perl -lne 'print for /url":"\K[^"]+/g' ~/.mozilla/firefox/*/sessionstore.js

Thanks to tybalt89 ( idea of the "for" statement ).

For perl purists, there's JSON and File::Slurp modules, buts that's not installed by default.

cpan -r
x=IO::Interface::Simple; perl -e 'use '$x';' &>/dev/null || cpan -i "$x"; perl -e 'use '$x'; my $ip='$x'->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>
2009-12-13 02:23:40
User: sputnick
Functions: perl
1

Thanks to comment if that works or not...

If you have already typed that snippet or you know you already have IO::Interface::Simple perl module, you can type only the last command :

perl -e 'use IO::Interface::Simple; my $ip=IO::Interface::Simple->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>

( The first perl command will install the module if it's not there already... )

xmlproc_parse.python-xml &>/dev/null <FILE> || exit 1
2009-12-11 17:30:03
User: sputnick
Functions: exit
Tags: python xml
-1

For debian likes, that's in python-xml package.