Add permissions to a particular file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Add permissions to a particular file
# 1  
Old 03-17-2008
Add permissions to a particular file on creation

Hi all,

I have a question regarding file permissions.

When I create a new file with a particular user user_a the default permissions for this user are -rw-r--r-- (644). I need to change the permissions for ONE PARTICULAR file from 644 to 646 when the file is created since this file will picked up by some other user_b with a scheduled job. The directory the file is located is called /apps/rawaexp and has the permissions 777.

I was thinking of changing .profile and to change the permissions for that file. Problem is that -as far as I understand- only is executed when the users logs on to UNIX. Also using umask does not seems to be much of help since it can only be used to revoke rights from a file.

Any help will be appreciated.

Endo

Last edited by Endo; 03-17-2008 at 09:22 AM..
# 2  
Old 03-17-2008
file permission

For a single file, the script/application that drops the file in that directory should make sure that the file has the right permissions. umask affects all the subsequent file creations and so is not a proper solution here.

But, you can use the umask in the script that is creating the file to get the correct permissions.

Search the forum for more information.
# 3  
Old 03-17-2008
Thanks for the answer. I forgott to mention that but we do not have access to the code which is putting the file there for political reasons. So I was hoping to get a spiffy trick for that.
# 4  
Old 03-19-2008
Another approach.
Is it possible to inherit the permissions for a newly created file not from the creator but from the directory where the file is created in ?

Thanks
# 5  
Old 03-19-2008
I think "umask" is a completely wrong direction to go to here.

ou can use "chmod" to change the properties of a single file, yes? But you do not know when this file appears because it is created by a "black-box"-script, right?

OK, first solution: you create a wrapper script for the script creating the file so you can react upon it being finished. The black-box-script is to be only executed from this wrapper script:

Code:
#! /bin/ksh

/path/to/the/unknown/script
chmod 646 /file/created/by/this/script

exit 0

Second solution: If the first solution is for any reason not possible you will have to watch for the file appearing and then change its filemode, right? Well, let a script do this somewhat boring task:

Code:
#! /bin/ksh

while : ; do
     if [ -f /file/created/by/script ] ; then
          chmod 646 /file/created/by/script
     fi
     sleep 30
done

exit 0

Save this script to a file, and execute it in background. It will have a look every 30 seconds if the file exists and if it does it will "chmod" it. 30 seconds is completely arbitrary, change that to your needs. If you do not need such a high resolution in time you could use cron instead of a "while"-loop:

Code:
# cat cronscript.sh
#! /bin/ksh

if [ -f /file/created/by/script ] ; then
     chmod 646 /file/created/by/script
fi

exit 0

# crontab -l

<...snipped...>
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /path/to/cronscript.sh

This will execute cronscript.sh every five minutes if this is enough for you. Generally speaking the lesser the frequency is the less stress it puts onto the machine, so "every 10 minutes" is better than "every 5 minutes", etc.

I hope this helps.

bakunin
# 6  
Old 03-19-2008
Hi bakunin thanks for your time.
Your second approach will do the trick and I will suggest this to my manager. I will then try to schedule all three process is a resonable way 1.) creation of file test3 by black-box-script 2.) chmod of test3 by cronjob 3.) pick-up of modified file tes3 by SAP-Job

However I have done some more searching and was thinking of using a sticky-bit approach but unfortunately this does not seem to work for me either.

This is my situation:
-rwsrwxrwx 1 dmadmin dmadmin 15 Mar 19 11:03 change.sh
-rw-r--r-- 1 dmadmin dmadmin 5 Mar 19 10:53 test3

Where test3 is the file in question and change.sh is a script to change the mod of test3 by the user dmadmin. So in change.sh you will find something like:
Code:
#! /bin/ksh
chmod 777 test3

My understanding of the sticky bit:
Code:
chmod u+s change.sh

is that if I set the sticky-bit any user can now execute change.sh as the orginal user of the script (dmadmin) itself having all the permissions and should be able to change the mode of test3 to 777.

However if I run change.sh as a different user (otheruser in this case) I get the following error message:

/apps/otheruser > change.sh
chmod: can't change test3: Not owner


We're on HP-UX.

Thanks for the help
# 7  
Old 03-19-2008
The reason for your problem is simple: out of security reasons shell scripts cannot be run in SUID mode per definition. I don't know HP-UX but this is so in every UNIX i came across so most probably this is true for HP-Ux too.

Anyways, you don't need a sticky bit, just make root to be the owner of the script and put the job in roots crontab. Then your script will be run under root authority.

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command to change add permissions for a new user to all files in all subfolders and folders

Hi there! I'm new to Unix and haven't done command line stuff since MS-Dos and Turbo Pascal (hah!), I would love some help figuring out this basic command (what I assume is basic). I'd like to add a User to the permissions of all files in a folder and all files in all subfolders, as well... (9 Replies)
Discussion started by: Janjbrt
9 Replies

2. Shell Programming and Scripting

Changing file permissions of a file created by another user

Hi, I have used expdp for datapump. The .dmp file is created by the "oracle" user. my requirement is to make a zipped file of this .dmp file. What i am trying to do is change the permissions of this .dmp file from 0640 to 0644 and then do a gzip and zip it. Is there any way i can change... (3 Replies)
Discussion started by: qwertyu
3 Replies

3. Shell Programming and Scripting

ksh; Change file permissions, update file, change permissions back?

Hi, I am creating a ksh script to search for a string of text inside files within a directory tree. Some of these file are going to be read/execute only. I know to use chmod to change the permissions of the file, but I want to preserve the original permissions after writing to the file. How can I... (3 Replies)
Discussion started by: right_coaster
3 Replies

4. Solaris

add a ftp user with read and write permissions on a directory

hi all how I can create an ftp user in solaris 10 and have read and write permission on a directory. Thanks. (1 Reply)
Discussion started by: luisfja
1 Replies

5. UNIX for Dummies Questions & Answers

File permissions

What command can i use to make sure that i cannot delete a file called backup (which i own) without affecting other permissions? seems simple dont know how i forgot. :mad: ---------- Post updated at 07:59 PM ---------- Previous update was at 06:34 PM ---------- anyone ??? ----------... (6 Replies)
Discussion started by: ink
6 Replies

6. Shell Programming and Scripting

Retain file permissions when saving .sh file from internet [OS X]

Hello. I have written a bash script that I am sharing with an OS X community I am a member of. The purpose of the script is to execute a series of commands for members without them having to get involved with Terminal, as it can be daunting for those with no experience of it at all. I have renamed... (4 Replies)
Discussion started by: baza210
4 Replies

7. Shell Programming and Scripting

file permissions

Hi all, My UNIX box is HP UX - 11.11. I have got a basic doubt. What are the global permissions for a file and directory? I set the mask as 111 in my .profile. When I create a new file, it gets created withe -rw-rw-rw- permissions. A directory is created with drw-rw-rw- permissions. So, i... (7 Replies)
Discussion started by: ranj@chn
7 Replies

8. HP-UX

To give the "unzip" permissions & "create" file permissions

Hi, I am a Unix Admin. I have to give the permissions to a user for creating new file in a directory in HP-Ux 11.11 system since he cannot able to create a new file in the directory. Thanks in advance. Mike (3 Replies)
Discussion started by: Mike1234
3 Replies

9. Cybersecurity

help with file permissions

Every time I copy files to a directory I am unable to edit the files. Why? The user is part of the group that has permissions to edit. The user can create and delete files but cannot edit files that are copied to the directory? :confused: (3 Replies)
Discussion started by: bbbngowc
3 Replies

10. UNIX for Dummies Questions & Answers

File permissions

Is there any way that I can use the ls command to view the permissions that a group has on a file. I know ls -l file1 will list all the permissions for file1. Would I have to use the following command: ls -l file1 Then grep or sed the output to retrieve what permissions the group has. (5 Replies)
Discussion started by: beginner1
5 Replies
Login or Register to Ask a Question