How to change the permission and group-owner of a file at the same time

14,237

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.

Share:
14,237

Related videos on Youtube

Raymond Tau
Author by

Raymond Tau

Updated on September 18, 2022

Comments

  • Raymond Tau
    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
    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 a chmod as well.
  • wurtel
    wurtel about 9 years
    Ah, I thougth he wanted to change 3 things, as he wrote For changing group-owner I thought he meant group and owner.
  • Keith
    Keith over 6 years
    Here is a script I wrote that combines these tools into one shell function, ch.
  • Pierre.Vriens
    Pierre.Vriens over 6 years
    i do not get it
  • telcoM
    telcoM over 6 years
    This 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
    S edwards over 6 years
    you 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
    S edwards over 6 years
    there'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
    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
    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
    Konrad Gajewski over 6 years
    Please do not just paste some code as an answer.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 6 years
    Commenting is not likely to be productive; this is the only thing this user ever posted, and they haven’t even logged in since then.