2

In my bash script I want to change file permissions on a particular file called "test.txt" which is located at :

"/var/www/tomcat7/dir1/test.txt"

My question is if I'm giving this full path to the file, I want to make change the permission on all the directories like, "var", "www", tomcat7", "dir1", and finally "test.txt".

File path is given via a separate text file as command-line arguments, and here is my code,

setFilePErmission(){
    ssh [email protected] "sudo chmod 777 $1"
}

setFilePErmission $1

Can anyone help me? Thank You.... :)

5
  • 1
    In this case you can do chmod -R 777 /var and all the hierarchy below will get this change done. Commented Apr 28, 2014 at 11:49
  • Hello @fedorqui i want to give the file path at once and need to set permission on all the directories which includes. Commented Apr 28, 2014 at 11:51
  • 1
    What I mean is that if you use -R (recursive), chmod in /var will automatically chmod in all directories inside /var, so you won't have to do chmod /var/www and then chmod /var/www/tomcat7/ and so on. Commented Apr 28, 2014 at 11:52
  • Why would you want to give such permission? The solution which is told by fedorqui will make everything under /var with 777 permission it will include www and any other dir under /var like /var/log as well and same 777 permissions for all other subdir and files under /var. If you do not have issue with then you can procced with chmod -R 777 /var else you will have to find another way. Commented Apr 28, 2014 at 12:05
  • @Jord as u told that is a problem actually, so is there any specific way to proceed permission on selected path only? Commented Apr 28, 2014 at 12:07

1 Answer 1

1
#!/bin/bash 
setFilePErmission(){  
i=$(echo "$1" | awk -F '/' '{print NF}') 
y=$1 
while [[ $i -gt 1 ]] 
do 
ssh [email protected] "sudo chmod 777 $y" 
y=${y%/*} 
(( i-- )) 
done
}

setFilePErmission "your path goes here"

Check if this works for you.

I am still doubtful, why would one need such permissions..

Please be sure while running such thing because once you change permission it will be very difficult to get them to previous values unless you dont remember each and every file permissions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.