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,848 results
find ~/Desktop/ \( -regex '.*/\..*' \) -print -exec rm -Rf {} \;
cat somefile.css | awk '{gsub(/{|}|;/,"&\n"); print}' >> uncompressed.css
2009-06-02 15:51:51
User: lrvick
Functions: awk cat
0

Ever compress a file for the web by replacing all newline characters with nothing so it makes one nice big blob?

It is a great idea, however what about when you want to edit that file? ...Serious pain in the butt.

I ran into this today in that my only copy of a CSS file was "compressed" with no newlines.

I whipped this up and it converted back into nice human readable CSS :-)

It could be nicer, but it does the job.

resolveip -s www.freshmeat.net
lynx -source http://www.commandlinefu.com/commands/random | sed 's/<[^>]*>//g' | head -1037 | tail -10 | sed -e 's/^[ \t]*//' | sed '/^$/d' | head -2
date -d '1 day ago'; date -d '11 hour ago'; date -d '2 hour ago - 3 minute'; date -d '16 hour'
2009-06-01 10:41:56
User: LrdShaper
Functions: date
5

With this command you can get a previous or future date or time. Where can you use this? How about finding all files modified or created in the last 5 mins?

touch -t `echo $(date -d "5 minute ago" "+%G%m%d%H%M.%S")` me && find . -type f -newer me

List all directories created since last week?

touch -t `echo $(date -d "1 week ago" "+%G%m%d%H%M.%S")` me && find . -type d -cnewer me

I'm sure you can think of more ways to use it. Requires coreutils package.

postsuper -d ALL
while [ i != 0 ]; do sleep 1 | dialog --clear --gauge "Quality: " 0 0 $(cat /proc/net/wireless | grep $WIRELESSINTERFACE | awk '{print $3}' | tr -d "."); done
2009-05-31 16:09:23
User: ncaio
Functions: awk cat grep sleep tr
1

The variable WIRELESSINTERFACE indicates your wireless interface

pdfinfo Virtualization_A_Beginner_Guide.pdf | awk /Pages/
mkdir -p /cdrom/unnamed_cdrom ; mount -F hsfs -o ro `ls -al /dev/sr* |awk '{print "/dev/" $11}'` /cdrom/unnamed_cdrom
2009-05-31 08:42:20
User: felix001
Functions: awk mkdir mount
-2

This will allow you to mount a CD-ROM on Solaris SPARC 9 or lower. This will not work on Solaris 10 due to void and the volume management daemons.

www.fir3net.com

net rpc shutdown -I ipAddressOfWindowsPC -U username%password
2009-05-31 07:18:01
User: LrdShaper
Functions: shutdown
Tags: Linux windows
105

This will issue a shutdown command to the Windows machine. username must be an administrator on the Windows machine. Requires samba-common package installed. Other relevant commands are:

net rpc shutdown -r : reboot the Windows machine

net rpc abortshutdown : abort shutdown of the Windows machine

Type:

net rpc

to show all relevant commands

less -Rf <( cat <(ls -l --color=always) <(ls -ld --color=always .*) )
2009-05-30 23:51:16
User: asmoore82
Functions: cat less ls
2

To sort hidden files first, simply switch the two inner `ls` commands.

I have this aliased to `dira`

`dir` is aliased to the simpler version with no hidden files:

ls -l --color=always | less -R
for f in `find ./ -name "*.zip"` ; do p=`pwd`; d=`dirname $f`; cd $d; b=`basename $f`; unzip $b; cd $p; done
2009-05-30 03:42:53
User: packetloss
Functions: cd
Tags: find unzip
-3

unzips all zip files in any subdirectory under the current directory. The zip files are unzipped in their respective subdirs

for i in `svn status | egrep '^(M|A)' | sed -r 's/\+\s+//' | awk '{ print $2 }'` ; do if [ ! -d $i ] ; then php -l $i ; fi ; done
2009-05-29 23:59:28
Functions: awk egrep sed
Tags: svn Linux PHP
0

Really only valuable in a PHP-only project directory. This is using standard linux versions of the tools. On most older BSD variants of sed, use -E instead of -r. Or use: sed 's/\+[[:space:]]\{1,\}//' instead.

dd if=/dev/sda5 bs=2048 conv=noerror,sync | gzip -fc | lftp -u user,passwd domain.tld -e "put /dev/stdin -o backup-$(date +%Y%m%d%H%M).gz; quit"
ioreg -lw0 | grep IODisplayEDID | sed "/[^<]*</s///" | xxd -p -r | strings -6
gmetric -n $METRIC_NAME -v foo -t string -d 10
2009-05-29 14:21:24
User: root
Tags: ganglia
-1

The -d flag sets the lifetime of a metric and defaults to 0 hence why old metrics continue to be graphed in the dashboard. Submitting a dummy value and short lifetime ensures that the metric is removed from the dashboard.

more /var/adm/messages
2009-05-29 12:10:18
User: miccaman
Functions: more
Tags: solaris
-10

read system logs of sun solaris 9

init 6
2009-05-29 07:44:05
User: miccaman
Functions: init
Tags: solaris Reboot
-11

init states on solaris are numbered

init 0 boot with prompt

init 5 shutdown

init 6 reboot

awk 'BEGIN{srand()}{print rand(),$0}' SOMEFILE | sort -n | cut -d ' ' -f2-
2009-05-29 01:20:50
User: axelabs
Functions: awk cut sort
Tags: sort awk random
4

This appends a random number as a first filed of all lines in SOMEFILE then sorts by the first column and finally cuts of the random numbers.

awk 'BEGIN{size=5} {mod=NR%size; if(NR<=size){count++}else{sum-=array[mod]};sum+=$1;array[mod]=$1;print sum/count}' file.dat
2009-05-29 00:07:24
User: mungewell
Functions: awk
3

Sometimes jittery data hides trends, performing a rolling average can give a clearer view.

cat typescript | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > typescript-processed
zip -vr example.zip example/ -x "*.DS_Store"
2009-05-28 20:28:12
User: JadedEvan
4

If you want to generate a cross-platform compatible zip file and ignore the Finder's hidden metadata directory

apropos keyword
egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt
find <directory path> -mtime +365 -and -not -type d -delete