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,856 results
\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
114

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
2

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
`find . -iname ".svn" -type d | sed -e "s/^/rm -rfv /g"`
whois cmd.fu;whois cmdfu.com|grep -i cmdfu
2009-02-19 08:57:50
User: axelabs
Functions: whois
-11

It would be nice if commandlinefu.com had a better domain name. Will they pick one of the above; We'll see.

mount -t cd9660 /dev/`mdconfig -a -t vnode -f discimg.iso` /cdrom
2009-02-19 06:31:14
User: mulad
Functions: mount
-1

FreeBSD version of the Linux command

mount discimg.iso /cdrom -o loop

The "mdconfig" command creates a device and prints the name (typically "md0"). Just append that to "/dev/" and mount.

Once unmounted, you can unconfigure the device with

mdconfig -d -u 0

using "0" for md0, "1" for md1, etc.

tail -n 20 ~/Library/Logs/FileSyncAgent.log
2009-02-19 05:05:21
User: sacrilicious
Functions: tail
-3

tail would be considered dull, but pair this with being able to push out unix commands over ARD, and life gets easier. (Same can be said for my TimeMachine scrape command, http://xrl.us/begrzb)

netstat -alpn | grep :80 | awk '{print $4}' |awk -F: '{print $(NF-1)}' |sort | uniq -c | sort -n
mplayer $(ls -t /tmp/Flash*|head -1)
2009-02-19 04:38:40
User: mulad
Functions: ls
1

Many sites with Flash video players will download video files to /tmp on Linux, with temporary filenames like "FlashbGTHm4". These will often play in mplayer, totem, or other movie playing software. You must first navigate to a video page, let it start loading, and then pause playback.

syslog -F '$Time $Message' -k Sender /System/Library/CoreServices/backupd -k Time ge -72h | tail -n 30
2009-02-19 04:33:25
User: sacrilicious
Functions: tail
0

While they are few config options and even fewer useful details regarding what actually is being sent by the time machine 'backupd' process, this can at least tell you its doing something, how much it's doing, and exactly how often. Via macosxhints, http://xrl.us/begrwa, which in turn was via comments

find cvsdir -name Root -exec sed -i 's/oldserver/newserver/' {} \;
2009-02-19 04:31:13
User: mulad
Functions: find sed
0

If your CVS server has moved, here's a way to update your CVS Root files throughout your code tree without checking out a new copy of your files.

apt-get () { [ "$1" = source ] && (command apt-get "$@";true) || sudo apt-get "$@" }
2009-02-19 04:17:24
User: mulad
Functions: apt command sudo
1

An apt-get wrapper function which will run the command via sudo, but will run it normally if you're only downloading source files.

This was a bit of an excuse to show off the framework of

cmd && echo true || echo false

...but as you can see, you must be careful about what is in the "true" block to make sure it executes without error, otherwise the "false" block will be executed.

To allow the apt-get return code to pass through, you need to use a more normal if/else block:

apt-get () { if [ "$1" = source ]; then command apt-get "$@"; else sudo apt-get "$@"; fi }