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

Commands using ls from sorted by
Terminal - Commands using ls - 448 results
ls /var/log/sa/sa[0-9]*|xargs -I '{}' sar -u -f {}|awk '/^[0-9]/&&!/^12:00:01|RESTART|CPU/{print "%user: "$4" %system: "$6" %iowait: "$7" %nice: "$5" %idle: "$9}'|sort -nk10|head
ls /var/log/sa/sa[0-9]*|xargs -I '{}' sar -q -f {}| awk '/Average/'|awk '{runq+=$2;plist+=$3}END{print "average runq-sz:",runq/NR; print "average plist-sz: "plist/NR}'
ls|wc -l
ls /dev/disk* | xargs -n 1 -t sudo zdb -l | grep GPTE_
2012-10-06 20:19:45
User: grahamperrin
Functions: grep ls sudo xargs
1

Show the UUID-based alternate device names of ZEVO-related partitions on Darwin/OS X. Adapted from the lines by dbrady at http://zevo.getgreenbytes.com/forum/viewtopic.php?p=700#p700 and following the disk device naming scheme at http://zevo.getgreenbytes.com/wiki/pmwiki.php?n=Site.DiskDeviceNames

cd () { command cd $1 && ls ; }
ls -l | grep "\->"
2012-09-20 12:48:36
User: joernfranz
Functions: grep ls
Tags: readlink
0

Pretty lame to rely on grep and "\->"? Maybe. But it works ;)

ls -l /proc/*/fd/* | grep 'deleted'| grep "\/proc.*\file-name-part"
2012-09-13 09:54:16
User: totti
Functions: grep ls
0

Accidentally deleted some file while used by a program ? (Eg: a song)

Use this command to find the file handle and recover using

cp /proc/pid/fd/filehandle /new/recoverd-file.ext
ls -rt | tail -n 1
ln -s /base/* /target && ls -l /target
wget --no-use-server-timestamps $(curl $(curl http://wallbase.cc/random/23/eqeq/1920x1080/0/100/20 | grep 'wallpaper/' | awk -F'"' '{print $2}' | head -n1) | grep -A4 bigwall | grep img | awk -F'"' '{print $2}'); feh --bg-center $(ls -1t | head -n1)
alias ls='if [[ -f .hidden ]]; then while read l; do opts+=(--hide="$l"); done < .hidden; fi; ls --color=auto "${opts[@]}"'
2012-08-12 13:10:23
User: expelledboy
Functions: alias ls read
Tags: hidden ls alias
1

Sometimes I would like to see hidden files, prefix with a period, but some files or folders I never want to see (and really wish I could just remove all together).

ls | grep -Ze ".*rar" | xargs -d '\n' -I {} mv {} backup-folder
2012-08-06 09:07:03
User: crisboot
Functions: grep ls mv xargs
0

In the example suppose we want to move all *.rar files in the current folder to a backupfolder

ls -il; find * \( -type d -prune \) -o -inum <NUM> -exec rm -i {} \;
2012-08-04 00:22:33
User: yingw
Functions: find ls rm
0

Code to delete file with gremlins/special characters/unicode in file name.

Use ls -i to find the INODE number corresponding to the file and then delete it using that find statement.

detailed here:

http://www.arsc.edu/arsc/support/howtos/nonprintingchars/

find . -type f -exec ls -l --full-time {} + | sort -k 6,7
2012-08-03 22:22:51
User: quadcore
Functions: find ls sort
Tags: sort find ls
2

This sorts files in multiple directories by their modification date. Note that sorting is done at the end using "sort", instead of using the "-ltr" options to "ls". This ensures correct results when sorting a large number of files, in which case "find" will call "ls" multiple times.

find . -type f -exec ls -shS {} + | head -10
2012-07-28 17:21:46
User: erichamion
Functions: find head ls
0

This requires a version of GNU find that supports the -exec {} + action, but it seems more straightforward than the versions already posted.

ls *.png | while read line; do pngcrush -brute $line compressed/$line; done
2012-07-17 20:20:49
User: waffleboi9
Functions: ls read
0

Nothing too magical here, just uses pngcrush to losslessly compress all your pngs!

ls -al | sort +4n
2012-06-26 19:20:05
User: ankush108
Functions: ls sort
Tags: size sort files
0

ls -al gives all files, sort +4n sorts by 5th field numerically

ls -ltr |grep 'May 12'|awk '{print $9;}'|xargs rm -v
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
ls -l| sort +4n|tail -1| awk '{print $NF}'
ls -al | grep ^d
2012-05-21 17:39:23
User: christo
Functions: grep ls
0

Cleaner than adding -p to ls ( eg ls -alp ), this will tell ls to only output directories in the file listing, whilst maintaining all the usual other column elements of the ls output.

find -type l | xargs ls -l
ls "`defaults read com.apple.itunes NSNavLastRootDirectory`/iTunes/iTunes Music/Mobile Applications/" > filepath
ls "~/Music/iTunes/iTunes Media/Mobile Applications" > filepath
2012-05-04 09:51:59
User: minnmass
Functions: ls
Tags: ipad
1

This will generate the same output without changing the current directory, and filepath will be relative to the current directory.

Note: it will (still) fail if your iTunes library is in a non-standard location.

cd "~/Music/iTunes/iTunes Media/Mobile Applications";ls > filepath