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 tagged ls from sorted by
Terminal - Commands tagged ls - 98 results
compgen -c | sort -u > commands && less commands
ls ${PATH//:/ }
2012-04-26 19:45:52
User: Zulu
Functions: ls
9

List all commands present on system by folder.

PATH contains all command folder separated by ':'. With ${PATH//:/ }, we change ':' in space and create a list of folder for ls command.
find . -type d |sed 's:[^-][^/]*/:--:g; s:^-: |:'
2012-04-14 00:51:09
User: khopesh
Functions: find sed
Tags: ls tree
0

shorter version. I believe find is faster than ls as well.

find /some/path -type f -and -printf "%f\n" | egrep -io '\.[^.]*$' | sort | uniq -c | sort -rn
2012-04-02 19:25:35
User: kyle0r
Functions: egrep find sort uniq
Tags: uniq ls grep
0

the

find -printf "%f\n" prints just the file name from the given path. This means directory paths which contain extensions will not be considered.
ls -Rl dir1/ > /tmp/dir1.ls; ls -Rl dir2/ > /tmp/dir2.ls; meld /tmp/dir1.ls /tmp/dir2.ls
2012-03-04 13:06:55
User: joeseggiola
Functions: ls
0

Compare the ls -Rl output of two directories in meld (you can also use diff -y instead of meld).

find ./ -type f -size +100000k -exec ls -lh {} \; 2>/dev/null| awk '{ print $8 " : " $5}'
2012-01-21 04:19:35
User: Goez
Functions: awk find ls
0

This command does a basic find with size. It also improves the printout given (more clearer then default)

Adjusting the ./ will alter the path.

Adjusting the "-size +100000k" will specify the size to search for.

ls -t | head
2012-01-17 16:28:32
User: scottlinux
Functions: ls
Tags: tail ls head,
2

This will quickly display files last changed in a directory, with the newest on top.

ls -d1 $PWD/*
ls -d1 $PWD/{.*,*}
ls -a | sed "s#^#${PWD}/#"
2011-12-16 22:19:06
User: bbbco
Functions: ls sed
Tags: sed ls pwd
-9

Use the -a flag to display all files, including hidden files. If you just want to display regular files, use a -1 (yes, that is the number one). Got this by RTFM and adding some sed magic.

[bbbco@bbbco-dt ~]$ ls -a | sed "s#^#${PWD}/#"

/home/bbbco/.

/home/bbbco/..

/home/bbbco/2011-09-01-00-33-02.073-VirtualBox-2934.log

/home/bbbco/2011-09-10-09-49-57.004-VirtualBox-2716.log

/home/bbbco/.adobe

/home/bbbco/.bash_history

/home/bbbco/.bash_logout

/home/bbbco/.bash_profile

/home/bbbco/.bashrc

...

[bbbco@bbbco-dt ~]$ ls -1 | sed "s#^#${PWD}/#"

/home/bbbco/2011-09-01-00-33-02.073-VirtualBox-2934.log

/home/bbbco/2011-09-10-09-49-57.004-VirtualBox-2716.log

/home/bbbco/cookies.txt

/home/bbbco/Desktop

/home/bbbco/Documents

/home/bbbco/Downloads

...

for file in * .*; do echo $PWD/$file; done
2011-12-16 13:42:07
User: marek158
Functions: echo file
Tags: echo ls
-8

Also lists hidden files, current dir and topdir.

for file in *; do echo $PWD/$file; done
ls -ad */
2011-12-10 17:08:07
User: tbekolay
Functions: ls
Tags: ls directory
3

Like normal ls, but only lists directories.

Can be used with -l to get more details (ls -lad */)

ls -l `whereis gcc`
2011-11-15 19:45:08
User: knathan54
Functions: ls
Tags: which ls zsh
0

whereis (1) - locate the binary, source, and manual page files for a command

Not actually better, just expanded a bit. The "whereis" command has the following output:

whereis gcc

gcc: /usr/bin/gcc /usr/lib/gcc /usr/bin/X11/gcc /usr/share/man/man1/gcc.1.gz

therefore the 'ls' error on first line, which could be eliminated with a little extra work.

ls -l =gcc
ls -l `which gcc`
fail () { ln -s /nonexistent 0_FAIL_${1}; }
2011-11-06 20:14:33
User: pipeliner
Functions: ln
Tags: ls color link fail
0

If you use colored ls(1), the broken symbolic links significantly differ from regular files and directories in the ls listing. In my case it is bright red. 0 is for getting the first place in the list.

ls -1 $PATH*/* | xargs file | awk -F":" '!($2~/PDF document/){print $1}' |xargs rm -rf
ls -lFart |tail -n1
2011-10-17 19:49:14
User: jambino
Functions: ls tail
Tags: tail pipe ls
-2

List all files in a directory in reverse order by modified timestamp. When piped through tail the user will see the most recent file name.

lsr() { find "${@:-.}" -print0 |sort -z |xargs -0 ls $LS_OPTIONS -dla; }
2011-08-15 03:10:58
User: h3xx
Functions: find ls sort xargs
2

Tells you everything you could ever want to know about all files and subdirectories. Great for package creators. Totally secure too.

On my Slackware box, this gets set upon login:

LS_OPTIONS='-F -b -T 0 --color=auto'

and

alias ls='/bin/ls $LS_OPTIONS'

which works great.

ls -l | grep ^d | sed 's:.*\ ::g'
rm -R `ls | egrep -v 'dir1|dir2|file1'`
find * -type d -maxdepth 0
2011-08-07 06:04:50
User: edogawaconan
Functions: find
Tags: find ls grep sh
1

Alternatively,

ls -F | grep /\$

but will break on directories containing newlines. Or the safe, POSIX sh way (but will miss dotfiles):

for i in *; do test -d "./$i" && printf "%s\n" "$i"; done
ls -1d */
ls -l | grep ^d | sed 's:.*\ ::g'
2011-08-06 23:52:46
User: LinuxMan
Functions: grep ls sed
Tags: bash sed ls grep
-10

Normally, if you just want to see directories you'd use brianmuckian's command 'ls -d *\', but I ran into problems trying to use that command in my script because there are often multiple directories per line. If you need to script something with directories and want to guarantee that there is only one entry per line, this is the fastest way i know