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,925 results
ctrl + r
2009-02-20 13:28:19
User: sixtus
6

This is not actually a command, it's just a keyboard shortchut. But a very useful one.

find / -name *~ -delete
pushd +2; pushd -2
2009-02-20 12:26:18
User: betsubetsu
1

'pushd +1' is equivalent to 'pushd'. Can be 'pushd +3' or more generaly 'pushd +N'. Can also be 'pushd -N'.

More description in 'man bash'.

FLOOR=0; RANGE=10; number=0; while [ "$number" -le $FLOOR ]; do number=$RANDOM; let "number %= $RANGE"; done; echo $number
2009-02-20 09:33:56
User: raphink
Functions: echo
Tags: bash
-1

This one-liner outputs a random number between the values given for FLOOR and RANGE.

xset b off
2009-02-20 07:40:14
User: dizzgo
7

Disable the annoying beep in xterm

<alt> + <print screen/sys rq> + <R> - <S> - <E> - <I> - <U> - <B>
2009-02-20 07:28:56
User: dizzgo
99

If the machine is hanging and the only help would be the power button, this key-combination will help to reboot your machine (more or less) gracefully.

R - gives back control of the keyboard

S - issues a sync

E - sends all processes but init the term singal

I - sends all processes but init the kill signal

U - mounts all filesystem ro to prevent a fsck at reboot

B - reboots the system

Save your file before trying this out, this will reboot your machine without warning!

http://en.wikipedia.org/wiki/Magic_SysRq_key

w3m
du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e'
/lib/ld-linux.so.2 =(echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | gcc -x c -o /dev/stdout -)
2009-02-20 06:06:29
User: mrtheplague
Functions: c++ echo gcc
6
/lib/ld-linux.so.2

is the runtime linker/loader for ELF binaries on Linux.

=(cmd) is a zsh trick to take the output for the command "inside" it and save it to a temporary file.

echo -e 'blah' | gcc -x c -o /dev/stdout -

pipes the C source to gcc. -x c tells gcc that it's compiling C (which is required if it's reading from a pipe). -o /dev/stdout - tells it to write the binary to standard output and read the source from standard input.

because of the the =() thing, the compiled output is stashed in a tempfile, which the loader then runs and executes, and the shell tosses the tempfile away immediately after running it.

mkfifo /tmp/fifo; cat /tmp/fifo | nc -l -p 1234 | tee -a to.log | nc machine port | tee -a from.log > /tmp/fifo
2009-02-20 04:00:15
User: prutser
Functions: cat mkfifo tee
27

Forwards localhost:1234 to machine:port, running all data through your chain of piped commands. The above command logs inbound and outbound traffic to two files.

Tip: replace tee with sed to manipulate the data in real time (use "sed -e 's/400 Bad Request/200 OK/'" to tweak a web server's responses ;-) Limitless possibilities.

this command is to see oracle instance process on linux server
php -i|grep -i max
2009-02-20 03:29:11
User: rjamestaylor
-2

Show the maximum settings in effect for PHP at the command line.

echo 1 > /proc/sys/kernel/sysrq; echo b > /proc/sysrq-trigger
2009-02-20 00:37:18
User: tsaavik
Functions: echo
7

This will cause your machine to INSTANTLY reboot. No un-mounting of drives or anything.

Very handy when something has gone horribly wrong with your server in that co-location facility miles away with no remote hands!

Suspect this works with all 2.2, 2.4 and 2.6 Linux kernels compiled with magic-syskey-request support.

if [[ `:$(cal);echo $_` == `date +%d` ]]; then ROTATE_MONTHLY_TABLES_SCRIPT;fi
2009-02-20 00:28:05
User: tsaavik
1

This is handy to just shove into a daily cron entry. If you do use cron, make sure to escape the %d with \%d or it will fail.

ps auxw |egrep "PID|process_to_look_at"
2009-02-20 00:17:43
User: tsaavik
Functions: ps
4

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1828 0.0 0.0 5396 476 ? Ss 2008 0:00 /usr/sbin/sshd

for i in `recode -l | cut -d" " -f 1`; do echo $i": ?" | recode utf-8..$i -s -p >> temp; done; vim temp
2009-02-20 00:14:33
User: saerdnaer
Functions: cut echo vim
0

If you have some textfile with an unknown encoding you can use this list to find out

check_disk -w 15% -c 10% $(for x in $(cat /proc/mounts |awk '{print $2}')\; do echo -n " -p $x "\; done)
grep lm /proc/cpuinfo
2009-02-19 22:48:47
User: bobbydale
Functions: grep
-2

If the 'lm' flag is present, then the CPU is 64-bit.

If no output, then CPU is 32-bit.

top -b -n 1 | awk '{if (NR <=7) print; else if ($8 == "D") {print; count++} } END {print "Total status D: "count}'
comm -1 -2 <(sort file1) <(sort file2)
cat foo.csv bar.csv | sort -t "," -k 2 | uniq
2009-02-19 20:23:03
User: rafeco
Functions: cat sort
2

The value for the sort command's -k argument is the column in the CSV file to sort on. In this example, it sorts on the second column. You must use some form of the sort command in order for uniq to work properly.

autossh -f -M 20000 -D 8000 somehost -N
2009-02-19 20:17:52
User: sud0er
10

This command will log you into somehost via SSH and then go into the background (-f). From there, you can point e.g. firefox at localhost:8000 as a SOCKS proxy. Autossh will use port 20000 and 20001 to send and receive test data on those ports to ensure the SSH tunnel is still running, and will try to re-start the tunnel if it goes down. Make sure you have ssh-agent running, or passwordless ssh keys distributed between the two hosts.

vncviewer -via root@your.dyndns.com 192.168.1.1
2009-02-19 20:14:37
2

The ip address is of the remote machine running the vncserver. Must log in first to the router then the VNC session. Very nice if you have open-wrt or dd-wrt on your router.

$(STEEP=300; sleep $STEEP; xmessage "Your tea is done") &
2009-02-19 20:10:49
User: sud0er
Functions: sleep
1

wrapping the snippet in $( )& puts the whole thing in the background so you don't tie up your login session.

lsof | awk '{print $1}' | sort | uniq -c | sort -rn | head