Advanced LS Output using Find for Formatted/Sortable File Stat info

find $PWD -maxdepth 1 -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n'
I love this function because it tells me everything I want to know about files, more than stat, more than ls. It's very useful and infinitely expandable. find $PWD -maxdepth 1 -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n' | sort -rgbS 50% 00761 drwxrw---x askapache:askapache 777:666 [06/10/10 | 06/10/10 | 06/10/10] [d] /web/cg/tmp The key is: # -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n' which believe it or not took me hundreds of tweaking before I was happy with the output. You can easily use this within a function to do whatever you want.. This simple function works recursively if you call it with -r as an argument, and sorts by file permissions. lsl(){ O="-maxdepth 1";sed -n '/-r/!Q1'<<<$@ &&O=;find $PWD $O -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n'|sort -rgbS 50%; } Personally I'm using this function because: lll () { local a KS="1 -r -g"; sed -n '/-sort=/!Q1' <<< $@ && KS=`sed 's/.*-sort=\(.*\)/\1/g'<<<$@`; find $PWD -maxdepth 1 -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n'|sort -k$KS -bS 50%; } # i can sort by user lll -sort=3 # or sort by group reversed lll -sort=4 -r # and sort by modification time lll -sort=6 If anyone wants to help me make this function handle multiple dirs/files like ls, go for it and I would appreciate it.. Something very minimal would be awesome.. maybe like: for a; do lll $a; done Note this uses the latest version of GNU find built from source, easy to build from gnu ftp tarball. Taken from my
Sample Output
$ find $PWD -maxdepth 1 -printf '%.5m %10M %#9u:%-9g %#5U:%-5G  [%AD | %TD | %CD]  [%Y] %p\n' | sort -rgbS 50%
00761 drwxrw---x  askapache:askapache    777:666    [06/10/10 | 06/10/10 | 06/10/10]  [d] /web/cg/tmp
00755 drwxr-xr-x  askapache:askapache    777:666    [02/10/10 | 05/21/10 | 05/25/10]  [d] /web/cg/java
00701 drwx-----x  askapache:askapache    777:666    [06/10/10 | 06/10/10 | 06/10/10]  [d] /web/cg/askapache-m
00644 -rw-r--r--  askapache:askapache    777:666    [02/10/10 | 01/12/10 | 06/01/10]  [f] /web/cg/.htaccess

$ find $PWD -maxdepth 1 -printf '%.5m %10M %#9u:%-9g %#5U:%-5G  [%AD | %TD | %CD]  [%Y] %p\n'
00644 -rw-r--r--  askapache:askapache    777:666    [03/22/10 | 03/22/10 | 05/25/10]  [f] /h/wireshark/cmdarg_err.h
00755 drwxr-xr-x  askapache:askapache    777:666    [03/22/10 | 03/22/10 | 05/25/10]  [d] /h/wireshark/codecs
00644 -rw-r--r--  askapache:askapache    777:666    [03/22/10 | 03/22/10 | 05/25/10]  [f] /h/wireshark/color.h
00644 -rw-r--r--  askapache:askapache    777:666    [03/22/10 | 03/22/10 | 05/25/10]  [f] /h/wireshark/colorfilters

These Might Interest You

  • This shows every bit of information that stat can get for any file, dir, fifo, etc. It's great because it also shows the format and explains it for each format option. If you just want stat help, create this handy alias 'stath' to display all format options with explanations. alias stath="stat --h|sed '/Th/,/NO/!d;/%/!d'" To display on 2 lines: ( F=/etc/screenrc N=c IFS=$'\n'; for L in $(sed 's/%Z./%Z\n/'<<<`stat --h|sed -n '/^ *%/s/^ *%\(.\).*$/\1:%\1/p'`); do G=$(echo "stat -$N '$L' \"$F\""); eval $G; N=fc;done; ) For a similarly powerful stat-like function optimized for pretty output (and can sort by any field), check out the "lll" function From my .bash_profile -> Show Sample Output

    statt(){ C=c;stat --h|sed '/Th/,/NO/!d;/%/!d'|while read l;do p=${l/% */};[ $p == %Z ]&&C=fc&&echo ^FS:^;echo "`stat -$C $p \"$1\"` ^$p^${l#%* }";done|column -ts^; }
    AskApache · 2010-06-11 23:31:03 0
  • This alias is super-handy for me because it quickly shows the details of each file in the current directory. The output is nice because it is sortable, allowing you to expand this basic example to do something amazing like showing you a list of the newest files, the largest files, files with bad perms, etc.. A recursive alias would be: alias LSR='find -mount -printf "%.5m %10M %#9u:%-9g %#5U:%-5G %TF_%TR %CF_%CR %AF_%AR %#15s [%Y] %p\n" 2>/dev/null' From: Show Sample Output

    alias LS='find -mount -maxdepth 1 -printf "%.5m %10M %#9u:%-9g %#5U:%-5G %TF_%TR %CF_%CR %AF_%AR %#15s [%Y] %p\n" 2>/dev/null'
    AskApache · 2013-02-06 17:54:14 2
  • This script compares the modification date of /var/lib/dpkg/info/${package}.list and all the files mentioned there. It could be wrong on noatime partitions. Here is non-oneliner: #!/bin/sh package=$1; list=/var/lib/dpkg/info/${package}.list; inst=$(stat "$list" -c %X); cat $list | ( while read file; do if [ -f "$file" ]; then acc=$(stat "$file" -c %X); if [ $inst -lt $acc ]; then echo used $file exit 0 fi; fi; done exit 1 ) Show Sample Output

    package=$1; list=/var/lib/dpkg/info/${package}.list; inst=$(stat "$list" -c %X); cat $list | (while read file; do if [ -f "$file" ];then acc=$(stat "$file" -c %X); if [ $inst -lt $acc ]; then echo used $file; exit 0; fi; fi; done; exit 1)
    pipeliner · 2010-09-20 18:10:19 0
  • Goes through all files in the directory specified, uses `stat` to print out last modification time, then sorts numerically in reverse, then uses cut to remove the modified epoch timestamp and finally head to only output the last 10 modified files. Note that on a Mac `stat` won't work like this, you'll need to use either: find . -type f -print0 | xargs -0 stat -f '%m%t%Sm %12z %N' | sort -nr | cut -f2- | head or alternatively do a `brew install coreutils` and then replace `stat` with `gstat` in the original command. Show Sample Output

    find . -type f -print0 | xargs -0 stat -c'%Y :%y %12s %n' | sort -nr | cut -d: -f2- | head
    HerbCSO · 2013-08-03 09:53:46 2

What Others Think

Thx, this looks fantastic.
oernii2 · 419 weeks ago
Yeah I love it.. Here's the main function I use instead of ls, which I save as /usr/local/bin/ll or ~/bin/ll #!/bin/sh -r _W=$((${COLUMNS:-80} - 20 ));[[ $# -eq 0 ]] && command ls -vlaph -w $_W --color=always||for a;do command ls -vlaph -w $_W --color=always "$a";done; exit $?
AskApache · 419 weeks ago
had to install the GNU findutils to get this to work as the OSX version of `find` doesn't have the -printf option. Very nice.
fukr · 347 weeks and 6 days ago
And gnu sed.
fukr · 347 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. 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.

Share Your Commands

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.


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: