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,583 results
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
sed -n -e '/postfix\/smtp\[.*status=sent/s/^.*to=<\([^>]*\).*$/\1/p' /var/log/mail.log | sort -u
netstat -an | grep -i listen
2009-02-19 19:27:49
User: scubacuda
Functions: grep netstat
-2

From 'man netstat'

"netstat -i | -I interface [-abdnt] [-f address_family] [-M core] [-N system]

Show the state of all network interfaces or a single interface

which have been auto-configured (interfaces statically configured

into a system, but not located at boot time are not shown). An

asterisk (``*'') after an interface name indicates that the

interface is ``down''. If -a is also present, multicast

addresses currently in use are shown for each Ethernet interface

and for each IP interface address. Multicast addresses are shown

on separate lines following the interface address with which they

are associated. If -b is also present, show the number of bytes

in and out. If -d is also present, show the number of dropped

packets. If -t is also present, show the contents of watchdog

timers."

. ./cd_some_dir.sh
2009-02-19 19:02:21
User: jcgam69
-16

The script contains a single command:

cd /a/long/directory/path/i/can/never/remember

x11vnc -display :0 -auth $(ps -ef|awk '/xauth/ {print $15}'|head -1) -forever -bg &
2009-02-19 18:55:54
User: nottings
Functions: ps
4

the $15 may change for you depending on your distro, etc...

ps aux | grep -i firefox | grep -v grep | awk '{print $2}' | xargs -t -i kill -9 {}
2009-02-19 18:50:00
User: blackdude
Functions: awk grep kill ps xargs
-7

This is a nice way to kill processes.. the example here is for firefox!!! substitute firefox for whatever the process name is...

while read line; do echo "$(date),$(hostname),$line"; done < somefile.txt
nmap -sS -O -oX /tmp/nmap.xml 10.1.1.0/24 -v -v && perl nmap2nagios.pl -v -r /tmp/10net.xml -o /etc/nagios/10net.cfg
set -o vi
xxcopy x:\folder1 y:\folder2 /s /h /tca /tcc /tcw /yy
cat somefilehere.txt | ssh-xfer nametocallfile.txt -
2009-02-19 18:35:33
User: scubacuda
Functions: cat
0

from http://matt.ucc.asn.au/ssh-xfer/

"ssh-xfer is a hackish but handy way of transferring files from remote hosts to your local computer. Firstly, you need to run a slightly modified SSH authentication agent program on your local computer. Patches are available for both OpenSSH and PuTTY, see below. If you haven't used a SSH agent program before, this article seems to be reasonable, or you can look at the OpenSSH/PuTTY docs.

You don't need any modifications to your ssh client or server programs - only the modified SSH authentication agent, and the extra ssh-xfer program."

^r in bash begins a reverse-search-history with command completion
2009-02-19 18:17:54
User: abcde
Functions: bash command
15

In the sample output, I pressed ctrl+r and typed the letters las. I can't imagine how much typing this has saved me.

grep -v "^$" file
2009-02-19 18:11:58
User: hkyeakley
Functions: grep
0

To save the result, redirect the output to another file.

grep -v "^$" file1 > file2
while [ 0 ]; do echo -e "HTTP/1.1 302 Found\nLocation: http://www.whatevs.com/index.html" | nc -vvvv -l -p 80; done
2009-02-19 18:04:01
User: wwest4
Functions: echo
4

any HTTP requests to the machine on the specified port will be redirected to http://www.whatevs.com... quick, dirty, works fine for sites w/

egrep '^[^#]' some_file
alias ds='dig +noauthority +noadditional +noqr +nostats +noidentify +nocmd +noquestion +nocomments'
2009-02-19 17:32:56
Functions: alias
4

Turn off almost all of dig's output except for what you'd see in a zone file. This can also be put into ~/.digrc.

ionice -c3 find /
2009-02-19 17:23:12
User: mkc
Functions: find
5

This command is somewhat similar to 'nice', but constrains I/O usage rather than CPU usage. In particular, the '-c3' flag tells the OS to only allow the process to do I/O when nothing else is pending. This dramatically increases the responsiveness of the rest of the system if the process is doing heavy I/O.

There's also a '-p' flag, to set the priority of an already-running process.

perl -e 'print 1, 2, 3' > /dev/full
2009-02-19 17:08:13
User: mkc
Functions: perl
8

The Linux /dev/full file simulates a "disk full" condition, and can be used to verify how a program handles this situation.

In particular, several programming language implementations do not print error diagnostics (nor exit with error status) when I/O errors like this occur, unless the programmer has taken additional steps. That is, simple code in these languages does not fail safely. In addition to Perl, C, C++, Tcl, and Lua (for some functions) also appear not to fail safely.