Linux change permission recursive

How can I recursively change the permissions of files and directories?

I have ubuntu installed on my local computer with apache / php / mysql. I now have a directory at /var/www — inside which I have several of my ongoing projects. I also work with opensource ( drupal, magento, sugarcrm ). The problem I am facing is changing file permission with terminal. Sometime I need to change the permission of entire folder and its subsequent sub-folders and files. I have to individually change using

sudo chmod 777 foldername 

How can I do this recursively. Also why do I have to always do it 777, I tried 755 for folders and 644 for files, but that won’t work.

5 Answers 5

Just add the -R option to recursively change the permissions of files. An example, recursively add read and write permissions for the owner and group on foldername :

Permissions will be like 664 or 775.

Setting the permissions to 777 is highly discouraged. You get errors 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 $USER 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 .

Источник

How to Change File Permissions Recursively with chmod in Linux

Multi-user systems, such as Linux, require setting up and managing file permissions that ensure only authorized users have access to files they are supposed to.

If you need to change a file permission, use the chmod command. It also allows to change the file permission recursively to configure multiple files and sub-directories using a single command.

In this tutorial, you will learn how to use chmod recursively and change file permission on Linux.

How to recursively change file permissions in Linux.

Note: The user who creates a file (or directory) has ownership of it. The file-owner has read, write, and execute privileges. Other users only have as much access as given to them when configuring permissions, while the root user has all privileges for all files.

Check File Permission

If you need to check the file permissions in the working directory, use the command:

The output lists the permissions of all the files in the directory.

For instance, the Example directory contains three files (test1.txt, test2.txt, and test3.txt) with the same permissions (-rw-rw-r–).

Check file permission of all files in directory.

The file permissions listed above tells us the following:

  • The owner has read and write privileges
  • The owner’s group has read and write privileges
  • Other users have read privileges

Note: Do you want to learn more about file permissions and how they are defined? Refer to the Linux File Permission Tutorial.

Читайте также:  Файл настройки apache linux

Change Permission Recursively

It is common to use the basic chmod command to change the permission of a single file. However, you may need to modify the permission recursively for all files within a directory.

In such cases, the chmod recursive option ( -R or —recursive ) sets the permission for a directory (and the files it contains).

The syntax for changing the file permission recursively is:

chmod -R [permission] [directory]

Therefore, to set the 755 permission for all files in the Example directory, you would type:

The command gives read, write, and execute privileges to the owner (7) and read and execute access to everyone else (55).

Change file permission recursively on Linux.

Note: In the example above, the permission is defined using the octal/numerical mode ( 755 ). Alternatively, you can utilize the symbolic mode (using alphanumerical characters) and use the command: chmod -R u=rwx,go=rx Example .

Change Permission With the find Command

To assign separate permissions to directories and files, you can use the find command.

The basic syntax includes using the find command to locate files/directories and then passing it on to chmod to set the permission:

sudo find [directory] -type [d/f] -exec chmod [privilege] <> \;
  • Replace [directory] with the directory path that holds the files and subdirectories you want to configure.
  • Specify whether it is searching for a directory -type d or a file -type f .
  • Set the file [privilege] with the chmod command using the numerical or symbolic mode.

Avoid assigning execute privileges to files. A common setup would include running the following commands:

sudo find Example -type d -exec chmod 755 <> \;
sudo find Example -type f -exec chmod 644 <> \;

In this example, the directories have 755 (u=rwx,go=rx) privileges, while the files have 644 (u=rw,go=r) privileges.

Change file permission recursively using the find command.

You can check to verify directories and files have different permission settings by moving into the Example directory ( cd Example ) and listing the content ( ls -l ). The output should be similar to the one below:

Setting different permissions for files and directories.

Note: Learn more about the Linux find command.

Change Permission of Specific Files Recursively

Combining the find command with chmod can also be used for changing the permission of files that are a specific type.

The command syntax for changing the permission of a specific file type in a directory is:

find [directory] -name "*.[filename_extension]" -exec chmod [privilege] <> \;

For example, to make all .sh files in the current directory executable, you would use:

find . -name "*.sh" -exec chmod +x <> \;

You should now know how to recursively change the file permission on your Linux system with chmod -R or the find command.

Alternatively. you can also use the umask command which lets you change the default permissions.

Источник

Chmod Recursive Guide: How to Change Permissions in Linux

chmod recursive guide text

ServerMania offers a variety of Hybrid, Cloud, and Dedicated Linux servers which all make use of the chmod command. It’s a frequently used command, so it’s important that any system admin knows how to use it.

In this article, we’re going to cover; what is chmod, how is it used, and what things to avoid.

What is chmod? Chmod, also known as ‘change mode’, changes the file mode bits of a certain file. You can change it with an actual number representation or just symbolically.

Except for changing permissions for files and directories, you are also able to modify special mode flags, like SGID, SUID, and sticky bit mode. It is present in Unix-like and Linux operating systems since the 70s.

Читайте также:  Install new font linux

One of the most essential parts of any Linux system administrator is setting up correct permissions. Setting up wrong permissions can allow hackers to execute malicious code to your server and harm it. Unless needed, it is highly advised that other users are restricted to read and execute files on your system.

If you’re looking for the best Linux distributions or Linux dedicated servers, make sure you visit our website and more about it!

In this article, we will cover the meaning of chmod, how it is used, what are the things you should avoid doing, and some general tips when it comes to chmod.

File Permissions

If you’re managing Linux servers or you’re using Linux as your main operating system, you will most likely come across the “Permission deny” error on your screen when you try to create or edit a file. These errors that occur due to insufficient permission can be handled by setting the right file permissions. Linux is controlled through ownership, attributes, and file permissions. These file permissions ensure that only authorized users can access files and directories.

ServerMania offers a variety of Hybrid, Cloud, and Dedicated Linux servers which all make use of the chmod command. It’s a frequently used command, so it’s important that any system admin knows how to use it.

Files and Directories

You can change all of the files and directories with the Linux chmod command. Sudo is the preferred way of editing system files. This is because sudo allows you to keep the system context.

To change directory permissions in Linux, use the following commands: chmod +rwx filename to add permissions; chmod -rwx directoryname to remove permissions; chmod +x filename to allow executable permissions; and chmod -wx filename to take out write and executable permissions.

Make sure you note that, “x” is for execute, “r” is for read, and “w” is for write.

Chmod R

When you’re working on a folder directory, you probably don’t need to change the permission of one single file. Often, people need to change the permission on that file, all of its subdirectories, and all the files in the current folder.

You can change permissions of files using numeric or symbolic mode with the chmod command. Use the chmod command with the R (recursive) option to work on all directories and files under a given directory.

To change the file permissions, the general syntax is:

For instance, you can the symbolic method by typing:

chmod -R u=rwx,go=rx /var/www/html

Or, if you want to change the permissions of all files and subdirectories under the /var/www/html directory to 755 you would use:

The permissions of a file can be changed only with the user with sudo priviledges, or the file owner. When you chmod recursively change the files’ permissions, you need to be really careful.

Type F Exec Chmod

Directories and files shouldn’t have the same permissions. In order to change into directories, you must set execute permissions. Recursively changing the website file’s permissions to 644 and the directory’s permissions to 755 is the most common scenario.

Using Different Types of Methods

This can be done by using the numeric method, with f exec chmod 644 or exec chmod 755, more precisely:

find /var/www/html -type d -exec chmod 755 <> ; find /var/www/html -type f -exec chmod 644 <> ;

It can also be done by using the symbolic method:

find /var/www/html -type d -exec chmod u=rwx,go=rx <> ; find /var/www/html -type f -exec chmod u=rw,go=r <> ;

Then, files and directories are searched by the file command under /var/www/html, and found files and directories are passed to the chmod command to set the permissions.

Читайте также:  Can create folder in linux

The chmod command is run for each found entry when you use find with -exec. This operation can be sped up by using the xargs command, which passes multiple entries at the same time:

find /var/www/html -type d -print0 | xargs -0 chmod 755 find /var/www/html -type f -print0 | xargs -0 chmod 644

We want to offer you some extra advice and tips on chmod. Firstly, in order to list directories and subdirectories, make sure that you remember that you need read permissions. Secondly, all files in a directory or folder can be set to writeable with chmod -R 775 [directory]. And lastly, with the find command, you can apply permissions independently to files and directories.

One chmod operation that is advised to be generally avoided since it can cause some pretty big security issues on your server is the chmod 777 operation.

This operation gives full permission to anyone who accesses your server, and it is the reason why this operation should be avoided.

It is better to just use the chmod 775 command since it does not have a significant security risk.

When it comes to your permission, you should also avoid using 664. In order to access it, you will need to execute permissions on a directory, and removing that permission will break whatever you apply it to.

Conclusion

Use the chmod command in combination with the find command in order to recursively set permissions on directories. Chmod recursive lets you recursively change the permission of a certain file.

As you can see from the article, there are several options you can choose from when it comes to permissions. The usability among users is dictated by you. Even though giving permission to everyone sounds like an easier choice, it isn’t the wisest one. So make sure you choose wisely when it comes to permissions.

Chmod helps out a lot with the sticky situations that file permissions can sometimes cause. You can truly take ownership of your files and directories once you know the procedure for changing permissions.

About ServerMania

ServerMania has over a decade of experience building high-performance infrastructure hosting platforms for businesses globally. We offer a wide range of fully customizable dedicated, hybrid, cloud, colocation and IP Transit services. Our mission is to empower clients by equipping them with fast, reliable, innovative infrastructure hosting while upholding a 100% network uptime SLA. This is assisted by a 24x7x365 rapid response team — one with some of the best response times in the industry.

COMPLETE DIGITAL SERVER SOLUTIONS FOR ALL

Bare Metal Dedicated Servers

A single tenant, physical server allowing you full access to its resources

Professional Hybrid Servers

Virtualized server platform hosted on enterprise-grade physical servers

Scalable Cloud Servers

High-performance and highly-available infrastructure

Managed Colocation

Our next-generation data center facilities

Источник

Оцените статью
Adblock
detector