Linux find all directories chmod

How do I change permissions for a folder and its subfolders/files? [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.

How do I change the permissions of a folder and all its subfolders and files? This only applies to the /opt/lampp/htdocs folder, not its contents:

How do I set chmod 755 for all of the /opt/lampp/htdocs folder’s current contents, as well as automatically in the future for new folders/files created under it?

Did you intend to write chmod 75 /opt/lampp/htdocs or should that really be chmod 755 /opt/lampp/htdocs ?

@ArthurDent, because this question (while perfectly valid and helpful) is more suited to a system-focused SE site, such as SuperUser or ServerFault. It’s not expressly related to programming.

@timelmer Sure, but I ask again, why are «permissions», «folder», and «cmod» tags? When would cmod ever be used ‘expressly’ for programming?

If you want to get really picky, Bash is actually a scripting language, but then so is php, which SO doesn’t seem to mind.

19 Answers 19

The other answers are correct, in that chmod -R 755 will set these permissions to all files and subfolders in the tree. But why on earth would you want to? It might make sense for the directories, but why set the execute bit on all the files?

I suspect what you really want to do is set the directories to 755 and either leave the files alone or set them to 644. For this, you can use the find command. For example:

To change all the directories to 755 ( drwxr-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 755 <> specifies the command that will be executed by find for each directory
  • chmod 644 <> specifies the command that will be executed by find for each file
  • <> is replaced by the path
  • ; the semicolon tells find that this is the end of the command it’s supposed to execute
  • \; the semicolon is escaped, otherwise it would be interpreted by the shell instead of find

@FlavorScape Womble is setting 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. See this guide for more info: nixsrv.com/llthw/ex23

With the exception of sleepynate, every response neglected to address the aspect of his question regarding the setting of permissions to files/folders created in the future.

Читайте также:  Remove any file in linux

You should wrap at least the placeholder into single quotes, so that potentially existing spaces in filenames do not result in multiple arguments; i.e.: -exec chmod 755 ‘<>‘ \; — …and when using single quotes for the placeholder, it is easier to remember the possibility of escaping the semicolon with single quotes as well; i.e.: -exec chmod 755 ‘<>‘ ‘;’

In the future, you can save a lot of time by checking the man page first:

it stands for manual page and is a linux command that shows the man page for a command (most linux commands have a man page). try man ls or man man.

This did not work for me in the Terminal in Mac OS X. There I did «chmod -R *» and it worked.

@SteveRobillard One concern with the solution though, when it comes to x / execute permission, when you are doing -x ; you don’t want to accidentally make a directly not cd able.

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:

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.

Great answer. 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).

In case it’s not entirely clear, uppercase X means «make all directories executable» (but not files).

@BenoitDuffez if your goal is to also remove the executable bit from files, you can combine directives, like «a-x+rX» to remove «x» from everything and then set «r» to everything and «x» to directories only.

In chmod -R a+rX and chmod -R g+wX , a is all and g is group. There is also u user/owner and o other. In those commands, you’re adding a read/write bit and an execute bit (capital X denotes «only if the file is a directory»)

You can use -R with chmod for recursive traversal of all files and subfolders.

You might need sudo as it depends on LAMP being installed by the current user or another one:

sudo chmod -R 755 /opt/lampp/htdocs 

Somehow, sudo chmod 755 -R /directory didn’t work but sudo chmod -R 755 /directory did. Weird. I’m using Mac os x by the way.

This is correct in my experience. The flag must appear directly after chmod, not anywhere in the string.

Above written command is wrong. The correct recursive command is: sudo chmod -R 755 /opt/lampp/htdocs Note: -R should be before 755

OSX USSING: sudo chmod 777 logs/ -R GETTING: chmod: -R: No such file or directory SHOULD BE: sudo chmod -R 777 logs/

The correct recursive command is:

sudo chmod -R 755 /opt/lampp/htdocs 

-R : change every sub folder including the current folder

To set to all subfolders (recursively) use -R

chmod 755 -R /opt/lampp/htdocs will recursively set the permissions. There’s no way to set the permissions for files automatically in only this directory that are created after you set the permissions, but you could change your system-wide default file permissions with by setting umask 022 .

sleepynate’s answer is the ONLY correct one. The poster asked two questions: 1) How do I fix permissions on files and folders, and 2) how do I change the defaults (meaning «future» files). As nate said, the first is with «chmod», and the second is with «umask». I’d also add that there is NO WAY to change umask on just 1 directory. umask is «all or nothing» per user. However nothing stops you from creating a new user, who shares groups with you, so that you can just ‘sudo -u someuser [create file]». That’s thinking outside the box a little, but it’s a fine workaround.

Читайте также:  Pdf viewer linux arch

I think you are right, but can’t inherit be a solution to many cases or will it work with NTFS-3G exclusively?

without give permission to folders may be created in the future, one may not be able to copy a folder,say myforlder from other places(like a PC) to this target directory(on a server) cause it will try to create a new folder in the target directory with name myfolder which you do not have permission to do so. As I have root , I just use chown -R username:usergroup /directory .

You might want to consider this answer given by nik on Super User and use «one chmod» for all files/folders like this:

chmod 755 $(find /path/to/base/dir -type d) chmod 644 $(find /path/to/base/dir -type f) 

It would be better to use the -exec option of find in order to overcome the Argument list too long error.

Here’s another way to set directories to 775 and files to 664.

find /opt/lampp/htdocs \ \( -type f -exec chmod ug+rw,o+r <> \; \) , \ \( -type d -exec chmod ug+rwxs,o+rx <> \; \) 

It may look long, but it’s pretty cool for three reasons:

  1. Scans through the file system only once rather than twice.
  2. Provides better control over how files are handled vs. how directories are handled. This is useful when working with special modes such as the sticky bit, which you probably want to apply to directories but not files.
  3. Uses a technique straight out of the man pages (see below).

Note that I have not confirmed the performance difference (if any) between this solution and that of simply using two find commands (as in Peter Mortensen’s solution). However, seeing a similar example in the manual is encouraging.

Example from man find page:

find / \ \( -perm -4000 -fprintf /root/suid.txt %#m %u %p\n \) , \ \( -size +100M -fprintf /root/big.txt %-10s %p\n \) Traverse the filesystem just once, listing setuid files and direct‐ tories into /root/suid.txt and large files into /root/big.txt. 
sudo chmod 755 -R /whatever/your/directory/is 

However, be careful with that. It can really hurt you if you change the permissions of the wrong files/folders.

chmod -R 755 directory_name works, but how would you keep new files to 755 also? The file’s permissions becomes the default permission.

For Mac OS X 10.7 (Lion), it is:

And yes, as all other say, be careful when doing this.

For anyone still struggling with permission issues, navigate up one directory level cd .. from the root directory of your project, add yourself (user) to the directory and give permission to edit everything inside (tested on macOS).

To do that you would run this command (preferred):

sudo chown -R username: foldername .* 

Note: for currently unsaved changes, one might need to restart the code editor first to be able to save without being asked for a password.

Читайте также:  Installing python package on linux

Also, please remember you can press Tab to see the options while typing the username and folder to make it easier for yourself.

sudo chmod -R 755 foldername 

but as mentioned above, you need to be careful with the second method.

There are two answers to finding files and applying chmod to them.

The first one is find the file and apply chmod as it finds (as suggested by @WombleGoneBad).

find /opt/lampp/htdocs -type d -exec chmod 755 <> \; 

The second solution is to generate a list of all files with the find command and supply this list to the chmod command (as suggested by @lamgesh).

chmod 755 $(find /path/to/base/dir -type d) 

Both of these versions work nicely as long as the number of files returned by the find command is small. The second solution looks great to the eye and is more readable than the first one. If there are a large number of files, the second solution returns an error: Argument list too long.

  1. Use chmod -R 755 /opt/lampp/htdocs if you want to change the permissions of all files and directories at once.
  2. Use find /opt/lampp/htdocs -type d -exec chmod 755 <> \; if the number of files you are using is very large. The -type x option searches for a specific type of file only, where d is used for finding the directory, f for file and l for link.
  3. Use chmod 755 $(find /path/to/base/dir -type d) otherwise
  4. Better to use the first one in any situation

You want to make sure that appropriate files and directories are chmod-ed/permissions for those are appropriate. For all directories you want

find /opt/lampp/htdocs -type d -exec chmod 711 <> \; 

And for all the images, JavaScript, CSS, HTML. well, you shouldn’t execute them. So use

But for all the logic code (for instance PHP code), you should set permissions such that the user can’t see that code:

I think Adam was asking how to change the umask value for all processes that are trying to operate on the /opt/lampp/htdocs directory.

The user file-creation mode mask (umask) is used to determine the file permissions for newly created files. It can be used to control the default file permissions for new files.

so if you will use some kind of FTP program to upload files into /opt/lampp/htdocs you need to configure your FTP server to use the umask you want.

If files / directories need be created, for example, by PHP, you need to modify the PHP code:

If you will create new files / folders from your Bash session, you can set umask value in your shell profile ~/.bashrc file.

Or you can set up a umask in /etc/bashrc or /etc/profile file for all users.

Add the following to the file:

umask 022 Sample umask Values and File Creation Permissions If umask value set to User permission Group permission Others permission 000 all all all 007 all all none 027 all read / execute none 

And to change permissions for already created files, you can use find.

Источник

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