With the Linux chmod command, we can recursively change file permissions on all files and directories. This guide explains how.
It’s likely you’ve run into the following errors before:
111 [Permission Denied] "Linux-Screw" [Permission Denied] "Linux-Screw" [readonly]
For any system files, using sudo is the preferred way of editing a file. This allows you to keep all the system context. For everyday use with user files, it’s best to change permissions. chmod can do that for us. It keeps us from needing to escalate permission and gives us access when we need it.
What is chmod? chmod, or change mode, changes the file mode bits of a given file. You can change it either symbolically or with an octal number representation.
chmod Syntax
chmod [OPTION] MODE FILE Or chmod [OPTION] OCTAL-MODE FILE Options: [-c], --changes Verbose-mode, but only for changes [-f], --silent, --quiet Suppress most error messages [-v], --verbose Output a diagnostic for every file processed [-R], --recursive Change files and directories recursively
Those are the available options. Mode refers to symbolic referencing. If I want to add executable permission to a file, I enter in:
chmod +x [FILE]
And that will add executable permissions. We’ve gone over the basics, we’ll fix a folder structure and make it available to us.
chmod Recursion
Often when you’re working in a folder directory, you don’t need to change the permission of a single file. You’ll need to change the permission of that file, all files in the current folder, and all subdirectories.
Luckily, chmod -R allows us to recursively change all files.
chmod -R 755 * ls -altrR
Shows us the following after we changed permissions.
If you want to know why -altrh is my default and preferred set of options, see my recent article on ls.
Okay, there we go. Above we see our directories and files have changed to match the 755, or rwx r-x r-x. That directory is now available to us since we belong to that group (a user group). It’s also available to us since we’re the user. If you’re cleaning up folders from a previous user you can change ownership of the files with chown.
This makes it available to you as a user. However, there is an issue with the above chmod. When you set your umask to something like 0022, the default perm, directories and files are different. Directories are created at 755. Allowing the cd, change directory, command to function properly. Files are created as 644, one less than the directory. This is because normal files do not need execution permissions. Directories do, for cd. So let’s fix it with find and -exec.
chmod Recursively with Find
find <dir> -type d -exec chmod 755{} \; find <dir> -type f -exec chmod 644 {} \;
And there we go, everything saved and kept in line with the default umask. No executable files, and executable directories.
Conclusion
File permissions can be sticky, but chmod helps us fix that. Now that you know how to change permissions, you can truly take ownership of your directories and files. Need to learn more about navigating the Linux infrastructure with shell commands? Read more tutorials here.