Chmod recursively

LinuxShellFindChmod

Linux Problem Overview


I have an archive, which is archived by someone else, and I want to automatically, after I download it, to change a branch of the file system within the extracted files to gain read access. (I can't change how archive is created).

I've looked into this thread: https://stackoverflow.com/questions/6937885/chmod-how-to-recursively-add-execute-permissions-only-to-files-which-already-ha as into some others, but no joy.

The directories originally come with multiple but all wrong flags, they may appear as:

drwx------
d---r-x---
drwxrwxr-x
dr--r-xr--

Those are just the few I've discovered so far, but could be more.

find errors when tries to look into a directory with no x permission, and so doesn't pass it to chmod. What I've been doing so far, is manually change permissions on the parent directory, then go into the child directories and do the same for them and so on. But this is a lot of hand labour. Isn't there some way to do this automatically?

I.e. how I am doing it now:

do:

$ chmod -R +x
$ chmod -R +r

until I get no errors, then

$ find -type f -exec chmod -x {} +

But there must be a better way.

Linux Solutions


Solution 1 - Linux

You can use chmod with the X mode letter (the capital X) to set the executable flag only for directories.

In the example below the executable flag is cleared and then set for all directories recursively:

~$ mkdir foo
~$ mkdir foo/bar
~$ mkdir foo/baz
~$ touch foo/x
~$ touch foo/y

~$ chmod -R go-X foo 
~$ ls -l foo
total 8
drwxrw-r-- 2 wq wq 4096 Nov 14 15:31 bar
drwxrw-r-- 2 wq wq 4096 Nov 14 15:31 baz
-rw-rw-r-- 1 wq wq    0 Nov 14 15:31 x
-rw-rw-r-- 1 wq wq    0 Nov 14 15:31 y

~$ chmod -R go+X foo 
~$ ls -l foo
total 8
drwxrwxr-x 2 wq wq 4096 Nov 14 15:31 bar
drwxrwxr-x 2 wq wq 4096 Nov 14 15:31 baz
-rw-rw-r-- 1 wq wq    0 Nov 14 15:31 x
-rw-rw-r-- 1 wq wq    0 Nov 14 15:31 y

A bit of explaination:

  • chmod -x foo - clear the eXecutable flag for foo
  • chmod +x foo - set the eXecutable flag for foo
  • chmod go+x foo - same as above, but set the flag only for Group and Other users, don't touch the User (owner) permission
  • chmod go+X foo - same as above, but apply only to directories, don't touch files
  • chmod -R go+X foo - same as above, but do this Recursively for all subdirectories of foo

Solution 2 - Linux

You need read access, in addition to execute access, to list a directory. If you only have execute access, then you can find out the names of entries in the directory, but no other information (not even types, so you don't know which of the entries are subdirectories). This works for me:

find . -type d -exec chmod +rx {} \;

Solution 3 - Linux

Try to change all the persmissions at the same time:

chmod -R +xr

Solution 4 - Linux

To make everything writable by the owner, read/execute by the group, and world executable:

chmod -R 0755

To make everything wide open:

chmod -R 0777

Solution 5 - Linux

Adding executable permissions, recursively, to all files (not folders) within the current folder with sh extension:

find . -name '*.sh' -type f | xargs chmod +x

* Notice the pipe (|)

Solution 6 - Linux

Give 0777 to all files and directories starting from the current path :

> chmod -R 0777 ./

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser797257View Question on Stackoverflow
Solution 1 - LinuxDmitryView Answer on Stackoverflow
Solution 2 - LinuxFred FooView Answer on Stackoverflow
Solution 3 - LinuxchorobaView Answer on Stackoverflow
Solution 4 - LinuxTim JensenView Answer on Stackoverflow
Solution 5 - LinuxAlikElzin-kilakaView Answer on Stackoverflow
Solution 6 - LinuxAnthony VinayView Answer on Stackoverflow