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 - 452 results
ls -trF | grep -v \/ | tail -n 1
2011-09-14 20:05:37
User: mrpollo
Functions: grep ls tail
Tags: find stat mtime
-1

Sort by time and Reverse to get Ascending order, then display a marker next to the a file, negate directory and select only 1 result

myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link"; }
2011-09-13 11:02:27
User: keymon
Functions: cd command dirname echo ls
0

This is a equivalent to the GNU ' readlink' tool, but it supports following all the links, even in different directories.

An interesting alternative is this one, that gets the path of the destination file

myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link" | sed "s|^\([^/].*\)\$|$(dirname $1)/\1|"); }
ls -l /etc/**/*killall
2011-08-30 05:57:49
User: xeor
Functions: ls
9

This command will give you the same list of files as "find /etc/ -name '*killall' | xargs ls -l".

In a simpler format just do 'ls /etc/**/file'.

It uses shell globbing, so it will also work with other commands, like "cp /etc/**/sshd sshd_backup".

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 -1d */
2011-08-10 05:40:15
User: weldabar
Functions: ls
1

omit the 1 (one) if you don't need one-per-line

cd $(ls -ltr|grep ^d|head -1|sed 's:.*\ ::g'|tail -1)
2011-08-10 03:39:35
Functions: cd grep head ls sed tail
-1

Replace the head -1 with head -n that is the n-th item you want to go to.

Replace the head with tail, go to the last dir you listed.

You also can change the parameters of ls.

ls -l | grep ^d | sed 's:.*\ ::g'
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

mplayer $(ls -l /proc/$(pgrep -f flash)/fd/* |grep Flash | cut -d" " -f8)
find . -type l | (while read FN ; do test -e "$FN" || ls -ld "$FN"; done)
ls *.zip|awk '{$a="zip -fo "$1" FILENAME"; system($a);}'
2011-07-27 10:22:21
User: youkey
Functions: awk ls
Tags: awk zip
0

- all zips are in current folder

- FILENAME is file name that should be subsitute in all zips (new version of this file is in current folder)

locate -i yourfilename | sed 's/ /\\ /g' | xargs ls -lah | less
alias cd1='cd $( ls -1t | grep ^d | head -1)'
ls -Fhitlar
2011-07-11 10:29:34
User: ringzero
Functions: ls
Tags: ls
1

Was playing with the shell. It struck to me, just by rearranging the parameters, i was able to remember what they did and in a cool way.

Enter the 'hitlar' mode.

bash-3.2$ ls -hitlar

Shows all items with inodes, in list view, human readable size, sorted by modification time in reverse,

bash-3.2$ ls -Fhitlar

Shows the same with classification info. Add the hitlar mode alias to your .bashrc.

bash-3.2$ echo "alias hitlar='ls -Fhitlar'" >> ~/.bashrc

bash-3.2$ hitlar

bash-3.2$ hitlar filename

nice -n0 ls | mpg321 -@- &
cd $(ls -1t --color=never | head -1)
alias cd1='cd $( ls -lt | grep ^d | head -1 | cut -b 51- )'
ls -d */* | wc -l
ls -lart
2011-05-28 15:01:39
User: shardservant
Functions: ls
-1

-l for long list, -r for recursive, -a for display of hidden files, and -t for modification date

ls -t1 | head -n1
ssh tomcat-server ls -l webapp-dir | grep -- '->' | awk ' { print $(NF-2) " " $(NF-1) " " $NF; }'
2011-05-23 08:38:28
User: igorfu
Functions: awk grep ls ssh
Tags: tomcat
0

Tomcat webapps are often remote links

sdiff <(ls /) <(ls /usr)
ls * | while read fin;do fout=$(echo -n $fin | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e);if [ "$fout" != "$fin" ];then echo "mv '$fin' '$fout'";fi;done | bash -x
2011-05-18 07:24:54
User: pawelb1973
Functions: bash echo ls read sed xargs
0

urldecode files in current directrory

ls -l $(type -path -all java)
2011-05-12 17:25:39
User: evandrix
Functions: ls type
1

The output will likely point to '/etc/alternatives/java'.

So find out where that points by issuing ls -l like this:

ls -l /etc/alternatives/java