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 -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

ls -atr /home/reports/*.csv -o --time-sty=+%s | tail -1 | awk '{print systime()-$5}'
ls *.jpg | awk -F'.' '{ printf "%s %04d.%s\n", $0, $1, $2; }' | xargs -n2 mv
2011-05-01 13:32:58
User: hute37
Functions: awk ls printf xargs
Tags: awk mv xargs
3

rename file name with fixed length nomeric format pattern

ls | sed 'p;s/foo/bar/' | xargs -n2 mv
ls * | sed -e 'p;s/foo/bar/' | xargs -n2 mv
2011-04-30 00:21:10
User: windfold
Functions: ls sed xargs
3

Renames all files in a directory named foo to bar.

foobar1 gets renamed to barbar1

barfoo2 gets renamed to barbar2

fooobarfoo gets renamed to barobarfoo

NOTE: Will break for files with spaces AND new lines AND for an empty expansion of the glob '*'