Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash loop to change permissions

Tags:

bash

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

like image 512
Jeff Avatar asked Jul 05 '26 00:07

Jeff


1 Answers

There are risks in making your documents executable. I recommend against it unless you know exactly what you're doing.

I'm going to take a wild guess that you don't really want to make all your Documents folder executable, and what you're really trying to do is standardize on user-writable everyone-else-readable permission for everything in that folder.

Note that (as someone mentioned in another answer) the chmod command has its own "recursive" option, -R. Note also that you can use "symbolic" permissions with chmod, you're not stuck with octal-only. So:

chmod -R go+r-wx ~/Documents/

will add Read and remove Write and eXecute functions from everything in and under ~/Documents/.

Note that this will make subdirectories readable, but will not provide access to the files within them (because that's what the executable bit on a directory does). So you may want to use TWO commands:

find ~/Documents/ -type d -exec chmod 755 {} \;
find ~/Documents/ -type f -exec chmod 644 {} \;

The first line only runs chmod on directories, making them both readable and accessible. The second line affects only your files, making them readable by the world.

like image 164
ghoti Avatar answered Jul 06 '26 12:07

ghoti