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,616 results
ffmpeg -an -f video4linux -s 320x240 -b 800k -r 15 -i /dev/v4l/video0 -vcodec mpeg4 myvideo.avi
2009-06-19 17:41:43
User: dcabanis
3

The option -an disables audio recording, -f forces the use of video4linux for the input, -s sets the video to the size 320x240, -b sets the recording bitrate, -r sets the frame rate to 15fps, -i gives the input device, -vcodec sets the output format.

Press Q to stop recording or you can specify the recording time with the -t option like -t 00:1:30

mencoder video.avi lavc -lavcopts vcodec=mpeg4:vbitrate=800 newvideo.avi
2009-06-19 17:30:46
User: dcabanis
1

Encode video.avi into newvideo.avi using the libav codec to produce an MPEG4 file with a bitrate of 800

mencoder -tv device=/dev/video1 tv:// -ovc copy -o video.avi
2009-06-19 17:21:33
User: dcabanis
1

video.avi is the resulting file. Press Ctrl+c to stop the recording. You can change the OVC option to another to record into a different format.

sed -n '/START/,${/STOP/q;p}'
2009-06-19 15:27:36
User: mungewell
Functions: sed
Tags: sed grep
3

GNU Sed can 'address' between two regex, but it continues parsing through to the end of the file. This slight alteration causes it to terminate reading the input file once the STOP match is made.

In my example I have included an extra '/START/d' as my 'start' marker line contains the 'stop' string (I'm extracting data between 'resets' and using the time stamp as the 'start').

My previous coding using grep is slightly faster near the end of the file, but overall (extracting all the reset cycles in turn) the new SED method is quicker and a lot neater.

ga
:!start c:\progra~1\Opera\opera.exe http://www.commandlinefu.com
:r! echo %
2009-06-19 12:17:28
User: miccaman
Functions: echo
Tags: vim
1

insert filename

Normal mode: "%p

Insert mode: %

:source ~/.vimrc
:set nomore :argdo %s/foo/bar/g | update
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
2009-06-19 10:23:38
User: plasticdoc
Functions: sed
Tags: Linux sed dpkg
4

will show:

installed linux headers, image, or modules: /^ii/!d

avoiding current kernel: /'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d

only application names: s/^[^ ]* [^ ]* \([^ ]*\).*/\1/

avoiding stuff without a version number: /[0-9]/!d

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
2009-06-19 10:11:00
User: plasticdoc
Functions: sed sudo xargs
6

will purge:

only installed apps: /^ii/!d

avoiding current kernel stuff: /'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d

using app names: s/^[^ ]* [^ ]* \([^ ]*\).*/\1/

avoiding stuff without a version number: /[0-9]/!d

gdiff --unified=10000 input.file1 inpute.file2 | egrep -v "(^\+[a-z]|^\-[a-z])"| sort > outputfile.sorted
2009-06-18 20:35:00
User: slashdot
Functions: egrep sort
-1

This commands will make it easier to select only common items between two files being compared. If your lines start with things other than lowercase a-z, adjust this Regex appropriately. Number of lines in the output has been set to no more than 10000, and should be adjusted as needed.

iptables -t nat -A PREROUTING -p tcp --dport [port of your choosing] -j REDIRECT --to-ports 22
2009-06-18 17:38:59
User: brizznown
Functions: iptables
9

Stuck behind a restrictive firewall at work, but really jonesing to putty home to your linux box for some colossal cave? Goodness knows I was...but the firewall at work blocked all outbound connections except for ports 80 and 443. (Those were wide open for outbound connections.) So now I putty over port 443 and have my linux box redirect it to port 22 (the SSH port) before it routes it internally. So, my specific command would be:

iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 22

Note that I use -A to append this command to the end of the chain. You could replace that with -I to insert it at the beginning (or at a specific rulenum).

My linux box is running slackware, with a kernel from circa 2001. Hopefully the mechanics of iptables haven't changed since then. The command is untested under any other distros or less outdated kernels.

Of course, the command should be easy enough to adapt to whatever service on your linux box you're trying to reach by changing the numbers (and possibly changing tcp to udp, or whatever). Between putty and psftp, however, I'm good to go for hours of time-killing.

cut -d: -f1 /etc/passwd | grep -vE "#" | xargs -i{} crontab -u {} -l
2009-06-18 16:49:52
User: hoberion
Functions: crontab cut grep xargs
1

additionally use "find /etc/cron*" for cronscripts

xmllint --format <filename> > <output file>
2009-06-18 15:00:30
User: topperge
4

Sometimes you're trying to read through an xml file to determine whats wrong with it and a tool had removed all the linebreaks. xmllint will go ahead and make it pretty for you.

shred -vzu /tmp/junk-file-to-be-shredded
curl -s http://defekt.nl/~jelle/pubkey.asc | gpg --import
2009-06-18 11:26:03
User: wires
Functions: gpg
2

imports a public key from the web. I know this by head.. but useful nevertheless

init=/bin/bash; mount -o remount,rw /
2009-06-18 08:51:24
User: m03hr3
Functions: init mount
2

Appended to grub boot parameters ... gives shell ... password recovery

grep -v "^\W$" <filename>
2009-06-18 08:17:22
User: nikc
Functions: grep
Tags: grep non-empty
0

I had some trouble removing empty lines from a file (perhaps due to utf-8, as it's the source of all evil), \W did the trick eventually.

find directory -size +nnn
ls -s | sort -nr | more
dtach -c /tmp/wires-mc mc
2009-06-17 22:18:25
User: wires
5

Starts midnightcommander and allows you to detach the console; use ctrl-\ to detach

Then at a later time you can reconnect using

dtach -a /tmp/wires-mc

In my experience dtach works much better for programs like irssi, mutt, mc, aptitude than screen does.

cp `ls -x1tr *.jpg | tail -n 1` newest.jpg
2009-06-17 20:32:04
User: Psychodad
Functions: cp tail
1

search the newest *.jpg in the directory an make a copy to newest.jpg. Just change the extension to search other files. This is usefull eg. if your webcam saves all pictures in a folder and you like the put the last one on your homepage. This works even in a directory with 10000 pictures.

grep -2 -iIr "err\|warn\|fail\|crit" /var/log/*
2009-06-17 19:41:04
User: miketheman
Functions: grep
6

Using the grep command, retrieve all lines from any log files in /var/log/ that have one of the problem states

dd if=/dev/zero of=testfile.txt bs=1M count=10
2009-06-17 17:06:16
User: mstoecker
Functions: dd
Tags: dd size test file
1

This will create a 10 MB file named testfile.txt. Change the count parameter to change the size of the file.

As one commenter pointed out, yes /dev/random can be used, but the content doesn't matter if you just need a file of a specific size for testing purposes, which is why I used /dev/zero. The file size is what matters, not the content. It's 10 MB either way. "Random" just referred to "any file - content not specific"