10
1
This is probably a duplicate, but all my searches are turning up questions about permission denied errors.
I am running a command in a bash shell. I want to redirect output to append to a file that probably does not exist on the first run. I want to set specific file permissions mode if output redirection has to create this file. Is there a way to do this with one command?
For example, I might try
foo >> /tmp/foo.log 0644
where 0644
are the permissions I want foo.log
to end up with. Most commands I've experimented with in bash end up interpreting 0644
as an additional argument to foo
.
I get the feeling that this is going to take a second command to chmod
the permissions before or after writing to it.
I am using GNU bash 4.2.25 and Ubuntu 12.04, if that makes a difference - general answers are preferred.
3The problem with this approach is that it creates a short time window when the permissions on the file are wrong. I would not use it if the goal is to protect sensitive data. – proski – 2015-10-14T17:04:31.850
1This answer works, as far as it goes, but @proski is right: the
umask
answers are better, and one of them should be accepted. – Scott – 2019-06-12T18:11:22.643Thanks Slowki, that was my hunch as well. I'm going to leave it open for a few days, in the hopes of attracting a guru to enlighten us. – Patrick M – 2014-01-21T17:30:09.083