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,492 results
^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
3

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.

echo "8000000/(20*6*86400)" | bc -l
yum install httpd
2009-02-19 16:59:51
User: sandrader
Functions: install
-30

This command install Apache 2 and other utilities on CentOS

\rm somefile
2009-02-19 16:55:54
User: mkc
3

The backslash avoids any 'rm' alias that might be present and runs the 'rm' command in $PATH instead.

In a misguided attempt to be more "friendly", some Linux distributions (or sites/etc.) alias 'rm' to 'rm -i'. Unfortunately, this trains users to expect that files won't actually be deleted until they okay it. This expectation will fail with catastrophic results when they use other distributions, move to other sites, etc., and doesn't really even work 100% even with the alias. It's too late to fix 'rm', but '\rm' should work everywhere (under bash).

ffmpeg -i video.avi -f mp3 audio.mp3
for i in $(cat listofservers.txt); do konsole --new-tab -e ssh $i; done
2009-02-19 16:32:36
User: nottings
Functions: cat ssh
4

creates a new tab for each of N servers in listofservers.txt and ssh's to said servers

then, try the "send to all sessions" feature of konsole to do the same work on all servers at the same time. BIG time saver, but be careful!

sudo hdparm -Tt /dev/sda
time { <command1> ; <command2> ; <command...> ; }
2009-02-19 16:23:42
User: dizzgo
Functions: time
5

The last ; is important. example:

time { rm -rf /folder/bar && mkdir -p /folder/bar ; echo "done" ; }

command is a bash builtin

ex some_file "+set ff=unix fileencoding=utf-8" "+x"
2009-02-19 16:23:21
User: nottings
Functions: ex
5

converts encoding of a file to unix utf-8

useful for data files that contain what would be usable ascii text but are encoded as mpeg or some other encoding that prevents you from doing common manipulations like 'sed'

ps -o ppid= <given pid> | xargs ps -p
lsof -p $(netstat -ltpn|awk '$4 ~ /:80$/ {print substr($7,1,index($7,"/")-1)}')| awk '$9 ~ /access.log$/ {print $9| "sort -u"}'
2009-02-19 16:11:54
User: rjamestaylor
Functions: awk netstat
2

Ever logged into a *nix box and needed to know which webserver is running and where all the current access_log files are? Run this one liner to find out. Works for Apache or Lighttpd as long as CustomLog name is somewhat standard. HINT: works great as input into for loop, like this:

for i in `lsof -p $(netstat -ltpn|awk '$4 ~ /:80$/ {print substr($7,1,index($7,"/")-1)}')| awk '$9 ~ /access.log$/ {print $9| "sort -u"}'` ; do echo $i; done

Very useful for triage on unfamiliar servers!

nohup <command> &
2009-02-19 14:45:04
User: gnawa
Functions: nohup
5

puts command in background and sends its output to nohup.out file

it will not die if you log out fromyour shell session ;-)

less +F somelogfile
2009-02-19 14:33:46
User: adamm9
Functions: less
110

Using +F will put less in follow mode. This works similar to 'tail -f'. To stop scrolling, use the interrupt. Then you'll get the normal benefits of less (scroll, etc.).

Pressing SHIFT-F will resume the 'tailling'.

echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
2009-02-19 14:15:31
User: stinger
Functions: echo
1

above line in .bash_profile will give you window title in putty or terminal client when you login to your remote server

lsof | grep "/var/spool/mail/"
echo 1+1|bc
du -sm *|sort -rn|head -10
links --dump 1 http://localhost/server-status|grep ^[0-9]|awk 'BEGIN {print "Seconds, PID, State, IP, Domain, TYPE, URL\n--"} $4 !~ /[GCRK_.]/ {print $6, $2, $4, $11, $12, $13 " " $14|"sort -n"}'
2009-02-19 13:06:44
User: rjamestaylor
6

Ever need to know why Apache is bogging down *right now*? Hate scanning Apache's Extended server-status for the longest running requests? Me, too. That's why I use this one liner to quickly find suspect web scripts that might need review.

Assuming the Extended server-status is reachable at the target URL desired, this one-liner parses the output through elinks (rendering the HTML) and shows a list of active requests sorted by longest running request at the bottom of the list. I include the following fields (as noted in the header line):

Seconds: How long the request is alive

PID: Process ID of the request handler

State: State of the request, limited to what I think are the relevant ones (GCRK_.)

IP: Remote Host IP making the request

Domain: Virtual Host target (HTTP/1.1 Host: header). Important for Virtual Hosting servers

TYPE: HTTP verb

URL: requested URL being served.

Putting this in a script that runs when triggered by high load average can be quite revealing. Can also capture "forgotten" scripts being exploited such as "formmail.pl", etc.

svn st | grep ^\? | awk '{print $2}' | xargs svn add