{"id":5541,"date":"2024-10-12T17:16:20","date_gmt":"2024-10-12T11:46:20","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=5541"},"modified":"2025-03-10T12:00:29","modified_gmt":"2025-03-10T06:30:29","slug":"change-permissions-with-chmod-in-linux-recursively","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/change-permissions-with-chmod-in-linux-recursively\/","title":{"rendered":"Recursively Change Permissions with CHMOD in Linux"},"content":{"rendered":"\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/10\/Change-Linux-Permissions-Recursively-Cover-Picture-scaled.jpeg\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"579\" data-id=\"5547\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/10\/Change-Linux-Permissions-Recursively-Cover-Picture-scaled.jpeg\" alt=\"With chmod change permissions for all files and directories in Linux recursive\" class=\"wp-image-5547\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Change-Linux-Permissions-Recursively-Cover-Picture-scaled.jpeg 2560w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Change-Linux-Permissions-Recursively-Cover-Picture-300x68.jpeg 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Change-Linux-Permissions-Recursively-Cover-Picture-1024x232.jpeg 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Change-Linux-Permissions-Recursively-Cover-Picture-768x174.jpeg 768w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Change-Linux-Permissions-Recursively-Cover-Picture-1536x348.jpeg 1536w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Change-Linux-Permissions-Recursively-Cover-Picture-2048x463.jpeg 2048w\" sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p>In Linux controlling file permissions is essential for maintaining security and appropriate access control. Distinct permissions for files and directories inside a folder are frequently necessary. Sometimes you need the same permission to set for all files\/directories inside a folder. Manually doing this file-by-file or all things will be a daunting task.<br>This tutorial focuses on how to change permissions for <strong>all files and directories in a folder<\/strong>, covering both <a href=\"https:\/\/linux.die.net\/man\/1\/chmod\" target=\"_blank\" rel=\"noopener\">chmod<\/a> recursive changes and how to apply different permissions to specific types of files or directories.<\/p>\n\n\n\n<p><\/p>\n\n\n\t\t\t\t<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-4083d87f      \"\n\t\t\t\t\tdata-scroll= \"1\"\n\t\t\t\t\tdata-offset= \"30\"\n\t\t\t\t\tstyle=\"\"\n\t\t\t\t>\n\t\t\t\t<div class=\"uagb-toc__wrap\">\n\t\t\t\t\t\t<div class=\"uagb-toc__title\">\n\t\t\t\t\t\t\tTable Of Contents\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"uagb-toc__list-wrap \">\n\t\t\t\t\t\t<ol class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#changing-permissions-for-all-files-and-directories-throughout-the-folder\" class=\"uagb-toc-link__trigger\">Changing Permissions for All Files and Directories throughout the Folder<\/a><li class=\"uagb-toc__list\"><a href=\"#setting-different-permissions-for-files-and-directories\" class=\"uagb-toc-link__trigger\">Setting Different Permissions for Files and Directories<\/a><li class=\"uagb-toc__list\"><a href=\"#changing-permissions-for-specific-file-types\" class=\"uagb-toc-link__trigger\">Changing Permissions for Specific File Types<\/a><li class=\"uagb-toc__list\"><a href=\"#securing-code-files-eg-php-with-more-restrictive-permissions\" class=\"uagb-toc-link__trigger\">Securing Code Files (e.g., PHP) with More Restrictive Permissions<\/a><li class=\"uagb-toc__list\"><a href=\"#setting-default-permissions-for-newly-created-filesdirectories\" class=\"uagb-toc-link__trigger\">Setting Default Permissions for Newly Created Files\/Directories<\/a><li class=\"uagb-toc__list\"><a href=\"#fine-tuning-permissions\" class=\"uagb-toc-link__trigger\">Fine-Tuning Permissions<\/a><li class=\"uagb-toc__list\"><a href=\"#changing-ownership-recursively-using-chown\" class=\"uagb-toc-link__trigger\">Changing Ownership Recursively Using chown<\/a><\/ol>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"changing-permissions-for-all-files-and-directories-throughout-the-folder\">Changing Permissions for All Files and Directories throughout the Folder<\/h4>\n\n\n<p>You can change permissions to all files and directories within a folder using the chmod command with the option -R which stands for <a href=\"https:\/\/www.vocabulary.com\/dictionary\/recursive\" target=\"_blank\" rel=\"noopener\">recursive<\/a>. You would do this with the -R option. That means it will recursively set permissions for everything in that folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod -R 755 \/your\/folder<\/code><\/pre>\n\n\n\n<p>You can adjust the permission value as needed by replacing 755 with your desired permission level. Make sure to change&nbsp;<code>\/your\/folder<\/code>&nbsp;to the path of the folder you want to apply the permissions to.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-veeble-hosting wp-block-embed-veeble-hosting\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.veeble.org\/kb\/what-do-chmod-755-644-and-others-mean\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"setting-different-permissions-for-files-and-directories\">Setting Different Permissions for Files and Directories<\/h4>\n\n\n<p>The <a href=\"https:\/\/www.veeble.org\/kb\/how-to-find-files-containing-specific-text-in-linux\/#:~:text=How%20to%20Find%20Files%20Containing%20Specific%20Text%20with%20Find%20Command%3F\" target=\"_blank\" rel=\"noopener\">find command<\/a> in Linux can be used to apply different permissions for files and directories. This could be helpful in case you want to avoid execute permission from being set on all the files that don&#8217;t need it, such as images, scripts, or text files.<\/p>\n\n\n<h6 class=\"wp-block-heading\" id=\"change-permissions-for-all-directories-to-755\">Change Permissions for All Directories to <code>755<\/code>:<\/h6>\n\n\n<pre class=\"wp-block-preformatted\">find \/your\/folder -type d -exec chmod 755 {} \\;<\/pre>\n\n\n\n<p>This command will look for all the <strong>directories<\/strong> inside \/your\/folder and change its permission to 755, giving full permission to the owner while giving the capability of reading and executing the contents of the directory to others. (Here <em>&#8220;-type d&#8221;<\/em> in the command stands for type directory.)<\/p>\n\n\n<h6 class=\"wp-block-heading\" id=\"change-permissions-for-all-files-to-644\">Change Permissions for All Files to <code>644<\/code>:<\/h6>\n\n\n<pre class=\"wp-block-preformatted\">find \/your\/folder -type f -exec chmod 644 {} \\;<\/pre>\n\n\n\n<p>The permission-changing command is only applicable to files, as indicated by the <code>-type f<\/code> option in the find command, which stands for &#8220;type file.&#8221;<\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"changing-permissions-for-specific-file-types\">Changing Permissions for Specific File Types<\/h4>\n\n\n<p>For some kinds of files (images, CSS, JavaScript, HTML) execution permissions are not needed. Well, you can change permissions for those kinds of files quite easily using chmod:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">chmod 644 \/your\/folder\/img\/* \/your\/folder\/js\/* \/your\/folder\/html\/*<\/pre>\n\n\n\n<p>By this command, permissions for files inside img, js, and html directories are set to 644, so everyone can read the files, while only the owner can write.<\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"securing-code-files-eg-php-with-more-restrictive-permissions\">Securing Code Files (e.g., PHP) with More Restrictive Permissions<\/h4>\n\n\n<p>If you have files that contain sensitive codes, such as PHP scripts, you probably want to lockdown those files so that only the owner can access them. Use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">chmod 600 \/your\/folder\/php\/*<\/pre>\n\n\n\n<p>This command sets the permissions to <code>600<\/code>, and by doing this only the owner can read and write the file, while others cannot access it at all.<\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"setting-default-permissions-for-newly-created-filesdirectories\">Setting Default Permissions for Newly Created Files\/Directories<\/h4>\n\n\n<p>You will want to change the<a href=\"https:\/\/man7.org\/linux\/man-pages\/man2\/umask.2.html\" target=\"_blank\" rel=\"noopener\"> umask<\/a> value if you want newly created files and directories to have certain permissions. umask defines the default permissions given for newly created files and directories.<\/p>\n\n\n<h6 class=\"wp-block-heading\" id=\"set-umask-in-a-php-script\">Set umask in a PHP Script:<\/h6>\n\n\n<p>If you create files programmatically using PHP, you can set the <code>umask<\/code> in your code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php<br>    umask(0022); \/\/ Set default permissions<br>    \/\/ Your code here<br>?&gt;<\/pre>\n\n\n<h6 class=\"wp-block-heading\" id=\"set-umask-in-the-shell\">Set umask in the Shell:<\/h6>\n\n\n<p>To set the <code>umask<\/code> for your shell session, add the following command to your <code>~\/.bashrc<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">umask 022<\/pre>\n\n\n\n<p>This ensures that new files in your shell have the correct default permissions.<\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"finetuning-permissions\">Fine-Tuning Permissions<\/h4>\n\n\n<p>Sometimes you might not want to have a universal permissions change on all your files and directories. How you can refine this using find:<\/p>\n\n\n<h6 class=\"wp-block-heading\" id=\"apply-permissions-to-directories-only\">Apply Permissions to Directories Only:<\/h6>\n\n\n<pre class=\"wp-block-preformatted\">chmod 755 $(find \/your\/folder -type d)<\/pre>\n\n\n<h6 class=\"wp-block-heading\" id=\"apply-permissions-to-files-only\">Apply Permissions to Files Only:<\/h6>\n\n\n<pre class=\"wp-block-preformatted\">chmod 644 $(find \/your\/folder -type f)<\/pre>\n\n\n\n<p>These commands allow you to apply permissions more selectively, ensuring that you only modify directories or files as required.<\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"changing-ownership-recursively-using-chown\">Changing Ownership Recursively Using chown<\/h4>\n\n\n<p>In addition to changing permissions, you can adjust the ownership of files and directories. To do this recursively (throughout the folder), use the <code>chown<\/code> command as in the following format:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo chown -R username:group \/your\/folder<\/pre>\n\n\n\n<p>This command ensures that the specified user and group own all the files and directories within the folder.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-uagb-separator uagb-block-3e253578\"><div class=\"wp-block-uagb-separator__inner\" style=\"--my-background-image:\"><\/div><\/div>\n\n\n\n<p>Changing permissions for all files and directories in a folder is a common task with chmod command in Linux system administration. By using these Linux commands chmod, chown, find, you can maintain proper permissions on all files and directories in a folder, improving both security and functionality.<\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-5f6bbc58 wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Start Your Hosting Business Today!<\/h3><p class=\"uagb-cta__desc\">Launch your reseller hosting venture effortlessly with white-label solutions, 24\/7 expert support, and high-performance servers. Earn more with competitive pricing and scalable packages.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/reseller-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Choose Your Plan<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Linux controlling file permissions is essential for maintaining security and appropriate access control. Distinct permissions for files and directories inside a folder are [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":7203,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[],"class_list":["post-5541","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Recursively-Change-Permissions-with-CHMOD-in-Linux.jpg",1366,768,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Recursively-Change-Permissions-with-CHMOD-in-Linux-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Recursively-Change-Permissions-with-CHMOD-in-Linux-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Recursively-Change-Permissions-with-CHMOD-in-Linux-768x432.jpg",768,432,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Recursively-Change-Permissions-with-CHMOD-in-Linux-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Recursively-Change-Permissions-with-CHMOD-in-Linux.jpg",1366,768,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/10\/Recursively-Change-Permissions-with-CHMOD-in-Linux.jpg",1366,768,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"In Linux controlling file permissions is essential for maintaining security and appropriate access control. Distinct permissions for files and directories inside a folder are [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/5541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=5541"}],"version-history":[{"count":10,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/5541\/revisions"}],"predecessor-version":[{"id":7972,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/5541\/revisions\/7972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/7203"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=5541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=5541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=5541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}