LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2016, 01:12 AM   #1
scriptkiddy
LQ Newbie
 
Registered: Feb 2016
Posts: 18

Rep: Reputation: Disabled
Question Recursive chmod not working as it should


I logged in as root on my test/lab CentOS 7 box, and wanted to put read and write permissions on all home directories and subdirectories for "other" (just to test, I'm trying to learn chmod -R a bit more).

So I ran:
Code:
# chmod -R o+rwX /home
It worked, however it also put rw on files within subdirectories. Example: A simple touch test1 by root created a file under "user1" home directory as owner root, group root, with standard 644 permissions. However, after the above chmod command was run, it was 646.

Per the man of ls, the capital X is supposed to work on directories only.

Thoughts?


SK
 
Old 03-08-2016, 01:38 AM   #2
OregonJim
Member
 
Registered: Feb 2016
Posts: 98

Rep: Reputation: Disabled
Quote:
Originally Posted by scriptkiddy View Post
the capital X is supposed to work on directories only.

Thoughts?


SK
(X): execute/search only if the file is a directory or already has execute permission for some user.
 
Old 03-08-2016, 01:47 AM   #3
scriptkiddy
LQ Newbie
 
Registered: Feb 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
@OregonJim

I'm not sure I understand your reply. I agree that is what the manpage states. However, the test1 file had 644, which is read permissions for other. So it did not "already have execute permissions for some user", so a file with read only for other does not meet this criteria.

SK
 
Old 03-08-2016, 01:48 AM   #4
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Never knew of the Capital X.

You can use find to get things straight

Quote:
find -type d /home -exec chmod o+rwx \{\} \;
 
1 members found this post helpful.
Old 03-08-2016, 01:57 AM   #5
OregonJim
Member
 
Registered: Feb 2016
Posts: 98

Rep: Reputation: Disabled
Quote:
Originally Posted by scriptkiddy View Post
@OregonJim

I'm not sure I understand your reply. I agree that is what the manpage states. However, the test1 file had 644, which is read permissions for other. So it did not "already have execute permissions for some user", so a file with read only for other does not meet this criteria.

SK
And I agree that it doesn't apply to your example case - I was just pointing out that your assumption of X only touching directories was incorrect. It will also touch files if they are already executable by anyone.

Last edited by OregonJim; 03-08-2016 at 02:00 AM.
 
Old 03-08-2016, 02:17 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by scriptkiddy View Post
I logged in as root on my test/lab CentOS 7 box, and wanted to put read and write permissions on all home directories and subdirectories for "other" (just to test, I'm trying to learn chmod -R a bit more).

So I ran:
Code:
# chmod -R o+rwX /home
It worked, however it also put rw on files within subdirectories. Example: A simple touch test1 by root created a file under "user1" home directory as owner root, group root, with standard 644 permissions. However, after the above chmod command was run, it was 646.

Per the man of ls, the capital X is supposed to work on directories only.
Wrong on three counts...

First, it doesn't matter really what man ls says, you are setting perms with chmod, so be precise and see what man chmod says. And it says...

Code:
       The letters rwxXst select file mode bits for the affected users: read (r), write (w), execute (or search
       for  directories)  (x), execute/search only if the file is a directory or already has execute permission
       for some user (X)...
So, second, it will set the search bit (also called traverse bit) on all sub-directories, and set execute on files if they are already executable by at least one of owner|group|other. So, it works on directories and files, not just directories.

And third, the status of the x bit doesn't mask or set the read or write bits. So, because you told it to set o+rwX, it did indeed set all the files to rw for other, just as you asked it to do.

Last edited by astrogeek; 03-08-2016 at 02:21 AM.
 
1 members found this post helpful.
Old 03-09-2016, 11:34 PM   #7
scriptkiddy
LQ Newbie
 
Registered: Feb 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
Hmm...

I see what you are saying astrogeek, and what the man of chmod is saying, but I'm not making the connection on one part: How did Red Hat determine that a file met the stated man page criteria?

We agree that (chmod) man page says "...execute/search only if the file is a directory or already has execute permission for some user (X)". This means there are two possible criteria:

1. File is a directory
OR
2. Already has execute permissions for some user

It was a file, so it does not meet #1. No user had execute permissions as it was 644, so it does not meet #2. That is where my little brain explodes, as the command should only set all the files to rw for other if it meets #1 or #2 above per the man page. The only possibility I can think of is that Red Hat took #1 and #2 and combined them. It applied it to the file because it was under a directory that had execute permissions on it for some user...

Maybe a better question is "How would I recursively set read write permissions only on directories (for other) using chmod command?". (I liked your alternate route there zhjim, but trying to wrap my head around this).

SK
 
Old 03-10-2016, 12:40 AM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by scriptkiddy View Post
I see what you are saying astrogeek, and what the man of chmod is saying, but I'm not making the connection on one part: How did Red Hat determine that a file met the stated man page criteria?
It does meet the criteria, read it again carefully.
Quote:
Originally Posted by scriptkiddy View Post
We agree that (chmod) man page says "...execute/search only if the file is a directory or already has execute permission for some user (X)". This means there are two possible criteria:

1. File is a directory
OR
2. Already has execute permissions for some user

It was a file, so it does not meet #1. No user had execute permissions as it was 644, so it does not meet #2. That is where my little brain explodes, as the command should only set all the files to rw for other if it meets #1 or #2 above per the man page. The only possibility I can think of is that Red Hat took #1 and #2 and combined them. It applied it to the file because it was under a directory that had execute permissions on it for some user...
The part in red, no, you have not got that right.

You are trying to apply the action controlled by the X bit to select which files are affected for the r and w bits also - that is not what it says.

In the chmod -R o+rwX command, r sets the read bit, w sets the write bit, and x sets the execute bit for files, or the search bit for directories. X (upper case) also sets the execute/search bit for directories, and for regular files only if it is already set for at least one of owner|group|other.

X (upper case) does not determine which files or directories are affected by the read or write bits - they are totally independent.


As I said,
Quote:
...the status of the x bit doesn't mask or set the read or write bits. So, because you told it to set o+rwX, it did indeed set all the files to rw for other, just as you asked it to do.
Quote:
Originally Posted by scriptkiddy View Post
Maybe a better question is "How would I recursively set read write permissions only on directories (for other) using chmod command?". (I liked your alternate route there zhjim, but trying to wrap my head around this).
You cannot do that recursively with chmod alone.

Use the find command to find all subdirectories, and the exec option to chmod them, something like this...

Code:
find /path/to/search -type d -exec chmod o+rw {} ;
You may need to escape the {} and/or ;, see man find for correct usage of that one.

Last edited by astrogeek; 03-10-2016 at 12:57 AM.
 
1 members found this post helpful.
Old 03-10-2016, 01:54 AM   #9
scriptkiddy
LQ Newbie
 
Registered: Feb 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
X (upper case) does not determine which files or directories are affected by the read or write bits - they are totally independent.
Ahhhhh.... Light bulb.

I think when I read the man page the word "execute" stuck out. And so in my head I thought "execute my chmod wishes"... and put the capital X as a filter for my wish (to only add rw to directories and not files).

Many thanks for the in depth explanations!

SK
 
Old 03-10-2016, 02:27 AM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by scriptkiddy View Post
Ahhhhh.... Light bulb.

I think when I read the man page the word "execute" stuck out. And so in my head I thought "execute my chmod wishes"... and put the capital X as a filter for my wish (to only add rw to directories and not files).

Many thanks for the in depth explanations!
I know those light bulb moments well!

You are welcome, and good luck!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 07:03 PM
chmod recursive on files on dlublink Linux - Newbie 6 03-02-2005 08:45 AM
recursive yet selective chmod bluefire Linux - General 6 10-22-2004 06:25 PM
chmod.....recursive help stateq2 Linux - General 3 03-28-2004 07:28 PM
Messed up recursive with chmod Cyth Linux - General 4 01-03-2003 12:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration