Bash Script: recursively change file permissions

I need a Bash script that changes file permissions for all files in a directory and in all subdirectories. It should behave as follows:

for each file in directory (and subdirectories)
   if i am the owner of the file
      if it is a directory
         chmod 770 file
      else
         chmod 660 file

I think this is not a difficult task, but I am not very versed in Bash scripts. Your help is appreciated !: D

+4
source share
3 answers

You can do this with two command calls find, with a parameter -userfor filtering by user and an option -typefor filtering by file type:

find . -user "$USER" -type d -exec echo chmod 770 {} +
find . -user "$USER" -not -type d -exec echo chmod 660 {} +

Delete echoafter testing to really change permissions.

+2
source

find : / , ( ). X ( X) chmod, , . xargs:

find . -user $(whoami) | xargs chmod ug=Xo=

, , . :)

+1

Use find:

find topdirectory -user "$USER" \( -type f -exec chmod 660 {} + \) -o \( -type f -exec chmod 770 {} + \)
+1
source

Source: https://habr.com/ru/post/1677740/


All Articles