I’m trying to change all the permissions for the files in my ~/Documents folder and I thought the following little loop would work, but it doesn’t seem to be working. Can anyone help me? Here’s the loop:
files=~/Documents/*
for $file in $files {
chmod 755 $file }
I’m trying to write this directly into the bash command line gives me the following error:
-bash: syntax error near unexpected token `chmod'
thanks for your help/advice
The loop should be :
Please note that you are relying on globbing when you say
files=~/Documents/*which will include the directories under~/Documentsas well, thus using the loop will change permissions of the directories as well. You can add a simple test to make sure that you update only the permission of files. Also please note that globbing can be turned off withset -fin which casefileswill have value of just<HOME_DIR>/*which maynot be a filename; which also will not serve your purpose.More importantly, as pointed by ghoti in the comments it is not a good idea to rely on globbing. When you make use of globbing the filenames with hypens can have undefined behaviour & filenames with semi-colons can cause security vulnerabilities. Apart for security vulnerabilities associated with globbing there are some common pitfalls associated with it as well. Please take a look at the answer provided by ghoti which highlights some of risks involved with your current operations.
You could use
findas well (This will recursive set for all the files):find ~/Documents/ -type f -exec chmod 755 {} \;For setting permissions only for the files under
~/Documents/you can make use of-maxdepthoption as such:find ~/Documents/ -maxdepth 1 -type f -exec chmod 755 {} \;Hope this helps!