Create file with 751 permissions at creation

3

1

I keep creating practice perl scripts in a linux directory using vi <filename> and need to chmod 751 <filename> before I can run it as I wish to. I'm sure there is a simple way to default my permissions or config them at creation, but I'm not familiar with it - ayuda me por favor.

mbb

Posted 2011-04-05T19:18:16.017

Reputation: 2 206

1Why don't you write a script to create a new one with the correct permissions? #!/bash/bin touch $1; chmod 751 $1 – beatgammit – 2011-04-05T19:19:49.813

Answers

2

A simple solution would be to make a bash alias for vi which will first create the file and then change it's permissions before opening it. Use something easy to remember, like vix (x for executable bit):

e.g. (untested):

function vix {
        touch $1
        chmod 751 $1
        vi $1
}

Note that aliasing vi itself is a bad idea as many other programs call on it directly, which may cause issues with your system.

John T

Posted 2011-04-05T19:18:16.017

Reputation: 149 037

1Re "aliasing vi itself is a bad idea as many other programs call on it directly": They would be unaffected by aliases or shell functions. (system() uses sh -c which ignores all rc files, and exec*() skip the shell entirely.) ...edit: apparently, exporting a shell function does affect sh -c. – user1686 – 2011-04-05T19:39:47.670

Depends on how the script/program is invoking it, but generally correct. – John T – 2011-04-05T19:41:55.713

all solid points -- now I know I'm not not missing a hidden ability of vi itself. I use both root & user dev on this system - which files do I need to edit to cover both? – mbb – 2011-04-05T21:00:55.950

1

751 doesn't make much sense for scripts - it allows "others" to execute the file, but not read it. (For scripts, which don't get setuid honored, this is impossible.)

To set default permissions for newly created files, you can use either umask (per-process) or default ACLs (per-directory). But they both can only remove permission bits; neither of them will add the "executable" bit automatically unless the program specifically asks for it. (For example, fopen() always requests 0666, the sane default for files.)

You will have to follow mjb's suggestion of writing a script that creates a file and immediately updates its permissions. Alternatively, run your practice scripts like perl foo.pl, which does not require the executable bit.

user1686

Posted 2011-04-05T19:18:16.017

Reputation: 283 655

good points - just so you know, I don't want coworkers who I may share the code w/being able to read the content of the file, but I don't mind if they run it. Hint that 1 at the end. – mbb – 2011-04-05T20:58:12.397

@mjb: As I said in the first paragraph of my answer, I'm not sure how that could possibly work. – user1686 – 2011-04-06T05:57:21.973