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,611 results
find . -depth -type d -exec du -s {} \; | sort -k1nr
2009-06-23 20:52:35
User: mohan43u
Functions: du find sort
Tags: sort find du
4

somewhat faster version to see the size of our directories. Size will be in Kilo Bytes. to view smallest first change '-k1nr' to '-k1n'.

readlink -f ../super/symlink_bon/ahoy
2009-06-23 17:46:08
User: rbs
Functions: readlink
11

readlink -f accepts a relative, noncanonical path and emits the corresponding canonical, absolute path.

tune2fs -O extents,uninit_bg,dir_index /dev/yourpartition
2009-06-23 17:42:01
User: starchox
Functions: tune2fs
5

Before doing this, back-up all data on any ext3 partitions that are to be converted to ext4.

After running previous command you MUST run fsck, is needed to return the filesystem to a consistent state.

fsck -pDf /dev/yourpartition

Edit /etc/fstab and change the 'type' from ext3 to ext4 for any partitions that are converted to ext4.

echo {0..1}{0..1}{0..1}{0..1}
2009-06-23 17:30:20
User: dennisw
Functions: echo
17

If you should happen to find yourself needing some binary numbers, this is a quickie way of doing it. If you need more digits, just add more "{0..1}" sequences for each digit you need. You can assign them to an array, too, and access them by their decimal equivalent for a quickie binary to decimal conversion (for larger values it's probably better to use another method). Note: this works in bash, ksh and zsh. For zsh, though, you'll need to issue a setopt KSH_ARRAYS to make the array zero-based.

binary=({0..1}{0..1}{0..1}{0..1})

echo ${binary[9]}
ls -1t | head -n10
2009-06-23 12:15:12
User: wires
Functions: head ls
Tags: ls
7

order the files by modification (thanks stanishjohnd) time, one file per output line and filter first 10

ditto --arch i386 doubleTwist.app doubleTwist_i386.app
2009-06-22 23:59:14
0

ditto can be used to "thinnify" Mach-O Universal binaries to separate unncessary code from your machine. For example, doubleTwist.app shaved off around 10 MB while Google Earth went from 112 MB to 62 MB after extracting only the i386 code from them.

ssh -L :: -L :: @
2009-06-22 19:12:48
User: starchox
Functions: ssh
10

Thankfully, the ssh command allows you to specify multiple tunnels through the same server in one command.

Remeber if you want a priviliged port on your machine, you must use root or sudo account.

wget -q --user=<username> --password=<password> 'https://updates.opendns.com/nic/update?hostname=your_opendns_hostname&myip=your_ip' -O -
2009-06-22 18:08:42
User: Alanceil
Functions: wget
5

Intended for dynamic ip OpenDNS users, this command will update your OpenDNS network IP.

For getting your IP, you can use one of the many one-liners here on commandlinefu.

Example:

I use this in a script which is run by kppp after it has successfully connected to my ISP:

---

#!/bin/bash

IP="`curl -s http://checkip.dyndns.org/ | grep -o '[[:digit:].]\+'`"

PW="hex-obfuscated-pw-here"

if [ "$IP" == "" ] ; then echo 'Not online.' ; exit 1

else

wget -q --user=topsecret --password="`echo $PW | xxd -ps -r`" 'https://updates.opendns.com/nic/update?hostname=myhostname&myip='"$IP" -O -

/etc/init.d/ntp-client restart &

fi

---

PS: DynDNS should use a similar method, if you know the URL, please post a comment. (Something with members.dyndns.org, if I recall correctly)

find . -name "*.sql" -print0 | wc -l --files0-from=-
2009-06-22 17:45:03
User: vincentp
Functions: find wc
Tags: find wc count line
2

This command gives you the number of lines of every file in the folder and its subfolders matching the search options specified in the find command. It also gives the total amount of lines of these files.

The combination of print0 and files0-from options makes the whole command simple and efficient.

multitail /var/log/messages /var/log/apache2/access.log /var/log/mail.info
2009-06-22 06:51:11
User: Neo23x0
4

It works like a "tail -f" on several files.

Use the number keys 0-9 to set a baseline in the numbered window.

Pressing "b" let you scroll back in one of the windows.

while true; do { $(which logger) -p local4.notice `free -m | grep Mem`; sleep 60; } done &
2009-06-22 00:29:53
User: Neo23x0
Functions: grep sleep which
3

Uses logger in a while loop to log memory statistics frequently into the local syslog server.

bind '"\e[24~"':"\"ps -elF;df -h;free -mt;netstat -lnpt;who -a\C-m"""
2009-06-21 23:57:20
User: Neo23x0
17

Command binds a set of commands to the F12 key.

Feel free to alter the dashboard according to your own needs.

How to find the key codes?

Type

read

Then press the desired key (example: F5)

^[[15~

Try

bind '"\e[15~"':"\"ssh su@ip-address\C-m"""

or

bind '"\e[16~"':"\"apachectl -k restart\C-m"""
!*
2009-06-21 17:58:01
User: Neo23x0
78

!* is all of the arguments to the previous command rather than just the last one.

This is useful in many situations.

Here's a simple example:

vi cd /stuff

oops!

[exit vi, twice]

!*

expands to: cd /stuff

# cd $partition; dd if=/dev/zero of=ShredUnusedBlocks bs=512M; shred -vzu ShredUnusedBlocks
2009-06-21 14:17:22
User: mpb
Functions: cd dd shred
8

This command securely erases all the unused blocks on a partition.

The unused blocks are the "free space" on the partition.

Some of these blocks will contain data from previously deleted files.

You might want to use this if you are given access to an old computer and you do not know its provenance.

The command could be used while booted from a LiveCD to clear freespace space on old HD.

On modern Linux LiveCDs, the "ntfs-3g" system provides ReadWrite access to NTFS partitions thus enabling this method to also be used on Wind'ohs drives.

NB depending on the size of the partition, this command could take a while to complete.

sudo sed 's/\o0/\n/g' "/proc/$(pidof -x firefox)/environ" ;# replace firefox
watch -t -n1 "date +%T|figlet"
2009-06-21 01:02:37
User: dennisw
Functions: watch
43

This command displays a clock on your terminal which updates the time every second. Press Ctrl-C to exit.

A couple of variants:

A little bit bigger text:

watch -t -n1 "date +%T|figlet -f big"

You can try other figlet fonts, too.

Big sideways characters:

watch -n 1 -t '/usr/games/banner -w 30 $(date +%M:%S)'

This requires a particular version of banner and a 40-line terminal or you can adjust the width ("30" here).

sudo pmset -a hibernatemode 1
2009-06-20 22:52:10
Functions: sudo
-1

sudo pmset -a hibernatemode 1 sets hiberate on.

sudo pmset -a hibernatemode 0 sets hibernate off.

from the pmset man page:

0001 (bit 0) enables hibernation; causes OS X to write memory state to

hibernation image at sleep time. On wake (without bit 1 set) OS X will

resume from the hibernation image. Bit 0 set (without bit 1 set) causes

OS X to write memory state and immediately hibernate at sleep time.

I often change my MacBook's sleep state. So I created a function for bash to make it a little easier.

Usage:

hibernate (on | off)

"hibernate on" will set your laptop to hibernate if the lid is closed.

"hibernate off" will set your laptop to sleep if the lid is closed.

### note : "proper" indentation isn't preserved on the website

function hibernate()

{

case "${1}" in

on)

sudo pmset -a hibernatemode 1

echo Hibernate mode on.

;;

off)

sudo pmset -a hibernatemode 0

echo Hiberate mode off.

;;

*)

echo "I'm sorry Dave, but I can't do that."

;;

esac

}

To make things easier, add the proper line in your /etc/sudoers file so that your user may invoke pmset without a password. Here's how mine looks:

bwayne luna = NOPASSWD: /usr/bin/pmset

Don't forget that you must edit sudoers with `sudo visudo` from Terminal.app, and not by another text editor.

Sorry this is so Mac OS specific.

svn status | grep '!' | sed 's/!/ /' | xargs svn del --force
mplayer dvdnav:// -dvd-device foo.img -mouse-movements
unrar p -inul foo.rar|mplayer -
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