16

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.

7 Answers 7

15

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

2
  • 1
    I wonder if "install" supports a way to operate on an already existing file, without moving anything Commented Jan 27, 2023 at 7:33
  • This is correct, but it's not the right answer since install doesn't handle the case of working with the same source and destination, which is what the question of "changing" the attributes on a give file implies. I think it's a major flaw on install not handling this "border case" (may be with a --force option), but that is what we have unless someone convince the GNU core utilities developers on changing this behavior Commented Jun 25, 2024 at 14:59
14

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.

4
  • Here is a script I wrote that combines these tools into one shell function, ch. Commented Oct 9, 2017 at 19:59
  • 2
    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 Commented Jan 17, 2018 at 13:07
  • @Kiwy Some linux tools do not conform unix-way principles and combine different functions of different nature. Like systemd for example. Commented Jan 17, 2018 at 13:21
  • 1
    @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. Commented Jan 17, 2018 at 13:35
2

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.

2

You could achieve suche a goal with long rsync command

rsync --chown=root:root --chmod=F755 filename filename 
2
  • i do not get it Commented Jan 17, 2018 at 9:07
  • This is unsafe: it will affect all matching files in the specified directory and subdirectories, not just the one file it's pointed to. Commented Jan 17, 2018 at 11:49
0

Rsync is useful in this case:

From the Fine Manual (TM)

--chown=USER:GROUP simple username/groupname mapping

--chmod=CHMOD affect file and/or directory permissions

So, for example, you want to chmod /mnt/lala/lala4000/ "ugo=rX" and chown "foo.bar"

rsync --chmod=ugo=rX --chown=foo:bar -rvtpolgh /mnt/lala/lala4000/ /mnt/lala/lala4000/

This would recursively chown and chmod the dir.

-1

You can do chown username:groupname file ... to change both simultaneously. It's changing two fields in the same (inode) structure so combining it saves two system calls (one for reading the current values and one for setting the modified vaules).

2
  • 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. Commented May 8, 2015 at 12:40
  • Ah, I thougth he wanted to change 3 things, as he wrote For changing group-owner I thought he meant group and owner. Commented May 8, 2015 at 12:42
-3
#!/bin/bash # Setting the Variables for execution LOGDIR=$HOME CURRENTTIME=$(date +"%Y%m%d%s%H%M%S") LOGFILE=${LOGDIR}/chgpermission_${CURRENTTIME}.log MASTERFILE=$1 INDEX=0 # Checking the number of Parameters passed if [ "$#" -ne 1 ]; then echo "Master file name is mandatory parameter" > ${LOGFILE} exit 1 fi # Loading the Master file into an Array echo "Loading the Master file ${MASTERFILE} into an array" >> ${LOGFILE} while read line do #echo $line MASTERARRAY[$INDEX]=$HOME/$line INDEX=$(expr $INDEX + 1) done < ${MASTERFILE} echo "Number of parameters in an master array ${MASTERARRAY[@]}" >> ${LOGFILE} # Changing the permission of the file echo "Changing the permission of the file in master file ${MASTERFILE}" >> ${LOGFILE} index=0 for index in "${MASTERARRAY[@]}" do if [ -f "$index" ] then echo "$index file exist" echo "$index file exist" >> ${LOGFILE} chmod 755 "$index" chown workstation "$index" chgrp workstation "$index" else echo "$index file does not extst" echo "$index file does not exist" >> ${LOGFILE} fi done 
3
  • 2
    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...) Commented Jan 17, 2018 at 13:04
  • 2
    Please do not just paste some code as an answer. Commented Jan 20, 2018 at 22:03
  • 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. Commented Jan 23, 2018 at 17:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.