Linux or UNIX change file permissions recursively ( conditional )

Q. How do I recursively change files with 777 permissions to 755 in /home/user/demo directory? I have a number of files in this directory and I need to change from 777 to only if that file has 777 permissions.

Is there an easy way out to achieve this?

A. To change file access permissions you use chmod command. It has -R or -recursive option that change files and directories recursively. For example
$ chmod -R 0755 directory

However, if you need to apply conditional file permissions recursively use combination of find and chmod command.

Find all files in /home/user/demo directory
$ find /home/user/demo -print

Now find all files in /home/user/demo directory with permission 777
$ find /home/user/demo -perm 777 -print

Next you need to apply chmod on all these files using -exec option:
$ find /home/user/demo -perm 777 -print -exec chmod 755 {} \;

Read man page of find for more information.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 3 comments… read them below or add one }

1 Thankful guy 09.17.08 at 7:21 am

Thanks a lot for the tutorial. Now I can change the permission of a folder and its subfolders without affecting files inside. i.e.:

$ find folder_name -type d -exec chmod 775 ‘{}’ \;

2 Info Gaptek 05.26.09 at 4:07 am

I found this page by googling.
Thanks for your help!

3 jay 06.21.09 at 11:31 am

Thanks for this amazing commands…!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Mysql remove duplicate data or rows with DISTINCT

Next post: Linux Start Restart and Stop The Cron or Crond Service