1850932 Members
3067 Online
104056 Solutions
New Discussion

Re: list file mode

 
ikbea
Frequent Advisor

list file mode

Hi all,

Run ls -l
-rwxr-x--- 1 owner group size date filename

How to get filename in mode 750 instead of
-rwxr-x---

Thanks ?
6 REPLIES 6
Geoff Wild
Honored Contributor

Re: list file mode

Uh...-rwxr-x--- is 750 - you mean you want it to display as 750?

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
ikbea
Frequent Advisor

Re: list file mode

I prepared a script for get mode of filename. For some reason, I want to get 750 (number) instead of -rwxr-x---.
Ivan Ferreira
Honored Contributor

Re: list file mode

You could create a script and use the cut command and case to convert the permissions, something like this logic:

result = cut from 2-4
case result = rwx then mode 700
case result = rw- then mode 600
case result = r-- then mode 400
and so on

result = cut from 5-7
case = rwx then mode 700+70
and so on

Probably some good c programmer on this forum could do this easily.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Muthukumar_5
Honored Contributor

Re: list file mode

Use this:

#!/usr/bin/perl

# Input is like permission only = -rwxrw-rw-

while (<>)
{
@arr=split //;
for ($i=1;$i<=3;$i++) {
if ( $arr[$i] =~ /r/ ) {
$own+=4;
}
if ( $arr[$i] =~ /w/ ) {
$own+=2;
}
if ( $arr[$i] =~ /x/ ) {
$own+=1;
}
if ( $arr[$i] =~ /s/ ) {
$own+=1;
$SID+=4;
}
if ( $arr[$i] =~ /S/ ) {
$SID+=4;
}
}
for ($i=4;$i<=6;$i++) {
if ( $arr[$i] =~ /r/ ) {
$group+=4;
}
if ( $arr[$i] =~ /w/ ) {
$group+=2;
}
if ( $arr[$i] =~ /x/ ) {
$group+=1;
}
if ( $arr[$i] =~ /s/ ) {
$group+=1;
$SID+=2;
}
if ( $arr[$i] =~ /S/ ) {
$SID+=2;
}
}
for ($i=7;$i<=9;$i++) {
if ( $arr[$i] =~ /r/ ) {
$other+=4;
}
if ( $arr[$i] =~ /w/ ) {
$other+=2;
}
if ( $arr[$i] =~ /x/ ) {
$other+=1;
}
if ( $arr[$i] =~ /t/ ) {
$other+=1;
$SID+=1;
}
if ( $arr[$i] =~ /T/ ) {
$SID+=1;
}
}
print $SID . $own . $group . $other . "\n";
}

save it as mode.pl

Execution:

# echo "-rwsrwsrw-" | perl mode.pl
6776
# echo "-rwsrwsrwT" | perl mode.pl
7776

Or simply as,

# ls -l | awk '{ print $1; }' | perl mode.pl

--
Muthu
Easy to suggest when don't know about the problem!
Peter Godron
Honored Contributor

Re: list file mode

Hi,
can I suggest you have a look at:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=60179

Previously asked question
Ralph Grothe
Honored Contributor

Re: list file mode

Hi,

if you already use Perl for the task,
why not use Perl's built ins (viz. stat())?

e.g.
you could collect mode bits in a file to mode hash like this

my $dir2stat = '/etc';
my $dh = do { local *DH };
opendir $dh, $dir2stat or die "Cannot opendir(): $!\n";
my %mbits;
map { $mbits{$_} = sprintf '%04o', (stat)[2] & 07777 }
map "$dir2stat/$_",
grep(!/^\.{1,2}/, readdir $dh);
closedir $dh;


For details please refer to "perldoc -f stat".

HTH
Ralph
Madness, thy name is system administration