Apply file permission


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Apply file permission
# 1  
Old 06-25-2012
Apply file permission

Hi All,

I would like to read the permission from a file and wanted to apply the same permission to another file.

say for example,

f1 755

first...i have to read the permission type (which is differ for each file) and need to apply the same for f2

a1 666

i have to get this one and need to apply for a2 as below.

a2 666



i don't want to hardcode the file permission in my script.

my requirement is

i have to read a file and store its permission type in a variable and then i need to use it for another file.
# 2  
Old 06-25-2012
Code:
% perm=$(ls -l file1 | nawk 'BEGIN {
v["r1"]=400; v["w2"]=200; v["x3"]=100; v["s3"]=4100; v["S3"]=4000
v["r4"]=40 ; v["w5"]=20 ; v["x6"]=10 ; v["s6"]=2010; v["S6"]=2000
v["r7"]=4  ; v["w8"]=2  ; v["x9"]=1  ; v["t9"]=1001; v["T9"]=1000}
{val=0
 for (i=1;i<=9;i++) val=val+v[substr($0,i+1,1)i]
 printf "%4d\n",val}')
%
% echo $perm
666
%
% chmod $perm file2
% ls -ltr file2
-rw-rw-rw-   1 userid   gid       573 Jun 22 14:01 file2
%

# 3  
Old 06-25-2012
Code:
stat -c '%a' filename.txt

# 4  
Old 06-25-2012
Quote:
Originally Posted by itkamaraj
Code:
stat -c '%a' filename.txt

stat(1) varies a great deal from one implementation to another (e.g., BSD stat has no -c option, some other systems don't even have the utility at all).

Your suggestion is GNU stat. If GNU stat is available, then the system has GNU chmod (they're both part of GNU coreutils), in which case the task is most easily accomplished with chmod --reference f1 f2

If portability is mandatory, then something unfortunately complex, like jayan_jay's suggestion, may be required.

Regards,
Alister

Last edited by alister; 06-25-2012 at 06:51 AM..
# 5  
Old 06-25-2012
If you have getfacl and setfacl in your machine then try the below,

To set the permission of File1 to File2:

Code:
$ uname -sr
SunOS 5.9
$ getfacl File1| setfacl -f - File2
$

# 6  
Old 06-25-2012
Code:
$ chmod 000 a.txt
$ ls -l a.txt | awk -f permission.awk 
000

$ chmod 001 a.txt
$ ls -l a.txt | awk -f permission.awk 
001

$ chmod 031 a.txt
$ ls -l a.txt | awk -f permission.awk 
031

$ cat permission.awk 
function cal(str)
{
    a=0;
    if(str~/r/)
    {
        a+=4;
    }
    if(str~/w/)
    {
        a+=2;
    }
    if(str~/x/)
    {
        a+=1;
    }
}
{
for(i=2;i<=length($1);i=i+3)
{
    cal(substr($1,i,3))
    octal=octal""a;
}
print octal
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Apply md5 hash to a field in csv file

I have a .csv file and I want to md5 hash the second column for each row in the file. File is something like data1,foobar1,123,345 data2,foobar2,456,9393 data3,foobar3,1002,10109 Output would be like data1,6c81243028f8e455fa617dd5f0232ce1,123,345... (3 Replies)
Discussion started by: jjwags
3 Replies

3. Homework & Coursework Questions

Finding the directories with same permission and then apply some default UNIX commands

Write a Unix shell script named 'mode' that accepts two or more arguments, a file mode, a command and an optional list of parameters and performs the given command with the optional parameters on all files with that given mode. For example, mode 644 ls -l should perform the command ls -l on all... (5 Replies)
Discussion started by: femchi
5 Replies

4. Shell Programming and Scripting

Finding the directories with same permission and then apply some default UNIX commands

HI there. My teacher asked us to write a code for this question Write a Unix shell script named 'mode' that accepts two or more arguments, a file mode, a command and an optional list of parameters and performs the given command with the optional parameters on all files with that given mode. ... (1 Reply)
Discussion started by: femchi
1 Replies

5. Shell Programming and Scripting

Apply condition on fixed width file and filter records

Dear members.. I have a fixed width file. Requirement is as below:- 1. Scan each record from this fixed width file 2. Check for value under field no "6" equals to "ABC". If yes, then filter this record into the output file Please suggest a unix command to achieve this, my guess awk might... (6 Replies)
Discussion started by: sureshg_sampat
6 Replies

6. UNIX for Dummies Questions & Answers

Do UNIX Permission apply to sub directories?

Hi Guys, Can you tell me if unix permissions apply to sub dirs? Dir is /home/ops/batch/files/all /home is rwxrwxrwx ops is rwxrwxrwx batch is rwxr-wr-w files is rwxrwxrwx all is rwxrwxrwx Having problems writing to all (does the userid nee to be the batch owner... (1 Reply)
Discussion started by: Grueben
1 Replies

7. Shell Programming and Scripting

Re-apply the file permission

Hi, I having an issue with file permission. To fix it I need to read the file's existing permission and re-apply the same permission to the file. This has to be done for every single file under a mount point. I'm novice in scripting. Help me with this in shell scripting. # ls -l /dev/null... (10 Replies)
Discussion started by: agent001
10 Replies

8. Shell Programming and Scripting

Apply Password to already Written XLS File.

I need to apply password protection to a xls file.I had looked at SpreadSheet::WriteExcel but problem being i dont want to write the contents of file again as the formatting the file would be a pain. Is there way in which i write a entire file in one go , something like this ... (0 Replies)
Discussion started by: dinjo_jo
0 Replies

9. Cybersecurity

file permission/acl: 2 users with write access on 1 file...

Hello, i need some help/advice on how to solve a particular problem. these are the users: |name | group | ---------- --------------- |boss | department1 | |assistant | department1 | |employee | department1 | |spy | department2 | this is the... (0 Replies)
Discussion started by: elzalem
0 Replies

10. Programming

Apply patch for make file.

I just downloaded a updating make file to build Ethereal to .dll file, but I don't know how to update the old make file with this new one. Please help.. thnx a lot. (1 Reply)
Discussion started by: blueblood
1 Replies
Login or Register to Ask a Question