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.


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:



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.




Commands using ls from sorted by
Terminal - Commands using ls - 452 results
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
ls -lad
2013-04-03 09:58:31
User: techie
Functions: ls
Tags: ls

This will show you the permissions on the directory you are currently in

ps aux | grep [process] | awk '{print $2}' | xargs -I % ls /proc/%/fd | wc -l
while true; do ls -all myfile; spleep 1; clear; done
2013-03-26 09:13:19
User: ivodeblasi
Functions: ls

Sometime you need to monitor file or direcory change in dimension or other attributes. This command output file (called myfile in the example) attributes in the top of the screen, updating each 1 second.

You should change update time, command ( e.g., ls -all ) or target ( myfile, mydir, etc...).

ls -tl **/*(om[1,20])
2013-03-24 00:14:03
User: khayyam
Functions: ls
Tags: ls zsh

zsh globbing and glob qualifier:

'**/*' = recursive

om = ouput by modification (last access)

[1,20] = twenty files.

The '-t' switch is provided to ls so that the files are ordered with the most recent at the top. For a more 'find' like output the following can be used.

print -rl **/*(om[1,20])

ls -Sh **/*(.Lm+100) | tail -5
2013-03-21 20:22:11
User: khayyam
Functions: ls tail
Tags: tail ls zsh

zsh: list of files sorted by size, greater than 100mb, head the top 5. '**/*' is recursive, and the glob qualifiers provide '.' = regular file, 'L' size, which is followed by 'm' = 'megabyte', and finally '+100' = a value of 100

find -type f | xargs ls -1tr
find . -type f -exec ls -s {} \; | sort -n -r | head -5
ls -qahlSr # list all files in size order - largest last
2013-03-13 09:52:07
User: mpb
Functions: ls size

I find it useful, when cleaning up deleting unwanted files to make more space, to list in size order so I can delete the largest first.

Note that using "q" shows files with non-printing characters in name.

In this sample output (above), I found two copies of the same iso file both of which are immediate "delete candidates" for me.

ls -l | grep ^d
ls -qaltr # list directory in chronological order, most recent files at end of list
2013-02-25 14:14:44
User: mpb
Functions: at ls

I find it very handy to be able to quickly see the most recently modified/created files in a directory.

Note that the "q" option will reveal any files with non-printable characters in their filename.

ls -ltrhd */
ls -lT -rt | grep "^-" | awk 'BEGIN {START=2002} (START <= $9){ print $10 ;START=$9 }' | tail -1
2013-02-24 23:39:22
User: Glamdring
Functions: awk grep ls tail
Tags: ls date osx

On the Mac, the 'ls' function can sort based on month/day/time, but seems to lack ability to filter on the Year field (#9 among the long listed fields). The sorted list continuously increases the 'START' year for the most recently accessed set of files. The final month printed will be the highest month that appeared in that START year. The command does its magic on the current directory, and suitably discards all entries that are themselves directories. If you expect files dating prior to 2002, change the START year accordingly.

ls -d */
ls -a | du --max-depth=1 -h 2>/dev/null |sort -h
touch file{1..10}.txt ; ls *txt| sed -e "p;s/\.txt$/\.csv/"|xargs -n2 mv
for x in `ps -u 500 u | grep java | awk '{ print $2 }'`;do ls /proc/$x/fd|wc -l;done
cd $(ls -1 | sed -n '<N>p')
for i in `pfiles pid|grep S_IFREG|awk '{print $5}'|awk -F":" '{print $2}'`; do find / -inum $i |xargs ls -lah; done
2013-01-24 13:57:19
User: giorger
Functions: awk find grep ls xargs

Executing pfiles will return a list of all descriptors utilized by the process

We are interested in the S_IFREG entries since they are pointing usually to files

In the line, there is the inode number of the file which we use in order to find the filename.

The only bad thing is that in order not to search from / you have to suspect where could possibly be the file.

Improvements more than welcome.

lsof was not available in my case

ls -lhR | grep -e "total\|:$"
2013-01-22 04:58:51
User: Sebasg
Functions: grep ls
Tags: ls grep

ls -lhR

Lists everithing using -l "long listing format" wich includes the space used by the folder. Displays it in -h "human readable form" (i.e. 2.2G, 32K), and -R recurses subfolders.

grep -e using a regex, show lines containing the word "total" or a ":" at the end of the line (those with the name of the folder) only.

ls | wc -l
2013-01-22 03:35:35
User: Sebasg
Functions: ls wc

ls -1 shows one file per line (update: -1 was not really needed)

wc -l counts the lines received from the previous command

ls -l /dev/disk/by-id/ | grep '/sda$' | grep -o 'ata[^ ]*'
2013-01-16 17:28:11
User: michelsberg
Functions: grep ls
Tags: ls grep drive

Substitute for #11720

Can probably be even shorter and easier.

man ls | egrep "^([A-Z]| [A-Z])"
2013-01-09 17:12:03
User: bartonski
Functions: egrep ls man

Uses the formatting of a man page to show an outline of its headers and sub-headers.

ls -l /dev/disk/by-id | egrep ata-.*`hdparm -i /dev/sda | grep SerialNo | sed 's/.*SerialNo=//' | tr -d "\n"`.*sda$ | sed -e 's/.*ata-/ata-/' -e 's|[ ].*||' | tr -d "\n"
2013-01-07 10:20:25
Functions: egrep grep ls sed tr
Tags: Ubuntu

This was tested on Ubuntu 12.04 (Precise) LTS Server. It returns the name of the symlink within /dev/disk/by-id for the physical drive you specify. Change /dev/sda to the one you want, and replace ata- with scsi- or the appropriate type for your drive.

I used this to pre-configure grub-pc during a non-interactive install because I had to tell it which disk to install grub on, and physical disks don't have a UUID such as that blkid provides.