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,585 results
apg -a 0 -n 10
:1,$d
2009-03-28 02:28:19
User: acirulli
Tags: vim vi
3

Within vi allow to empty a text file in one shot

!}sort
2009-03-28 00:18:39
User: mpb
12

The vi key sequence !}command will send the file contents from the cursor

to the next blank line as STDOUT to the command specified

and replace that sequence of file lines with the output of the command.

For example: sorting a block of data - !}sort

The sequence !{command will do the same but "upwards" (from the current position towards the start of the file.

chmod +x FILES
sudo chmod -R g=u-w,g+X *
ls -Sl * | head
2009-03-27 23:20:32
User: colinpj
Functions: ls
-4

head by default displays first ten lines of its output. Use 'head -nXX' to display the XX largest files

base64 -d <<< aHR0cDovL3d3dy50d2l0dGVyc2hlZXAuY29tL3Jlc3VsdHMucGhwP3U9Y29tbWFuZGxpbmVmdQo=
2009-03-27 23:20:23
User: stu
15

apart from not being generalisable to all shells, `Y <<< X` seems nicer to me than `echo X | Y`, e.g.

&lt;&lt;&lt; lol cat;

it reads easier, you type less, and it also looks cool

kill -9 $$
2009-03-27 23:13:53
User: stu
Functions: kill
Tags: history exit
28

this exits bash without saving the history. unlike explicitly disabling the history in some way, this works anywhere, and it works if you decide *after* issuing the command you don't want logged, that you don't want it logged

... $$ ( or ${$} ) is the pid of the current bash instance

this also works perfectly in shells that don't have $$ if you do something like

kill -9 `readlink /proc/self`
netstat -atn | awk ' /tcp/ {printf("%s\n",substr($4,index($4,":")+1,length($4) )) }' | sed -e "s/://g" | sort -rnu | awk '{array [$1] = $1} END {i=32768; again=1; while (again == 1) {if (array[i] == i) {i=i+1} else {print i; again=0}}}'
2009-03-27 20:38:43
User: mpb
Functions: awk netstat sed sort
4

Some commands (such as netcat) have a port option but how can you know which ports are unused?

service --status-all | grep running
2009-03-27 19:53:30
User: Williebee
Functions: grep
1

A quick way to list services running

:g:^\s*#.*:m0
2009-03-27 18:56:36
User: jnash
4

Python comments begin with a #. Modify to suit other languages.

Other uses: Instead of m0 use m$ for end of file or d for deleting all comments.

perl -pi -e 's/foo/bar/g' $(grep -rl foo ./*)
2009-03-27 17:21:35
User: dopeman
Functions: grep perl
Tags: perl
-1

This command will replace all instances of 'foo' with 'bar' in all files in the current working directory and any sub-directories.

perl -pi -e 's/foo/bar/g' $(grep -l foo ./*)
2009-03-27 17:18:08
User: dopeman
Functions: grep perl
-1

This command will replace all instances of 'foo' with 'bar' in all files in the current working directory.

rename 's/.*/[it]$&/' *.pdf
iptables -F && iptables -X && iptables -P INPUT ACCEPT && iptables -OUTPUT ACCEPT
2009-03-27 15:03:54
User: lme
Functions: iptables
Tags: Linux iptables
0

This turns your iptables packet filter to a "Allow any from any to any" filter, so you can rule out any filtering issues when you have a problem to enable a connection from or to your host.

To re-enable it, run /etc/init.d/iptables restart

vlc -vvv http://mafreebox.freebox.fr/freeboxtv/playlist.m3u --sout '#transcode{vcodec=mp2v,vb=384,scale=0.5,acodec=vorbis,ab=48,channels=1}:standard{access=http,mux=ogg,url=:12345}' -I ncurses 2> /dev/null
2009-03-27 14:29:27
User: pronoiaque
4

You can watch channels of your freebox, everywhere. With " vlc http://your-ip:12345 " on the client and ncurses vlc interface on the host. et voila

find ./ -name '*.JPG' -type f -execdir rename -f 'y/A-Z/a-z/' {} \+
2009-03-27 13:49:56
User: pronoiaque
Functions: find rename
2

Change files case, without modify directories, recursively.

... fucking vfat

find ./ -iname "*.mp3" -type f -printf "mv '%p' '%p'\n" | sed -e "s/mp3'$/mp3'/I" | sh
2009-03-27 13:42:40
User: jnash
Functions: find sed
-1

Extensible to other ugly extensions like *.JPG, *.Jpg etc..

Leave out the last pipe to sh to perform a dry run.

mogrify -format jpg -quality 80 -resize 800 *.jpg
2009-03-27 13:30:26
User: pronoiaque
3

To resize photos without changing exif datas, pretty cool for gps tagging.

(Require ImageMagick)

netstat -an | grep -i listen
wget -qO- whatismyip.org
on the listening side: sudo nc -lp 2022 | sudo tar -xvf - and on the sending side: tar -cvzf - ./*| nc -w 3 name_of_listening_host 2022
2009-03-27 09:59:33
User: smcpherson
Functions: sudo tar
Tags: netcat
-2

This is useful for sending data between 2 computers that you have shell access to. Uses tar compression during transfer. Files are compressed & uncompressed automatically. Note the trailing dash on the listening side that makes netcat listen to stdin for data.

on the listening side:

sudo nc -lp 2022 | sudo tar -xvf -

explanation: open netcat to -l listen on -p port 2022, take the data stream and pipe to tar -x extract, -v verbose, -f using file filename - means "stdin"

on the sending side:

tar -cvzf - ./*| nc -w 3 name_of_listening_host 2022

explanation: compress all files in current dir using tar -c create, -v verbose, -f using file, - filename - here means "stdout" because we're tar -c instead of tar -x, -w3 wait 3 seconds on stream termination and then end the connection to the listening host name_of_listening_host, on port 2022

while true; do X=$Y; sleep 1; Y=$(ifconfig eth0|grep RX\ bytes|awk '{ print $2 }'|cut -d : -f 2); echo "$(( Y-X )) bps"; done
function svnundopoint() { if [ -d .undo ]; then r=`svn info | grep Revision | cut -f 2 -d ' '` && t=`date +%F_%T` && f=${t}rev${r} && svn diff>.undo/$f && svn stat>.undo/stat_$f; else echo Missing .undo directory; fi }
2009-03-27 07:14:31
User: codeape
Functions: cut echo grep info
Tags: bash svn
2

Allows you to save progress without committing.

To revert to an undo point, svn revert then apply the undo point with patch.

svn revert -R . && patch -p0 < .undo/2009-03-27_08:08:11rev57

Similar: http://www.commandlinefu.com/commands/view/373/archive-all-files-containing-local-changes-svn

setarch i386 [command [args]]
2009-03-27 05:49:08
User: GinoMan2440
2

The above was done using the i386 flashplayer plugin, and was installed on a AMD64 machine running an AMD64 kernel and AMD64 programs. the resulting plugin install ultimately didn't work for swiftfox (but worked for iceweasel) without also covering it with a nspluginwrapper which took a bit of fenangaling to get to work (lots of apt-getting) but it is a nice feature to be able to trick installers that think you need i386 into running on a amd64, or at least attempting to run on amd64. Enjoy