stat command with ls -l


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting stat command with ls -l
# 1  
Old 08-10-2010
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.
# 2  
Old 08-11-2010
On some OS there are commands that have printf style formatting. For instance, this works on my current system (Linux):
Code:
$ stat -c "%a" file
755
$ find file -printf "%m\n"
755

or perhaps you can filter the stat command
# 3  
Old 08-11-2010
Quote:
Originally Posted by Scrutinizer
On some OS there are commands that have printf style formatting. For instance, this works on my current system (Linux):
Code:
$ 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
Code:
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.
# 4  
Old 08-12-2010
Quote:
Originally Posted by cola
No the rwx-rw-r ,octal format would be shown for this place,but other result from ls -l would be same.
Suppose
Code:
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?
Code:
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}
'

# 5  
Old 08-12-2010
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...:

Code:
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.. Smilie

Last edited by Scrutinizer; 08-12-2010 at 05:48 AM..
# 6  
Old 08-12-2010
Quote:
Originally Posted by Scrutinizer
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...
Hi Scrutinizer,

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

Regards
# 7  
Old 08-12-2010
Quote:
Originally Posted by Scrutinizer
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...:

Code:
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.. Smilie
a small correction
Code:
 
...........
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question on stat command

hello, I wanted to know which is the output of the stat command with a file, for example if I write on the terminal: stat ./unix.pdf i get the output: 754974726 6915670 -rwxrwxrwx 1 mbruno106 staff 0 90501 "Aug 13 13:26:02 2013" "Aug 13 13:26:02 2013" "Aug 13 13:26:02 2013" "Aug 13... (1 Reply)
Discussion started by: Marina2013
1 Replies

2. UNIX for Dummies Questions & Answers

Help with stat command

Hi Experts, I am here with very simple request: #!bin/bash a=`stat -c %y log1.csv` echo $a and this stat command returning value as 2013-08-11 05:42:10.000000000 -0400: But I want to see in mm/dd/yyyy format? any help is highly appreciated thank you ---------- Post... (9 Replies)
Discussion started by: parpaa
9 Replies

3. Shell Programming and Scripting

Stat value changes

Die to what all operations, the "Modify" and "Change" values of stat output changes for a file. I found, during editing a file, Change and Modify alters. When chmod'ing Change alters, while Modify doesnot alters. Is there more situations where these changes? (1 Reply)
Discussion started by: anil510
1 Replies

4. UNIX for Dummies Questions & Answers

Stat command

i know this command does not exist in solaris. however, i read somewhere on this forum that basically everything the stat command provides in other oses can be obtained in solaris using the ls command. i've searched the forum for a while now and i cant find the thread. does anyone know about... (1 Reply)
Discussion started by: SkySmart
1 Replies

5. Shell Programming and Scripting

AIX and HP-UX equivalent of Linux stat command

To list file permission/access right in octal format, linux has a command 'stat'. For example, we can use the followin - stat -c %a `find . -type f Is there any equivalent command in AIX and HP-UX to give the same result as linux 'stat' command? Please advice. (3 Replies)
Discussion started by: atanubanerji
3 Replies

6. Shell Programming and Scripting

Need file timestamp without stat command

Hi all, I want to check whether a file is not updated in last 15 minutes, for this i need to get timestamp of file, (yyyy:mm:dd:hh:mi:ss). I dont have access to STAT command :(. Please suggest a program or command to do this . Thanks, Saravana (1 Reply)
Discussion started by: sam_1210
1 Replies

7. Windows & DOS: Issues & Discussions

Does PowerSHell have *stat command analogs?

I.e. like vmstat, cpustat, iostat, fsstat, kstat ..etc? (1 Reply)
Discussion started by: Xcislav
1 Replies

8. Shell Programming and Scripting

hp-unix stat command to get last change date of file

I'm on hp-unix. I would like a variable to hold the last change date of a file. I looked at the man pages for stat, but I don't see any examples and can't get the syntax right. Can anyone help me? Thank you. (2 Replies)
Discussion started by: sboxtops
2 Replies

9. Shell Programming and Scripting

Equivalent command to 'stat'

Can anyone tell me which is the equivalent command to 'stats' in ksh shell which discribes the file system? Thanks in advance Regards, Im_new (6 Replies)
Discussion started by: im_new
6 Replies

10. Shell Programming and Scripting

stat command

how can ý use "stat command"????.. (2 Replies)
Discussion started by: emreatlier
2 Replies
Login or Register to Ask a Question