• Home
  • SEM
    • PPC
    • SEO
    • How To
  • Bangladesh
  • School
    • HTTP
    • REP
    • Bots n’ htaccess
    • Source Code
    • Excel 2010
    • HTML XHTML Entities
    • Gmail Operators
    • HTML & ASCII
  • About
    • Privy
    • About This Blog
  • Contact
  • বাংলা

Saidul Hassan

Digital Marketing Evangelist

Home / Linux Mint / How to add read & write permissions for the owner and group on folder & file name recursively?

How to add read & write permissions for the owner and group on folder & file name recursively?

29 Nov, 2015 By Saidul Hassan · Filed Under: Linux Mint

To add read and write permissions for the owner and group on folder, open terminal and type:

chmod ug+rw "folder name" 

Add the -R option to recursively change the permissions of the folder and all files and sub-folders:

chmod -R ug+rw "folder name"

Example:

sudo find "folder name" -exec chmod a+rwx {} ";"
sudo find "folder name" -type d -exec chmod 755 {} ";"
sudo find "folder name" -type f -exec chmod 644 {} ";"

When you’re inside the folder, remove "folder name" and replace it with .

If you want to have all of your files be readable to the world (ie. it’s just a static set of HTML files/images), then use this command:

chmod -R a+r "folder name"

That will recursively go through all of the files & subdirectories and add read permission to them.
WARNING: Don’t do this for code that executes! Only files that should be visible by everybody.
Also, if you try to use

chmod -r some-options

(small -r instead of -R) you might end up removing read permissions for everyone.

File permission errors in Apache, editors etc. where only 777 works but not 775 for folders and 644 for files.

Errors occur in either Apache or your editor regarding permissions because Apache runs under a different user (www-data) than you.
If you want to write to /var/www, add yourself to the www-data group and set umask+permissions accordingly.

  • Add yourself to the www-data group: sudo adduser your-username www-data
  • Change the ownership of the files in /var/www: sudo chown -R www-data:www-data /var/www
  • Change the umask, so newly created files by Apache grants write permissions to the group too. Add umask 007 to /etc/apache2/envvars.
  • Grant yourself (technically, the group www-data) write permissions: sudo chmod -R g+w /var/www.

To set the directories to 755 and either leave the files alone or set them to 644. you can use the find command. For example:
To change all the directories to 755 (-rwxr-xr-x):

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

To change all the files to 644 (-rw-r--r--):

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

chmod 644 {} \; specifies the command that will be executed by find for each file. {} is replaced by the file path, and the semicolon denotes the end of the command (escaped, otherwise it would be interpreted by the shell instead of find).
Notice the execute bit exclusively on the directories returned by find. The execute permission on a directory allows a user class to list the contents of that directory and to cd into it. Generally speaking you want both r and x on a directory for it to be accessible to you, even though there might be strange edge cases where you’d set only one or the other.

If you want to set permissions on all files to a+r, and all directories to a+x, and do that recursively through the complete subdirectory tree, use:

chmod -R a+rX *

The X (that is capital X, not small x!) is ignored for files (unless they are executable for someone already) but is used for directories. Uppercase X means “make all directories executable” (but not files). Just note that * will not match hidden files (names beginning with a dot). It may make more sense, then, to use . (for the current directory).
For example, if you want to set all directories to 775 and all files to 664, you could use

chmod -R g+wX .
The following two tabs change content below.
  • Bio
  • Latest Posts
My Twitter profileMy Facebook profileMy LinkedIn profileMy Instagram profileMy Pinterest profile

Saidul Hassan

Managing Partner at Up Arrow Consulting
COO at Up Arrow Consulting, MCC manager, & Technical SEO consultant. Certified Google Partners and Microsoft Bing Ads Accredited Professional, Python enthusiast, wannabe SysAdmin. Graduated from School of Management & Business Administration (SOMBA), Khulna University.
My Twitter profileMy Facebook profileMy LinkedIn profileMy Instagram profileMy Pinterest profile

Latest posts by Saidul Hassan (see all)

  • Batch script to create directories & moving fixed number of files to those directories - 30 Apr, 2021
  • How to use PrismJS syntax highlighter on WordPress without plugin - 30 Mar, 2020
  • Download an Entire Website for Offline Viewing - 26 Nov, 2019

Share:

  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Pocket
  • Email
  • Print

First published on 29 Nov, 2015 · Last updated 29 Nov, 2015 · Tagged With: File Permission, How To, Terminal

Batch script to create directories & moving fixed number of files to those directories

30 Apr, 2021 By Saidul Hassan

How to use PrismJS syntax highlighter on WordPress without plugin

30 Mar, 2020 By Saidul Hassan

Download an Entire Website for Offline Viewing

26 Nov, 2019 By Saidul Hassan

How to color highlight .htaccess files in Xed/Gedit

20 Aug, 2018 By Saidul Hassan

HMA Pro VPN Setup for Multiple Locations without User/Password Every time in Linux CLI

14 May, 2018 By Saidul Hassan

  • ♥ Bangladesh ♥
    Log in · Privacy Policy · Contact
    Copyright © 2011 Saidul Hassan

  • DMCA