Operating System - HP-UX
1827733 Members
3031 Online
109968 Solutions
New Discussion

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

 
SOLVED
Go to solution
Mike_305
Super Advisor

Display the file permission (rw-r--r--) in to numbers on lots of file

Hello,

I need help on capture the listing of lots of files in multiple file systems and directories and instead of listing this file permission as (rw-r--r--) this I need to display in traditional permission format?

Is there any way to do this in HP-UX?

I have been running some thing like this to get the information.

find /m4 -type f -exec ll -d {} \; > file.perm.lst

Appreciate your help in advance.

Regards,

MP
If there is problem then don't think as problem, think as opportunity.
21 REPLIES 21
Mike_305
Super Advisor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hi,

One more correction to my note.

I need the permission listing in "number" format.

r--r--r-- = 444

Sorry if I confuse the crap out of you guys.

Regards,

Mike
If there is problem then don't think as problem, think as opportunity.
Dennis Handly
Acclaimed Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

>I need the permission listing in "number" format. r--r--r-- = 444

Why would you want this obsolete format?
I have a tool that converts to the symbolic format:
chmod u=r,g=r,o=r

See my scripts in this thread:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1215123

It shouldn't be that hard to convert to octal numbers if you insist.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hi:

Use Perl and you could do:

# cat ./showperms
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my $path = shift || qq(.);
find(
sub {
if ( -f $_ ) {
my $mode = ( stat(_) )[2];
printf "%04o %-s\n", $mode & 0777, $File::Find::name;
}
},
$path
);
1;

...run as:

# ./showperms /path

...or to analyze the current directory run without arguments:

# ./showperms

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

I got the attached from someone (Geoff Wild?) a long time ago.


Pete


Pete
Mike_305
Super Advisor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hello,

The reason I need the number format because after the upgrade of the app if things needs to go back the way it was then I wanted run quick loop to restore the permission of the file because we are change the permission on the files prior to upgrades.

Regards,

MP
If there is problem then don't think as problem, think as opportunity.
James R. Ferguson
Acclaimed Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hi (again):

> The reason I need the number format because after the upgrade of the app if things needs to go back the way it was then I wanted run quick loop to restore the permission of the file because we are change the permission on the files prior to upgrades.

That was pretty clear. So what's wrong with the Perl script I offered? It produces output like:

# ./showperms /etc | head
0644 /etc/fstab
0444 /etc/group
0444 /etc/passwd
0444 /etc/magic
0444 /etc/nfssec.conf
0644 /etc/hosts
0444 /etc/services
0444 /etc/inetsvcs.conf
0664 /etc/.prev.ioconfig
0600 /etc/lvmtab

Regards!

...JRF...
Mike_305
Super Advisor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hello JR,

Thanks a lot, I was not questioning anyone but just trying to reply Dennis's question.

Thanks for your help that is what I was trying to accomplish.

Appreciate your help. Now I have to figure out the loop just in case if I have to revert back things.

Regards,

MP
If there is problem then don't think as problem, think as opportunity.
Patrick Wallek
Honored Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

>>Now I have to figure out the loop just in case if I have to revert back things.

Assuming you use JRF's PERL snippet and send the output to a file, this should come close to working:

while read PERM FILE
do
echo "Changing permission on ${FILE} to ${PERM}"
chmod ${PERM} ${FILE}
done < perm-list
Mike_305
Super Advisor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hello JR,

Is it possible to make your code read under the link fron the root level?

Patrick, great code, thanks for the information. Love the way it gives the format. But not reading under the directory level. I can stick with JR's script.

Thanks,

MP
If there is problem then don't think as problem, think as opportunity.
James R. Ferguson
Acclaimed Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hi (again) MP:

> Is it possible to make your code read under the link from the root level?

You can pass any argument to the script that represents a directory or filesystem. If you want to start at the root filesystem, do:

# ./showperms /

Regards!


...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hi (again):

A small change to the script allows us to pass one or more arguments. Once again, passing nothing infers search the current working directory. Use:

# cat ./showperms
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my @dir = @ARGV ? @ARGV : ('.');
find(
sub {
if ( -f $_ ) {
my $mode = ( stat(_) )[2];
printf "%04o %-s\n", $mode & 0777, $File::Find::name;
}
},
@dir
);
1;

...to do things like:

# ./showperms /etc /sbin /usr/local/bin

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

>The reason I need the number format because after the upgrade of the app if things needs to go back the way it was then I wanted run quick loop to restore the permission of the file

That was the purpose of those scripts on that thread.
Mike_305
Super Advisor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hello JRF, Dennis, and Patrick

Thanks for your help, I got what I wanted but just a quick question.

On the script that Patrick attached, can it be possible to make it read underneath the directory level and all the files under that directory?

Once again, thanks for all your help.

See attached.

Regards,

MP
If there is problem then don't think as problem, think as opportunity.
Patrick Wallek
Honored Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

The script that I posted takes a file as input. It will act on whatever is in that file. The file I assume it is reading is the output of the script JRF posted which has the numeric permission and file name on each line of the file.

For example:

# ./showperms /etc
0644 /etc/fstab
0444 /etc/group
0444 /etc/passwd
0444 /etc/magic
0444 /etc/nfssec.conf
0644 /etc/hosts
0444 /etc/services
0444 /etc/inetsvcs.conf
0664 /etc/.prev.ioconfig
0600 /etc/lvmtab

(From one of JRFs posts above)

To make that script send its ouput to a file, you would run it like:

# showperms /etc > perm_file

The you would use perm_file as the input for my script.

Run JRF's script on whatever directory you want.
Mike_305
Super Advisor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hello Patrick,

Sorry I was not clear in my note. I was talking about the getmod.sh (attachment) that you put in your post.

I apologize for my confusions.

Regards,

MP


If there is problem then don't think as problem, think as opportunity.
Patrick Wallek
Honored Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

The 'getchmod.sh' wasn't mine, it was Pete Randall's. Anyway, it appears that the script will work on any directory you feed it.

You could do a 'getchmod.sh /*' and it looks like it should work from the / directory onwards.

Patrick Wallek
Honored Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Regarding the 'getchmod.sh' script -- If you run the script as 'getchmod.sh /*' are you wanting it to be recursive and just go through ALL directories and files on your system?

Patrick Wallek
Honored Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Perhaps something like this is what you are wanting?

for DIR in $(find / -type d -print)
do
getchmod.sh ${DIR}/* ${DIR}/.*
done

This will find ALL directories on the system, starting at /, and run getchmod.sh on them, including any hidden directories.
Mike_305
Super Advisor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hello Patrick,

>>>>>> Regarding the 'getchmod.sh' script -- If you run the script as 'getchmod.sh /*' are you wanting it to be recursive and just go through ALL directories and files on your system? >>>>>

You right that was Pete, it was too late when I was replying this last night. :-(

JRF's script was awesome, the only reason I was messing around with "getmod" because it was giving me output in one file but it was not doing *Recursive* even if I was doing 'getchmod.sh /*'.

Currently, I have to two different commands to get the ownership (using find connand) and permission in (number format using JRFâ s).

find /m4/* -type f -exec ll {} \; | awk '{printf $1" "$3":"$4" "$9"\n"}' > perm-list.

-r--r--r-- c346538:edi /m4/app16/test1.db

This gives me the format and then I run your little loop to restore things back (C.M.B).

I will try your last loop and let you know.

I think I have all the pieces in place to do what I need.

Thanks again to JRF, Patrick, Dennis and Pete and all the guys that reply to my note.

Thanks,
MP
If there is problem then don't think as problem, think as opportunity.
Mike_305
Super Advisor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Hello,

The last loop Patrick suggested has worked, thanks for your help.

Once again, thanks to JRF, Patrick, Pete and Dennis.

Regards,

MP
If there is problem then don't think as problem, think as opportunity.
Tingli
Esteemed Contributor

Re: Display the file permission (rw-r--r--) in to numbers on lots of file

Again, although it is a little too late, I still put this on:

ls -l | awk '{print $1" " $9}' | while read PERM FNAME
do
DPERM=$(echo $PERM | tr rwx 421 | sed -e 's/./ &/g' | awk '{s1=$2+$3+$4; s2=$5+$6+$7; s3=$8+$9+$10; print s1 s2 s3}'
echo "DPERM $FNAME"
done