9

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.

migrated from serverfault.com May 8 '15 at 15:16

This question came from our site for system and network administrators.

9

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

8

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.

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

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.

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.

0

You could achieve suche a goal with long rsync command

rsync -a --chown=root:root --chmod=F755 filename filename
  • i do not get it – Pierre.Vriens Jan 17 '18 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. – telcoM Jan 17 '18 at 11:49
-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).

  • 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. – jscott May 8 '15 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. – wurtel May 8 '15 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 
  • 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...) – Kiwy Jan 17 '18 at 13:04
  • 2
    Please do not just paste some code as an answer. – Konrad Gajewski Jan 20 '18 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. – G-Man Jan 23 '18 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.