How to change the permission and group-owner of a file at the same time
Solution 1
If what you also want is to copy the file somewhere (like its final destination), you might want to have a look at the install
command:
install -m 0777 -o root $sourcefile $destinationfile
Solution 2
There is concept known as "UNIX-way". Each tool should perform one simple function. If one need a more complex function, he can combine smaller tools.
The opposite is the monolitic design when all functionality is aggregated within one huge tool.
If you want to do something complex - just write a script, invoking simple tools.
Solution 3
Using the right tool for the job in *nix is important, but indeed repeating the same path in each chained command looks silly. Instead, you should really use Bash variables, and in smaller scripts, especially make use of $_
.
Your command would become:
chmod 123 WantToChangeThisFile && chgrp admin $_
ALT + . does the similar thing of pulling the last used argument in your current shell.
Related videos on Youtube
Raymond Tau
Updated on September 18, 2022Comments
-
Raymond Tau almost 2 years
For changing file permission, I know I could use chmod. For changing group-owner, I could use chgrp. However, if I want to change both permission and owner at the same time, any command I could use on Linux?
For example, there is a file with this permission and owner:
-rw-r--r--+ 1 raymondtau staff 0 May 8 16:38 WantToChangeThisFile
And now I want to change it to:
---x-w--wx+ 1 raymondtau admin 0 May 8 16:38 WantToChangeThisFile
I know I could use this command:
chmod 123 WantToChangeThisFile && chgrp admin WantToChangeThisFile
, but want to know if there is any neat way to do that. -
jscott about 9 years
chown
only changes the owners. How does this change the permissions at the same time as OP requested? Note OP's example includes achmod
as well. -
wurtel about 9 yearsAh, I thougth he wanted to change 3 things, as he wrote For changing group-owner I thought he meant group and owner.
-
Keith over 6 yearsHere is a script I wrote that combines these tools into one shell function,
ch
. -
Pierre.Vriens over 6 yearsi do not get it
-
telcoM over 6 yearsThis is unsafe: it will affect all matching files in the specified directory and subdirectories, not just the one file it's pointed to.
-
S edwards over 6 yearsyou could at least provide an explanation of how it works and how it's suppose to be use (parameters how much what they are...)
-
S edwards over 6 yearsthere's no way it's an answer, what he asked could be achieve with several tools already existent such as rsync and though linux tend to prefers one tool/one function it does mean tools doing multiple stuff aren't existing
-
Kondybas over 6 years@Kiwy Some linux tools do not conform unix-way principles and combine different functions of different nature. Like systemd for example.
-
S edwards over 6 years@Kondybas "tend to" is the important part of the sentence. exception to the rule does exist. Still your answer to me is a really bad one.
-
Konrad Gajewski over 6 yearsPlease do not just paste some code as an answer.
-
G-Man Says 'Reinstate Monica' over 6 yearsCommenting is not likely to be productive; this is the only thing this user ever posted, and they haven’t even logged in since then.