Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ls and chmod commands 1

Status
Not open for further replies.

rondebbs

MIS
Dec 28, 2005
109
0
0
US
Hello,

When I do a ls -l, I can see my directories have
drwxr-xr-xr. I am more used to the chmod numerical syntax like 755. Is there an easy way to list out the numerical permissions rather than rwx etc.
 
The only trick i can give you is :

relpace rwx by 421 (r=4, w=2 and x=1) and add it up

rwxr-xr-x would mean 4214-14-1 = (4+2+1) (4+1) (4+1) = 755

As for ls , i don't think it's feasable.

Brossob
 
following shell/awk script may be of use to you:

Code:
#!/bin/ksh
# # # # #
# if -l or -U or -e is specified,
#  perform real ls and awk output to convert -rwxrwxrwx to numeric form
# if no long listing,
#  just perform the real ls
longlist=no
for parameter
do
 case ${parameter} in
  -*l*|-*U*|-*e*)
   longlist=yes
   ;;
 esac
done
if [ "${longlist}" = 'yes' ]
then
 /usr/bin/ls $*|awk '{
 # # #
 # look for the field containing permission bits
 for (i=1;i<=NF;i++) {
  if ($i ~ /^[bcdlps-][r-][w-][xsS-][r-][w-][xsS-][r-][w-][xtT-][E+-]*/) {
   num=0;
   # # #
   # break up into individual bits
   tp=substr($i,1,1);
   ur=substr($i,2,1); uw=substr($i,3,1); ux=substr($i,4,1);
   gr=substr($i,5,1); gw=substr($i,6,1); gx=substr($i,7,1);
   or=substr($i,8,1); ow=substr($i,9,1); ox=substr($i,10,1);
   ea=substr($i,11,1);
   # # #
   # substitute type and extended attributes flags if necessary
   if (tp=="-") tp=" ";
   if (ea=="-") ea=" ";   if (ea=="") ea=" ";
   # # #
   # convert to numeric representation
   if (ur=="r") num+=400; if (uw=="w") num+=200;
   if (ux=="x") num+=100; if (ux=="s") num+=4100; if (ux=="S") num+=4000;
   if (gr=="r") num+=40;  if (gw=="w") num+=20;
   if (gx=="x") num+=10;  if (gx=="s") num+=2010; if (gx=="S") num+=2000;
   if (or=="r") num+=4;   if (ow=="w") num+=2;
   if (ox=="x") num+=1;   if (ox=="t") num+=1001; if (ox=="T") num+=1000;
   # # #
   # print out type of file, numeric representation
   # and possibly the extended attributes indication
   printf "%s%04d%s ", tp, num, ea;
   # # #
   # found the permissions field, no need to continue searching
   break;
  }
 }
 # # #
 # print out original line
 print
}'
else
 # no longlist
 /usr/bin/ls $*
fi

If you name this script /usr/local/bin/numls, chmod 755 it and use an alias ls=numls, then you can just use 'ls -whatever whichever' and get the modified output from the script, provided of course /usr/local/bin is in your search PATH.


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top