Assigning file attributes to variables

Hi,

I'm trying to assign the permissions, owner and group of a file to seperate variables, but using

ls -l filename | awk '{print $1 "\t" $3 "\t" $4}'

gives the owner as tom.ja instead of tom.james
Is there any way to expand it so i get the full name, or is there an easier way to get them into variables?

Also, i'm processing the permissions into a table ie Owner has READ and EXECUTE permissions, Group has... etc. I was thinking to use a shift to deal with each permission, is there any way to now seperate each permission? ie.
-rwxr-x-r-- into

  • r w x r - x - r - -
    it doesn't have to be whitespace, but just so i can process each one.

Thanks

Oliver

Dear Oliver,

I am not sure about your first problem as it's working fine on my machine as far as tour second problem is concern, you can try this code.

ls -ltr filename | awk '{
for (i=1;i<10;i++)
print(substr($1,i,1));
}'

regards
Apoorva kumar

regarding your question about permission,user and group..you can use it with cut.

line=`ls -l in2.txt`
perm=`echo $line| cut -d" " -f1`
owner=`echo $line| cut -d" " -f3`
grup=`echo $line| cut -d" " -f4`
echo $perm
echo $owner
echo $grup

Another way to parse file attributes :

ls -l x.sh | read perms links owner group size mtime1 mtime2 mtime3
 file
echo "$perms | $links | $owner | $group | $size | $mtime1 $mtime2 $mtime3 | $file"

The output is :

Jean-Pierre.

Thanks for the advice

Thanks Apoorva, this works great! As for the first question, if the owner name isn't cut short with you, maybe the columns are too small on mine, is there any way to widen them so i get the full username?

Dhruva, could you explain what f1, f3 and f4 do? As when i tried your code the only output is from $perm which is
-rwxr-xr-x

And i'm afraid John-Pierre that yours only prints out the | !!

Thanks again for your help,

Oliver

Dear Oliver,

I have not faced any colmn size problem so far. Still if you think so, you can also try 'printf' function. As far as f1,f2...are concern they just indecate the respective fields.

regards
Apoorva Kumar