Gather File permission during scripting on unix as numbers.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Gather File permission during scripting on unix as numbers.
# 1  
Old 08-22-2008
Gather File permission during scripting on unix as numbers.

Hi,

I have a script with following file permission on box.
-rwxr-xr-x 1 root system 15347 Aug 14 15:08 b_reboot.ksh

Without calculating or watching at -rwxr-xr-x (permission's) of this above mentioned file. I would like to get the file permission assigned to a file.

Basically looking for after issuing some commands <file name>, I should get the permission of provided file in a variable as 755. (as a number, either 755/644..or what ever it is.)

Example,

$some_command <file_name>
$permission=$?

And the output which I looking forward is like this.
$print ${permission}
755

Do we have any command which can substitute "some_command" like this on Unix.?

Note:
------
I don't like to go for the manipulation using the ls -l |grep <filename> |awk '{print $1}'
Looking for any thing which can be easily done to retrieve permission of file in numbers.
# 2  
Old 08-22-2008
do you mean set file permissions to 755?
[code]chmod 755 filename[/url]

If you mean get the permissions, and you are in Linux check out the stat command. That would be
Code:
man 1 stat 
#or
stat --help

# 3  
Old 08-22-2008
from what I read, ajilesh is wanting to see what the permissions are in a non-volatile way. I don't know how you do this in csh, but in ksh you can do this:

if [[ -x /usr/lib/sendmail ]] this is true if the user doing the condition, can execute /usr/lib/sendmail
if [[ -r /usr/lib/sendmail ]] this is true if the user doing the condition, can read /usr/lib/sendmail
if [[ -w /usr/lib/sendmail ]] this is true if the user doing the condition, can write /usr/lib/sendmail

but this only checks the permissions of the user performing the, in this case, if/then/else has those permissions. I do not know of any way to convert rwxrw-rw- to the octal 755 to then confirm you have the correct permissions on said file or directory. I have run into this same problem in my own scripting because the file I was checking on needed to be 766 and I was trying to track down when the permissions got changed so I could figure out what was changing the permissions, the only option I found was to ls -l <file> then pipe that into a grep on "rwxrw\-rw\-" to confirm (yes, \- is necessary to ensure it doesn't try to treat -rw as an option some how) it had the correct permissions.

while using chmod is a good suggestion, chmod sets the permissions without regard to the previous state.
# 4  
Old 08-22-2008
James, well, you are correct. I'm looking for any way to convert rwxrw-rw- to the octal 755.. Do we have any internal unix commands where we can trace the octal which is assigned to a file or directory directly.
# 5  
Old 08-22-2008
find . -name "*" -maxdepth 1 -printf '%m %p\n'
or
stat -c "%a %n" *
# 6  
Old 08-22-2008
Quote:
Originally Posted by sudhamacs
find . -name "*" -maxdepth 1 -printf '%m %p\n'
or
stat -c "%a %n" *
both are not working in AIX..Smilie
# 7  
Old 08-22-2008
If you have Perl, you can use that as a workaround.

Code:
perl -e 'for (@ARGV) { printf "%04o %s",  (stat)[2] & 07777, $_ }' files ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to Gather data from logs and export to a CSV file

Greetings, After a few hours of trial and error, I decide to ask for some help. I am new to AWK and shell script, so please don't laugh :p I made the below script, to gather data from some logs and have the output into a CSV file : #!/bin/sh #Script to collect Errors ... (9 Replies)
Discussion started by: Yagami_Sama
9 Replies

2. UNIX for Dummies Questions & Answers

How can I use my code to gather information from a file in a completely different directory?

I need my code to compare two different files that are in two completely different directories, How can I do this? So for example, my code will look at file1 which is in my home directory, and compare the files with those from file2 that is in /abc/adf/adr/afc/adf/file2... does that make sense? (1 Reply)
Discussion started by: castrojc
1 Replies

3. UNIX for Dummies Questions & Answers

Change unix permission when I don't own the file

Hi, A file is transferred from a Windows server(say username : user1) to Unix server via ftp. In unix, the permission of the file for a user, say user2 will be "-rw-r-----". Since the user1 is the owner of the file, user2 is not able to change the file permission using chmod. Is there... (5 Replies)
Discussion started by: merin
5 Replies

4. UNIX for Dummies Questions & Answers

How to find out who changed the file permission in unix

Can any one tell me is there any command to find out who changed the permission of a file Or is there any log file so that i can find out who has changed the permission of a file? Thanks in Advance:) (7 Replies)
Discussion started by: Uttamnsd
7 Replies

5. UNIX for Dummies Questions & Answers

Simple question on unix file permission

As I understand the file permissions in UNIX is basically Owner, group, others Lets assume scott user who's primary group is dev creates a file called test.dat and then grants some privileges on that file... scott@unix-host> echo "this is a test" > test.dat scott@unix-host> chmod 640... (4 Replies)
Discussion started by: luft
4 Replies

6. Shell Programming and Scripting

How to rename a file even when it shows permission Denied in Unix

While executing a script, I am not being able to able to create a file as the file with the same name already exists. That existing file is not getting overwritten as I am not the owner of the file. So, Neither am I able to rename the file nor delete the existing file, so as to get my file created.... (2 Replies)
Discussion started by: Haimanti
2 Replies

7. Shell Programming and Scripting

How to create a file with full permission in unix script

In my script, I am creating a file ----> then writting one line (i.e. Timestamp) ----> then FTP'ing. The same script can be executed by many other users. While other users executing this script, they couldn't Over write this one line (i.e. Timestamp) My expectation So I wanted to create a... (2 Replies)
Discussion started by: sbmk_design
2 Replies

8. UNIX for Advanced & Expert Users

UNIX file Permission

I am trying to FTP files from windows to UNIX (IBM AIX). After having sent the files to unix server. Permisssion of the files becomes 640 (rw-r-----). I have to manually login to unix and do chmod 644 on the folder to give it permission. Is it possible that the files automaically be set to 644 on... (2 Replies)
Discussion started by: puspendu.das.in
2 Replies

9. Shell Programming and Scripting

How to create file in ksh scripting with permission(rw-rw-rw)

Hi, Please provide your inputs.. Thanks in Advance, Mansa (1 Reply)
Discussion started by: mansa
1 Replies

10. Shell Programming and Scripting

Scripting file permission problems...

Hello all - I have two systems. 1) Linux box running Redhat 8.0 2) Tru64 box running V4.0f From the Linux box I am remotely mounting a directory (nfs mount) that resides on the Tru64 machine. The directory that is nfs mounted contains two subdirectories: my_dir1 my_dir2 I want... (3 Replies)
Discussion started by: Heron
3 Replies
Login or Register to Ask a Question