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 - 455 results
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 -l | sed -e 's/--x/1/g' -e 's/-w-/2/g' -e 's/-wx/3/g' -e 's/r--/4/g' -e 's/r-x/5/g' -e 's/rw-/6/g' -e 's/rwx/7/g' -e 's/---/0/g'
lso(){ jot -w '%04d' 7778 0000 7777 |sed '/[89]/d;s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \ |sh \ |{ echo "lso(){";echo "ls \$@ \\";echo " |sed '";sed 's, ,@,2;s,@.*,,;s,\(.* \)\(.*\),s/\2/\1/,;s, ,,';echo \';echo };};}
2012-01-08 05:48:24
User: argv
Functions: chmod echo ls sed sh
0

this requires the use of a throwaway file.

it outputs a shell function.

assuming the throwaway file is f.tmp

usage: >f.tmp;lso f.tmp > f.tmp; . f.tmp;rm f.tmp;lso -l ...

notes:

credit epons.org for the idea. however his version did not account for the sticky bit and other special cases.

many of the 4096 permutations of file permissions make no practical sense. but chmod will still create them.

one can achieve the same sort of octal output with stat(1), if that utility is available.

here's another version to account for systems with seq(1) instead of jot(1):

lso(){

case $# in

1)

{ case $(uname) in

FreeBSD)

jot -w '%04d' 7778 0000 7777 ;;

*)

seq -w 0000 7777 ;;

esac; } \

|sed '

/[89]/d

s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \

|sh \

|{

echo "lso(){";

echo "ls \$@ \\";

echo " |sed '";

sed '

s, ,@,2;

s,@.*,,;

s,\(.* \)\(.*\),s/\2/\1/,;

s, ,,';

echo \';

echo };

};

;;

*)

echo "usage: lso tmp-file";

;;

esac;

}

this won't print out types[1]. but its purpose is not to examine types. its focus is on mode and its purpose is to make mode easier to read (assuming one finds octal easier to read).

1. one could of course argue "everything is a file", but not always a "regular" one. e.g., a "directory" is really just a file comprising a list.

ls | view -
2012-01-04 07:18:44
User: lefada
Functions: ls
Tags: vim
0

view does not enable the buffer because it opens in read-only, so it does the same

ls -d1 $PWD/*
ls | vim +'set bt=nowrite' -
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

...

ls -d $PWD/*
find `pwd` -maxdepth 1 -exec ls --color -d {} \;
ls -1 | awk ' { print "zip "$1".zip " $1 } ' | sh
2011-12-14 20:30:56
User: kaywhydub
Functions: awk ls
Tags: awk zip sh
1

This will list the files in a directory, then zip each one with the original filename individually.

video1.wmv -> video1.zip

video2.wmv -> video2.zip

This was for zipping up large amounts of video files for upload on a Windows machine.

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 ${my_dir:=/home}
2011-11-30 15:06:51
Functions: ls
2

Will use variable value (for variable $my_dir, in this case), an assign a default value if there is none.

figlet -f $(ls /usr/share/figlet/fonts/*.flf | shuf -n1) namakukingkong | cowsay -n -f $(ls /usr/share/cows/ | shuf -n1)
2011-11-25 13:54:06
Functions: ls
Tags: Linux
0

You need to have figlet(for font) and cowsay installed then add it to your .bashrc file.You can see it every time after start a new session.

function right { bc <<< "obase=8;ibase=2;$1"; }; touch foo; chmod $(right 111111011) foo; ls -l foo
2011-11-16 22:43:31
User: nerd
Functions: bc chmod ls touch
0

I simply find binary notation more straightforward to use than octal in this case.

Obviously it is overkill if you just 600 or 700 all of your files...

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
for f in $(ls -A ./dir); do echo -n $f && diff original.txt ./dir/$f | wc -l ; done | perl -ne 'my $h={}; while (<>) { chomp; if (/^(\S+?)\s*(\d+?)$/){$h->{$1}=$2;} }; for my $k (sort { $h->{$a} $h->{$b} } keys %$h ){ print "$k\t$h->{$k}\n"}'
ls -l `which gcc`
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.

ls -ltp | sed '1 d' | head -n1
2011-10-17 16:21:15
Functions: head ls sed
-2

wrap it in a function if you like...

lastfile () { ls -ltp | sed '1 d' | head -n1 }
find / -perm +6000 -type f -exec ls -ld {} \;
ls -Fart
2011-09-19 13:07:47
User: jambino
Functions: ls
1

It's both silly, and infinitely useful. Especially useful in logfile directories where you want to know what file is being updated while troubleshooting.