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,490 results
firefox -no-remote -P
2009-03-09 07:03:55
User: asmoore82
14

even when another instance is already open.

Great for testing purposes when you need

to be 2 people at once on the same site.

wget -S -O/dev/null "INSERT_URL_HERE" 2>&1 | grep Server
2009-03-09 06:54:54
User: asmoore82
Functions: grep wget
8

the good:

Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.4 with Suhosin-Patch

the bad:

Server: Microsoft-IIS/6.0

and the ugly:

Server: Apache/2.2.10 (Win32) mod_ssl/2.2.10 OpenSSL/0.9.8i PHP/5.2.6

i="8uyxVmdaJ-w";mplayer -fs $(curl -s "http://www.youtube.com/get_video_info?&video_id=$i" | echo -e $(sed 's/%/\\x/g;s/.*\(v[0-9]\.lscache.*\)/http:\/\/\1/g') | grep -oP '^[^|,]*')
2009-03-09 03:57:44
User: lrvick
Functions: echo grep sed
56

This is the result of a several week venture without X. I found myself totally happy without X (and by extension without flash) and was able to do just about anything but watch YouTube videos... so this a the solution I came up with for that. I am sure this can be done better but this does indeed work... and tends to work far better than YouTube's ghetto proprietary flash player ;-)

Replace $i with any YouTube ID you want and this will scrape the site for the _real_ URL to the full quality .FLV file on Youtube's server and will then will hand that over to mplayer (or vlc or whatever you want) to be streamed.

In some browsers you can replace $i with just a % or put this in a shell script so all YouTube IDs can be handed directly off to your media player of choice for true streaming without the need for Flash or a downloader like clive. (I do however fully recommend clive if you wish to archive videos instead of streaming them)

If any interest is shown I would be more than happy to provide similar commands for other sites. Most streaming flash players use similar logic to YouTube.

Edit: 05/03/2011 -

Updated line to work with current YouTube. It could be a lot prettier but I will probably follow up with another update when I figure out how to get rid of that pesky Grep. Sed should take that syntax... but it doesn't.

Original (no longer working) command:

mplayer -fs $(echo "http://youtube.com/get_video.php?$(curl -s $youtube_url | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p")")

sudo cat /proc/kcore | strings | awk 'length > 20' | less
2009-03-09 02:19:47
User: nesquick
Functions: awk cat strings sudo
Tags: cat ram strings
15

This command lets you see and scroll through all of the strings that are stored in the RAM at any given time. Press space bar to scroll through to see more pages (or use the arrow keys etc).

Sometimes if you don't save that file that you were working on or want to get back something you closed it can be found floating around in here!

The awk command only shows lines that are longer than 20 characters (to avoid seeing lots of junk that probably isn't "human readable").

If you want to dump the whole thing to a file replace the final '| less' with '> memorydump'. This is great for searching through many times (and with the added bonus that it doesn't overwrite any memory...).

Here's a neat example to show up conversations that were had in pidgin (will probably work after it has been closed)...

sudo cat /proc/kcore | strings | grep '([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\})'

(depending on sudo settings it might be best to run

sudo su

first to get to a # prompt)

echo "Whatever you need" | sudo tee [-a] /etc/system-file.cfg
2009-03-09 01:33:31
User: asmoore82
Functions: echo sudo tee
4

only for sudo-style systems.

Use this construct instead of I/O re-directors ``>'' or ``>>'' because

sudo only elevates the commands and *not* the re-directors.

***warning: remember that the `tee` command will clobber

file contents unless it is given the ``-a'' argument

Also, for extra security, the "left" command is still run unprivileged.

cat /path/to/7z.sfx /path/to/archive > archive.exe
2009-03-09 00:54:33
User: asmoore82
Functions: cat
4

using `cat` under *NIX - just because you help manage M$ Windoze

*doesn't* mean you should have to resort to using it!

You can also make custom win32 installers with the 7zip "extras" package:

cat /path/to/7zSD.sfx /path/to/config.txt /path/to/archive > setup.exe
export PS1="${PS1%\\\$*}"' \t \$ '
dd if=/dev/cdrom | pv -s 700m | md5sum | tee test.md5
2009-03-09 00:11:42
User: asmoore82
Functions: dd md5sum tee
9

[re]verify those burned CD's early and often - better safe than sorry -

at a bare minimum you need the good old `dd` and `md5sum` commands,

but why not throw in a super "user-friendly" progress gauge with the `pv` command -

adjust the ``-s'' "size" argument to your needs - 700 MB in this case,

and capture that checksum in a "test.md5" file with `tee` - just in-case for near-future reference.

*uber-bonus* ability - positively identify those unlabeled mystery discs -

for extra credit, what disc was used for this sample output?

find . -iname "*wav" > step1 ; sed -e 's/\(^.*\)wav/\"\1wav\" \"\1mp3\"/' step1 > step2 ; sed -e 's/^/lame /' step2 > step3 ; chmod +x step3 ; ./step3
find -name ".svn" -exec rm -rf {} \;
cd !$
readom dev=/dev/scd0 f=/path/to/image.iso
2009-03-08 13:21:23
User: atoponce
51

Many like to use 'dd' for creating CD/DVD iso images. This is bad. Very bad. The reason this is, is 'dd' doesn't have any built-in error checking. So, you don't know if you got all the bits or not. As such, it is not the right tool for the job. Instead, 'reaom' (read optical media) from the wodim package is what you should be using. It has built-in error checking. Similarly, if you want to burn your newly creating ISO, stay away from 'dd', and use:

wodim -v -eject /path/to/image.iso
find /dir_name -mtime +5 -exec rm {} \
2009-03-08 12:03:44
User: eleffie
Functions: find rm
Tags: delete
5

This command will delete files i a given path (/dir_name) , which older than given time in days (-mtime +5 will delete files older than five days.

perl -0777 -ne 'print "$1\n" while /word-a(.*?)word-b/gs' filename.txt
2009-03-08 11:47:18
User: eleffie
Functions: perl
3

This command will display all lines between 2 patterns: word-a and word-b

useful for grepping command outputs from file

find . -type f -iname '*.wmf' | while read FILE; do FILENAME="${FILE%.*}"; wmf2svg -o ${FILENAME}.svg $FILE; done
2009-03-07 22:21:01
User: atoponce
Functions: find read
3

This assumes you have the package installed necessary for converting WMF files. On my Ubuntu box, this is libwmf-bin. I used this command, as libwmf is not on my wife's iMac, so I archived the directories containing the WMF files from OS X, ran them on my Ubuntu box, archived the resulting SVGs, and sent them back to her. Quick, simple and to the point.

Searches directories recursively looking for extensions ignoring case. This is much more readable and clean than -exec for find. The while loop also gives further flexibility on complex logic. Also, although there is 'wmf2svg --auto', it expects lowercase extensions, and not uppercase. Because I want to ignore case, I need to use the -o option instead.

Works in ZSH and BASH. Haven't tested in other shells.

cuebreakpoints "$2" | shnsplit -o flac "$1"
2009-03-07 21:04:45
User: photraip
5

Converts a single FLAC file with associated cue file into multiple FLAC files.

Takes two arguments: the name of the FLAC file and and the name of the cue file.

Example: flacAlbumToFiles foo.flac foo.cue

Requires:

- cuetools

- shntools

ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed "/^ 0.0 /d"
echo "The date is: $(date +%D)"
2009-03-07 15:51:59
User: atoponce
Functions: echo
74

This is a simple example of using proper command nesting using $() over ``. There are a number of advantages of $() over backticks. First, they can be easily nested without escapes:

program1 $(program2 $(program3 $(program4)))

versus

program1 `program2 \`program3 \`program4\`\``

Second, they're easier to read, then trying to decipher the difference between the backtick and the singlequote: `'. The only drawback $() suffers from is lack of total portability. If your script must be portable to the archaic Bourne shell, or old versions of the C-shell or Korn shell, then backticks are appropriate, otherwise, we should all get into the habit of $(). Your future script maintainers will thank you for producing cleaner code.

nmap -sP -T Insane 192.168.1.1-254
cat <( command1 arg arg ) <( command2 arg ) ...
2009-03-07 04:33:12
User: Pistos
Functions: cat
2

Concatenate the stdout of multiple commands.

ls -1 *.part1.rar | xargs -d '\n' -L 1 unrar e
find /usr/lib -maxdepth 1 -type l -print0 | xargs -r0 du -Lh
2009-03-07 00:17:45
User: starchox
Functions: du find xargs
3

You also can sum the file usage of all files

find /usr/lib -maxdepth 1 -type l -print0 | xargs -r0 du -Lch
INFILE=/path/to/your/backup.img; MOUNTPT=/mnt/foo; PARTITION=1; mount "$INFILE" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "$INFILE" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ]
6

Suppose you made a backup of your hard disk with dd:

dd if=/dev/sda of=/mnt/disk/backup.img

This command enables you to mount a partition from inside this image, so you can access your files directly.

Substitute PARTITION=1 with the number of the partition you want to mount (returned from sfdisk -d yourfile.img).

ttmkfdir mkfontdir fc-cache /usr/share/fonts/miscttf
2009-03-06 21:28:17
User: starchox
Functions: fc-cache
5

First you have to create a directory in your system, where the fonts will be stored, and copy them.

sudo mkdir /usr/share/fonts/miscttf; sudo cp *.ttf /usr/share/fonts/miscttf

After recharge cache with the command