Give execution permission to directories but not to files
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Quiet Intelligence
--
Chapters
00:00 Give Execution Permission To Directories But Not To Files
00:34 Answer 1 Score 0
01:01 Answer 2 Score 2
01:27 Accepted Answer Score 26
02:53 Answer 4 Score 0
03:52 Thank you
--
Full question
https://superuser.com/questions/757806/g...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #bash #filepermissions #chmod
#avk47
ACCEPTED ANSWER
Score 26
To give execution (search) permission to directories, but not to files, use:
chmod -R +X .
To assign all the permissions as in your example, use:
chmod -R u=rwX,g=rX,o= .
-R
changes files and directories recursively, while +X
sets execute/search only if the file is a directory or already has execute permission for some user. r
and w
are of course for reading and writing, respectively.
Mode X
(upper x) is documented in both the traditional manual page1 and the info documentation2.
It should also work on other Unix like systems, e.g. FreeBSD, NetBSD or OpenBSD. Quoting from the chmod(1) man page of The Open Group Base Specifications Issue 7, 2018 edition:
The X perm symbol was adopted from BSD-based systems because it provides commonly desired functionality when doing recursive (-R option) modifications. Similar functionality is not provided by the find utility. Historical BSD versions of chmod, however, only supported X with op+; it has been extended in this volume of POSIX.1-2017 because it is also useful with op=. (It has also been added for op- even though it duplicates x, in this case, because it is intuitive and easier to explain.)
1 man 1 chmod
2 info '(coreutils)Conditional Executability'
ANSWER 2
Score 2
If you want to do it recursively, i.e., to directories within directories within directories, the command to use is:
find /path/to/starting/directory -type d -exec chmod +x {} \;
This locates all and only subdirectories (-type d flag) of the directory /path/to/starting/directory, and then performs the required change of execute permission to each one of them. The space before \; is mandatory.
ANSWER 3
Score 0
Have you tried something like:
chmod +x $(ls -p | grep /)
, this adds the execution permission ton only directories
and if you would like to give execution permission to files and directories, just do :
chmod +x *
and to delete permissions to execute to files try something like this :
chmod -x *.*
Hoping it helps
ANSWER 4
Score 0
Try this:
find . -mindepth 1 -print0 | while IFS= read -r -d '' file; do
if [ -d "$file" ]; then chmod 750 "$file";
else chmod 640 "$file"; fi
done
Explanation
-mindepth 1
: That's sofind
wont match the current directory,.
.-print0
: prints null-separated output. This ensures that we deal correctly with fioles containing newlines.while IFS=
: settingIFS
to the empty string turns off word splitting. Necessary for file/directory names with whitespace.read -r -d ''
: this reads each line into$file
but the-r
ensures we don't treat\
specially (in case there are some in your file names) and-d ''
sets the delimiter to the null string so we parse the output offind
correctly and can deal with fiole names containing newlines.if [ -d "$file" ]; then chmod 750 "$file";
: If the$file
is a directory ([ -d "$file" ]
, set its permissions todrwxr-x---
.else chmod 640 "$file"; fi
: if it's not a directory, set them to-rw-r-----
.