chmod -R versus chmod *

What is the difference in outcomes when using chmod -R /dir/subdir and chmod /dir/subdir/* ?
Let's say we have /dir/subdir/file1. And we are assigning read and write perms to subdir and its file(s) for user and group owner, nothing for Others but it seems implied since permissions for Others aren't even mentioned. I am saying:
chmod -R 660 /dir/subdir
but the solution given in the guide says it should be:
chmod 660 /dir/subdir/*
What's with the asterisk?

Comments

  • hiddenknight821hiddenknight821 Member Posts: 1,209 ■■■■■■□□□□
    Just gave this a try to see what's the difference. Turned out the asterisk doesn't traverse to the subdirectories. So, you'd be applying permissions to only the files and directories at that directory level, but not the subdirectories below.
  • DoubleNNsDoubleNNs Member Posts: 2,015 ■■■■■□□□□□
    Just to add on to what hiddenknight821 said:

    The asterick is a glob/wildcard and is essentially like doing a "for loop" on the results of $I(ls).
    The -R stands for "recursive." It essentially keeps going until it can't anymore.
    Goals for 2018:
    Certs: RHCSA, LFCS: Ubuntu, CNCF CKA, CNCF CKAD | AWS Certified DevOps Engineer, AWS Solutions Architect Pro, AWS Certified Security Specialist, GCP Professional Cloud Architect
    Learn: Terraform, Kubernetes, Prometheus & Golang | Improve: Docker, Python Programming
    To-do | In Progress | Completed
  • masqmasq Member Posts: 33 ■■□□□□□□□□
    My 2 cents:
    asterisk is a wildcard processed by your shell, like bash or whatever you have, not by cp command;
    so your command chmod 660 /dir/subdir/* eventually expands to :
    $chmod 660 /dir/subdir/file1
    $chmod 660 /dir/subdir/file2
    $chmod 660 /dir/subdir/anothersubdir1/
    $chmod 660 /dir/subdir/anothersubdir2/
    ...
    
    and it doesn't effect files and subfolders in "anothersubdirs", for this you must explicitly specify -r key for cp command, so it would go recursively, i.e. for all the subfolders and files it contains.

    Besides,if you have any folders in that subdir of yours, changing permission bits to 660 for these folders won't make you any good , because you need an executive bit set for them so it would be possible to change current working to one of these.

    I bet you got the idea.
    2018:
    • RHCE
  • varelgvarelg Banned Posts: 790
    masq wrote: »
    My 2 cents:
    asterisk is a wildcard processed by your shell, like bash or whatever you have, not by cp command;
    so your command chmod 660 /dir/subdir/* eventually expands to :
    $chmod 660 /dir/subdir/file1
    $chmod 660 /dir/subdir/file2
    $chmod 660 /dir/subdir/anothersubdir1/
    $chmod 660 /dir/subdir/anothersubdir2/
    ...
    

    ... Besides,if you have any folders in that subdir of yours, changing permission bits to 660 for these folders won't make you any good , because you need an executive bit set for them so it would be possible to change current working to one of these.

    I bet you got the idea.
    Following your reply, I expanded the structure somewhat to see how far would * reach compared to -R. So, let's say we have the following structure:
    /level0/level1/level2/level3/file1.txt
    
    where "leveln" are directories and file1.txt is a text file. If I issue chmod with asterisk at the end and no -R:
    chmod 660 /level0/level1/*
    
    permissions will change only for contents of level1. The only affected part of the structure was level2, but not level3 and below. Whereas using -R with chmod:
    chmod -R 660 /level0/level1/
    
    changes permissions for level1 AND everything below.
    The wording of the exercise was: existing files should be set rw for user and group owner. Meaning, existing files of /level0/level1. So when reading this, would it be more logical to use -R? What wording would trigger anyone into using asterisk instead of -R?
  • hiddenknight821hiddenknight821 Member Posts: 1,209 ■■■■■■□□□□
    In response to the exercise you were going through, I believe they mean you should use the 'chmod -R'. To be honest, I never came across the asterisk until you brought it up which was just a bit thought-provoking for me. I actually enjoyed this type of question.

    What I believe you need to understand is the difference between absolute and relative access permissions (I made up the 'absolute/relative' but it makes sense here). Absolute permission would be 660, and this is dangerous if you are going to use the '-R' option since certain directories would need the execution bit on all (users, groups, and others) so that anyone with appropriate permissions can at least traverse the directories with the 'ls' command to troubleshoot the system if necessary.

    The only directories that I can think of that have different permission set is the users' home directories including the /root. This is obviously done to prevent other users from snooping in the home directories that are not theirs.

    I prefer the idempotent approach with the relative permission. If I were you, I'd just do this just to be safe: 'chmod -R ug+rw'. This won't affect the existing permission. Let's say the permission was r-xr-xr--. With the command I just mentioned, the new permission would be rwxrwxr--. With the 660, the permission would be rw-rw----.

    The only directories that I can think of that have different permissions set is the users' home directories including the /root.

    UPDATE: Sorry. I just reread your post. I may be missing the content of the exercise you were going through, but if you're certain it's just 'existing files' in the said directory, then you may can change only the files in that directories. Keep in mind, the asterisk would also change the permission on the directories that's sitting in the same location as those files. So either way with -R or octal mode, you'd not complete the task correctly, and would have to make sure only those files have their permissions changed.
Sign In or Register to comment.