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
lomount -diskimage /path/to/your/backup.img -partition 1 /mnt/foo
4

Instead of calculating the offset and providing an offset option to mount, let lomount do the job for you by just providing the partition number you would like to loop mount.

ip addr|grep "inet "
2009-07-22 07:38:06
User: RickDeckardt
Functions: grep
-3

Shows a single line per interface (device), with its IPv4 settings.

Shorter command, better readability in output.

curl -u 'username' https://api.del.icio.us/v1/posts/all | sed 's/^.*href=//g;s/>.*$//g;s/"//g' | awk '{print $1}' | grep 'http'
2009-07-22 07:32:59
User: bubo
Functions: awk grep sed
2

a variation of avi4now's command - thanks by the way!

/sbin/ifconfig | grep inet | cut -f 2 -d ":" | cut -f 1 -d " " |egrep -v "^$"
while true ; do IFS="" read i; echo "$i"; sleep .01; done < <(tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]")
2009-07-22 03:59:07
Functions: dd echo grep read sleep tr true
Tags: color
2

Same as above but slooooow it down

/sbin/ifconfig | grep inet | cut -f 2 -d ":" | cut -f 1 -d " "
2009-07-21 21:42:04
User: random_bob
Functions: cut grep
-1

another way to output the IP address' of the system

for i in `git log --all --oneline --format=%h`; do git grep SOME_STRING $i; done
/sbin/ifconfig | awk -F'[ :]+' '/inet addr/{print $4}'
2009-07-21 14:18:17
User: inof
Functions: awk
-1

That one works on Linux. On BSD and Solaris, the ifconfig output is much easier to parse:

/sbin/ifconfig -a | awk '/inet/{print $2}'
alias restart='apache2ctl configtest && apache2ctl restart'
2009-07-21 14:13:15
Functions: alias
2

making lots of configurations to apache and restarting the server only to find it broken just plain sucks.

/sbin/ifconfig eth0 | grep "inet addr" | sed -e 's/.*inet addr:\(.*\) B.*/\1/g'
ifconfig $DEVICE | perl -lne '/inet addr:([\d.]+)/ and print $1'
2009-07-21 13:48:19
User: jdob
Functions: ifconfig perl
Tags: IP
-2

Found this useful for scripts where I needed to work with the machine's IP. If $DEVICE is not specified, this will return all IPs on the machine. If $DEVICE is set to a network adapter, it will return just that adapter's IP.

alias grip="grep -i"
2009-07-21 11:12:15
User: inof
Functions: alias
-3

This is *NOT* about the -i option in grep. I guess everybody already knows that option. This is about the basic rule of life that the simplest things are sometimes the best. ;-)

One day when I used "grep -i" for the umpteenth time, I decided to make this alias, and I've used it ever since, probably more often than plain grep. (In fact I also have aliases egrip and fgrip defined accordingly. I also have wrip="grep -wi" but I don't use this one that often.)

If you vote this down because it's too trivial and simplistic, that's no problem. I understand that. But still this is really one of my most favourite aliases.

alias sucs="sort | uniq -c | sort -n"
2009-07-21 10:55:06
User: inof
Functions: alias
0

This alias finds identical lines in a file (or pipe) and prints a sorted count of them (the name "sucs" descends from the first letters of the commands). The first example shows the number of logins of users; the one who logged in most often comes last. The second example extracts web client IP addresses from a log file, then pipes the result through the "sucs" alias to find out which clients are performing the most accesses. Or pipe the first column of ps(1) output through "sucs" to see how many processes your users are running.

/usr/sbin/apache2ctl -S
2009-07-21 10:52:49
User: lingo
0

Simply shows virtualhosts enabled, root access not needed (assuming apache2ctl is non-root executable).

/usr/sbin/apache2ctl -S 2>&1 | perl -ne 'm@.*port\s+([0-9]+)\s+\w+\s+(\S+)\s+\((.+):.*@ && do { print "$2:$1\n\t$3\n"; $root = qx{grep DocumentRoot $3}; $root =~ s/^\s+//; print "\t$root\n" };'
2009-07-21 10:51:30
User: lingo
Functions: perl
4

Lists virtualhosts currently enabled for apache2, showing the ServerName:port, conf file and DocumentRoot

find / | xargs ls -l | tr -s ' ' | cut -d ' ' -f 1,3,4,9
cvs update -C
cat `whereis mysqlbug | awk '{print $2}'` | grep 'CONFIGURE_LINE='
pbpaste | tidy -xml -wrap 0 | pbcopy
print 'g/'delete this line'/delete\nwq' | ex file.txt
2009-07-20 19:15:29
User: mph
Functions: ex
0

If you've ever wanted to change a text files contents without having to create an intermediate file. This is it. Ex is a part of vim. The command as given will delete ALL lines containing "delete this line" from the file.

More Examples: print '%s/'this is a line'/'that is not the same line'/g\nwq' | ex file.txt will substitute the first string with the second string. print '3a\n'Inserted Line'\n.\n\nwq' | ex file.txt will insert the given line after line 3.

CAVEAT, Some distro's like the print command, others like echo with this command. Also note there are NO error messages on failure, at least that I've ever seen. Ex can also be quite fussy as to how it takes strings, parameters, etc... I use at&t's ksh syntax may very with other shells.

lsof -p <process_id> | wc -l
ulimit -n <value>
2009-07-20 17:39:28
User: virtualshock
Functions: ulimit
0

It is not uncommon to receive an error "Too many open files", this command allows you to change the limit for a user. This can be put into /etc/profile so that all users will have this change.

Add to ~/.inputrc: set match-hidden-files off
sed -n '/<Tag>/,/<\/Tag>/p' logfile.log
2009-07-20 13:24:56
User: sanmiguel
Functions: sed
0

If your XML is appended to a line with a time stamp or other leading text irrelevant to the XML, then you can append a s/foo/bar/ command, like this:

sed -n /<Tag>/,/<\/Tag>/p; s/.*\(<Tag.*\)/\1/' logfile.log
pkill $1
2009-07-20 12:47:41
User: svg
-5

There's already a proper command for what the former alternative tried to script