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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

All commands from sorted by
Terminal - All commands - 12,181 results
awk '/^--- Day changed (.*)/ {st=""; for (i=0;i<ar[date];i++) {st=st"*"} print date" "st; date=$7"-"$5"-"$6} /> emergency/ {ar[date]++} END {st=""; for (i=0;i<ar[date];i++) {st=st"*"}; print date" "st}' #engineyard.log
2010-02-24 23:10:03
User: MarcoN
Functions: awk
-2

This awk command prints a histogram of the number of times 'emergency' is the first word in a line, per day, in an irssi (IRC client) log file.

passpdf(){ for i; do qpdf --password=<YOUR PASSWD> --decrypt "$i" "new$i"; done; }
2010-02-25 01:21:00
User: sputnick
Tags: pdf qpdf
-2

You should install qpdf.

That way, you can have a copy without any password required.

find -D rates . -name "*.mp3" -type f
alias cn='cat > /dev/null'
2011-09-16 00:00:28
Functions: alias
-2

cn stands for "Cat Null"

.

The idea is that sometimes you run across something on maybe a webpage - like commandlinefu - that you want to try out on your terminal. You could put a '#' in and then paste it, but what if it is several lines?

.

This command will echo the pasted characters to the screen and divert them to the bit bucket.

.

Put this simple alias in your .bashrc, hit cn, paste away, and hit a ctrl+c or a ctrl+d when you are done to get your prompt back.

for i in `grep -n "SomeRegEx" foo.txt | sed 's/:/ /' | awk '{print $1}'`; do echo "head -n `echo "$i+4" | bc` foo.txt | tail -n 5"; done > headsandtails.sh
2010-07-01 05:48:02
User: reklis
Functions: awk echo sed
-2

Finds the line number matching the regex, then passes that to BC for some math, passes that to head, and uses tail to trim off the unwanted section at the top. The whole thing is spit out to a script that can then be shared or run. Comes in handy for reading select sections from error logs.

cgrep() { GREP_COLOR="1;3$((RANDOM%6+1))" grep --color=always "$@" }
2011-03-04 18:45:58
User: derekschrock
Functions: grep
Tags: grep
-2

Randomize GNU grep's colors 31-36 excluding black and white.

alias acs='apt-cache search'
wget -H -r ?level=1 -k -p http://www.domain.com/folder/
2009-12-27 05:23:27
User: svnlabs
Functions: wget
-2

wget ? server to server files transfer

kded --version | awk -F: 'NR == 2 {print $2}' | sed 's/\s\+//g'
2012-01-17 17:44:23
User: voyeg3r
Functions: awk sed
-2

kded --version return this

Qt: 3.3.8b

KDE: 3.5.10

KDE Daemon: $Id: kded.cpp 711061 2007-09-11 09:42:51Z tpatzig $

awk -F: ................. Awk Field separator

NR == 2 ................. Register Number, second line

{print $2} ............... second field

sed 's/\s\+//g' .......... remove one space or more \s\+ changing by nothing

sudo lsof -i -n | grep sshd | grep sshuser | grep :[PORT-RANGE] | grep -v IPv6 | awk -F\: '{print $2}' | grep -v http | awk -F" " '{print $1}'
2015-04-09 15:41:11
User: das_shark
Functions: awk grep sshd sudo
-2

gets network ports

only ones for the sshd service

only logged in a specific user (changed for public posting)

only in a specific localhost:port range

not IPv6

Only the part of the response after the ":" character

Only the part of the response before the 1st space

Output is just the rssh port

long_command; say I am all done
2010-01-27 19:03:01
User: haivu
Tags: audio osx
-2

I often run some command that takes a while to finish. By putting the say command afterward, I get an audio notification.

Please note that this command (say) only works on Mac OS X and not Linux.

ls | while read filename; do tar -czvf "$filename".tar.gz "$filename"; rm "$filename"; done
2010-03-29 08:10:38
User: Thingymebob
Functions: ls read rm tar
-2

Compresses each file individually, creating a $fileneame.tar.gz and removes the uncompressed version, usefull if you have lots of files and don't want 1 huge archive containing them all. you could replace ls with ls *.pdf to just perform the action on pdfs for example.

cryptmount -m <name>
2012-01-17 18:02:47
User: totti
-2

In order to create a new encrypted filing system managed by cryptmount,

you can use the supplied 'cryptmount-setup' program, which can be used

by the superuser to interactively configure a basic setup.

Alternatively, suppose that we wish to setup a new encrypted filing

system, that will have a target-name of "opaque". If we have a free

disk partition available, say /dev/hdb63, then we can use this directly

to store the encrypted filing system. Alternatively, if we want to

store the encrypted filing system within an ordinary file, we need to

create space using a recipe such as:

dd if=/dev/zero of=/home/opaque.fs bs=1M count=512

.

cryptmount --generate-key 32 opaque

.

cryptmount --prepare opaque

.

mke2fs /dev/mapper/opaque

.

cryptmount --release opaque

.

mkdir /home/crypt

.

cryptmount -m opaque

.

cryptmount -u opaque

For detail see sample output

netstat -anp | grep :80 | grep ESTABLISHED | wc -l
2015-04-10 19:32:31
User: krizzo
Functions: grep netstat wc
Tags: session
-2

This counts all established sessions on port 80. You can change :80 to any port number you want to check.

SEARCHPATH=/var/; find $SEARCHPATH -type d -print0 | xargs -0 du -s 2> /dev/null | sort -nr | sed 's|^.*'$SEARCHPATH'|'$SEARCHPATH'|' | xargs du -sh 2> /dev/null
2011-07-06 08:21:58
User: moogmusic
Functions: du find sed sort xargs
-2

This command lists all the directories in SEARCHPATH by size, displaying their size in a human readable format.

:%s/<control-VM>//g
2009-02-17 01:23:39
User: roliver
-2

Files saved on a windows machine use different ascii characters for lines turns. When viewing such files in VI the will most often have a ^M(control-VM) character at the end of each line. This command will remove all occurrences of that character

man emacs
2011-08-17 00:37:00
User: cwimmer
Functions: man
-2

Emacs backs up previous versions by default.

echo $[RANDOM % 100] # range 0-99
2013-05-25 19:02:00
User: anapsix
Functions: echo
-2

use it to stagger cronjob or to get a random number

increase the range by replacing 100 with your own max value

i="0"; while [ $i -lt 5 ] ; do xpenguins & i=$[$i+1] ; done
2009-12-27 21:05:33
User: eastwind
-2

just replace the number 5 with the variable you want , and the xpenguins software with the one you want

echo -n "String to MD5" | md5sum | cut -f1 -d' '
2011-03-05 03:53:09
User: xakon
Functions: cut echo md5sum
-2

Nothing special about hashing here, only the use of cut, I think, could result at fewer keystrokes.

ls -ltr |grep 'May 12'|awk '{print $9;}'|xargs rm -v
find -iname '*.flac' | sed 's:/[^/]*$::' | uniq
2009-03-24 13:26:31
User: ar
Functions: find sed
-2

Run this in your music folder, or give the path directly after "find".

The sed pattern filters away the basename.

html2text http://checkip.dyndns.org | grep -i 'Current IP Address:'|cut -d' ' -f4
2009-08-06 11:30:32
User: adonis28850
Functions: cut grep
-2

The same as the other user, but smarter, using -d and -f

dd if=/dev/zero of=filename.file bs=1024 count=10240
2012-01-19 08:38:14
User: harpo
Functions: dd
-2

Command to create a dummy file (full of nulls). Useful for testing e.g. file transfers when no file is at hand.

bs = blocksize, count = filesize in kilobytes

unrar x *.rar
2010-12-08 21:50:23
User: yababay
-2

unrar e ... extracts all files into current directory, without archived subdirectories order. unrar x is more accurate command.