ChatGPT解决这个技术问题 Extra ChatGPT

Chmod recursively

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: chmod: How to recursively add execute permissions only to files which already have execute permission 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.

-r not the same as -R
Misc note, if you want to say make all group permissions match the owner permissions (seems less likely to fug everything up IMO) thats totally possible to serverfault.com/a/284478/210994

D
Dmitry

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


F
Fred Foo

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 {} \;

What is the difference between '\;' and '+' that was used by the OP at the end of the command?
@konpsych Apparently + builds the command-line in a different way. I wasn't actually aware of it; \; is the usual way, with one command per file that matches the search.
Doesn't work if some files have spaces in their name
@Soonts Then "{}" instead of {} should fix it.
I ran this under the linux subsystem on Windows 10 and it had the opposite effect. Most file permissions were removed.
c
choroba

Try to change all the persmissions at the same time:

chmod -R +xr

That will put execute permissions on all non-directories as well.
Yes. This just replaces the first iterated step. You still have to run find -type f -exec chmod -x {} + afterwards.
J
João Mantovani

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

Add the current folder location: chmod -R 0777 .
C
Community

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 (|)


A
Anthony Vinay

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

chmod -R 0777 ./