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,621 results
for i in *.bak ; do nuname=`echo $i | sed 's/\.[^\.]*$//'`; echo renaming $i to $nuname;mv $i $nuname; done
awk '/pattern1/ && /pattern2/ && !/pattern3/ {print}'
2009-02-05 15:18:19
User: themensch
Functions: awk
12

Rather than chain a string of greps together and pipe them to awk, use awk to do all the work. In the above example, a string would be output to stdout if it matched pattern1 AND pattern2, but NOT pattern3.

for i in * ; do cp $i $i.bak; done
2009-02-05 15:15:40
User: swinz
Functions: cp
-2

quick in directory backup of all files in this directory. Adds the .bak extension to all copies.

ps -ef | grep $USER
2009-02-05 14:57:39
User: ryodoan
Functions: grep ps
0

I like to make it an alias in my .bashrc file, as such:

alias psme='ps -ef | grep $USER'

mysql -u uname dbname -e "show tables" | grep -v Tables_in | grep -v "+" | gawk '{print "optimize table " $1 ";"}' | mysql -u uname dbname
mysql -u uname dbname -e "show tables" | grep -v Tables_in | grep -v "+" | gawk '{print "drop table " $1 ";"}' | mysql -u uname dbname
curl -I www.commandlinefu.com
awk 'length($0)!=12 {print}' your_file_name
2009-02-05 14:27:52
User: hannahbrian
Functions: awk
1

Alternatively, print all the lines that are a certain length:

awk 'length($0)==12 {print}' your_file_name
lspci | grep Ether | awk '{ VAR=$1; split(VAR,ARR,"."); count[ARR[1]]++; LINE=$0; split(LINE,LINEARR,":"); LINECOUNT[ARR[1]]=LINEARR[3]; } END { for(i in count) { printf("PCI address: %s\nPorts: %d\nCard Type: %s\n", i, count[i], LINECOUNT[i]) } }'
echo "SHOW PROCESSLIST\G" | mysql -u root -p | grep "Info:" | awk -F":" '{count[$NF]++}END{for(i in count){printf("%d: %s\n", count[i], i)}}' | sort -n
awk '{count[length]++}END{for(i in count){printf("%d: %d\n", count[i], i)}}'
ps awwwux | grep httpd | grep -v grep | awk '{mem = $6; tot = $6 + tot; total++} END{printf("Total procs: %d\nAvg Size: %d KB\nTotal Mem Used: %f GB\n", total, mem / total, tot / 1024 / 1024)}'
sudo lsof | awk '{printf("%s %s %s\n", $1, $3, $NF)}' | grep -v "(" | sort -k 4 | gawk '$NF==prv{ct++;next} {printf("%d %s\n",ct,$0);ct=1;prv=$NF}' | uniq | sort -nr
grep -v "^#" file.txt | more
for i in $(locate your_search_phrase); do dirname $i; done | sort | uniq
2009-02-05 14:03:20
User: realbrewer
Functions: dirname locate sort
1

Ever use 'locate' to find a common phrase in a filename or directory name? Often you'll get a huge list of matches, many of which are redundant, and typically the results are not sorted. This command will 'locate' your search phrase, then show you a sorted list of just the relevant directories, with no duplications. So, for example, maybe you have installed several versions of the java jre and you want to track down every directory where files matching "java" might exist. Well, a 'locate java' is likely to return a huge list with many repeated directories since many files in one directory could contain the phrase "java". This command will whittle down the results to a minimal list of unique directory names where your search phrase finds a match.

package-cleanup --leaves --all
2009-02-05 13:51:27
User: drebes
0

This command lists all packages in a yum based system that no other packages depend on. Hence, these packages are good candidates for removal. It's a great command for cleaning up a yum based distribution after installation.

alias s='screen -X screen'; s top; s vi; s man ls;
2009-02-05 13:47:14
User: jonty
Functions: alias man
18

If you are already running screen then you often want to start a command in a fresh window. You use this alias by typing 's whatever' from your command line and 'whatever' starts running in a new window. Good with interactive commands like info, vim, and nethack.

ps -ef | grep [f]oo | awk '{print $2}' | xargs kill -9
2009-02-05 13:43:01
User: eredicatorx
Functions: awk grep kill ps xargs
1

Kill all processes with foo in them. Similar to pkill but more complete and also works when there is no pkill command.

Works on almost every Linux/Unix platform I have tried.

alias 'ps?'='ps ax | grep '
mkdir /Volumes/sshdisk 2> /dev/null; sshfs user@server:/ /Volumes/sshdisk -oreconnect,volname=SSHDisk
2009-02-05 13:31:15
User: fzero
Functions: mkdir
1

To make it even more practical, make sure you can login to the ssh server using a keypair.

pgrep xterm
2009-02-05 13:29:30
User: leonardovaz
0

pgrep is an useful tool which looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. pkill can also be used with pgrep to send a kill signal to the program.

TZ=XYZ24 date
2009-02-05 13:27:14
User: jonty
1

Fool date by setting the timezone out by 24 hours and you get yesterday's date. Try TZ=XYZ-24 to get tomorrow's date. I live in TZ=GMT0BST so you might need to shift the number 24 by the hours in your timezone.

cd /this/directory; for f in *; do ln -s `pwd`/$f /that/directory; done
2009-02-05 13:21:16
User: jonty
Functions: cd ln
0

I find this handy for linking all the bin files in a package to /usr/bin or all the man files to /usr/share/man. You can replace * with */* to operate on all the files in subdirectories.

lspci -vv
2009-02-05 13:01:41
User: kumara
Functions: lspci
4

great for running off a bootable cd to identify hardware other os's can't detect

cat /proc/cpuinfo