stat command with ls -l

If i do ls -l i get the result rwx-rw-r ...... ............... file.
How can i get the result in octal format.
All other output will be the same as ls -l shows.
The rwx-rw-r would be like 755 etc.

On some OS there are commands that have printf style formatting. For instance, this works on my current system (Linux):

$ stat -c "%a" file
755
$ find file -printf "%m\n"
755

or perhaps you can filter the stat command

No the rwx-rw-r ,octal format would be shown for this place,but other result from ls -l would be same.
Suppose

total 768
drwxr-xr-x 2 user1 users   4096 2010-07-16 23:45 Desktop
-rwxrwxr-x 1 user1 users 744955 2010-06-18 16:52 desktop.JPG
drwxr-xr-x 2 user1 users   4096 2010-07-16 22:13 Documents
drwxr-xr-x 3 user1 users   4096 2010-08-11 13:44 Download
drwxr-xr-x 2 user1 users   4096 2010-08-09 08:27 dwhelper
drwxr-xr-x 2 user1 users   4096 2010-07-16 22:17 Music
drwxr-xr-x 2 user1 users   4096 2010-07-16 22:17 Pictures
drwxr-xr-x 2 user1 users   4096 2010-07-16 22:17 Public
drwxr-xr-x 2 user1 users   4096 2010-08-10 05:26 public_html
drwxr-xr-x 2 user1 users   4096 2010-07-16 22:17 Templates
drwxr-xr-x 2 user1 users   4096 2010-07-16 22:17 Videos

Octal format would replace drwxr-xr-x.The right side of the output would be same.

Something like this?

ls -l |awk 'BEGIN{
  a["---"]=0
  a["--x"]=1
  a["-w-"]=2
  a["-wx"]=3
  a["r--"]=4
  a["r-x"]=5
  a["rw-"]=6
  a["rwx"]=7
} 
NR > 1{
  owner=substr($1,2,3)
  group=substr($1,5,3)
  other=substr($1,8,3)
  $1=substr($1,1,1) FS a[owner] a[group] a[other]
}
{print}
'

That would work for basic access rights but not for the s,S,t an T characters that denote sticky bits and set[ug]ids...
This should also work for those cases if the stat command is present...:

ls -l | 
{ 
read total
echo $total
while read -r a b c d e f g h
do
  printf "%4s %3d %8s %8s %6d %s %s %s\n" "$(stat -c "%a" "${h% -> *}")" "$b" "$c" "$d" "$e" "$f" "$g" "$h"
done
} 

albeit a bit slow.. :slight_smile:

Hi Scrutinizer,

You're right, but I'll leave this as an exercise for the OP :slight_smile:

Regards

a small correction

 
...........
while read -r a b c d e f g h i;
  do
    printf "%4s %3d %8s %8s %6d %s %s %s %s\n" "$(stat -c "%a" "${i% -> *}")" "$b" "$c" "$d" "$e" "$f" "$g" "$h" "$i";
 done
 

That would be dependent on how many columns ls -l produces (which depends on date/time formatting I think). On my system h corresponds to the file name...