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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-17-2008, 04:38 PM
|
#1
|
LQ Newbie
Registered: May 2008
Posts: 4
Rep:
|
chmod -R problem
I'm having some problems using/understanding chmod.
When in the right directory,
chmod -R 600 *.doc
produces no error message, and doesn't change any of the permissions of .doc files in the subdirectories.
When in the same directory,
chmod -R 600 2007_Training_Record/*.doc
gives the error message
chmod: cannot access `2007_Training_Record/*.doc': No such file or directory
That subdirectory (2007_Training_Record) does exist, and it has 7 subdirectories, and they're full of .doc files.
Can anyone please tell me what I'm doing wrong?
|
|
|
06-17-2008, 04:46 PM
|
#2
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
It isn't a problem with chmod, it is a problem with what you are expecting the * to expand to.
chmod -R 600 *.doc will chmod every file ending in ".doc" in the current directory and ALL files in any subdirectory that ends in ".doc". Likewise with the second command except it will do that for files in the 2007_Training_Record directory. Since you don't have any files that end with ".doc" or any directories that end with ".doc" it gives you a file not found error.
What you want to do is:
Code:
find . -name \*.doc -exec chmod 600 {} \;
With the -exec paramater to the find command, "{}" means replace the file that was found in the command. You also must end the command with a ";" but it must be escaped from the shell (thus "\;").
HTH
Forrest
EDIT: When I said, "any subdirectory that ends with '.doc'" I did not mean recursively. Just a subdirectory directly under the one you were in.
Last edited by forrestt; 06-17-2008 at 04:49 PM.
Reason: Clarification
|
|
|
06-17-2008, 06:03 PM
|
#3
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994
Rep:
|
What forrestt says is correct. There's also a more general point here:
When you type *.doc into a command shell, the shell starts off by expanding the *.doc into a list of files (for example, "1.doc, abc.doc" and so on). The shell then executes the command "chmod -R 600 1.doc abc.doc" and so on.
So the programs you run never see special characters you type into filenames like * \ { } except in the case where they don't match any file (hence your error message).
You might also want to look at the umask command, which can set the permissions with which new files are created.
|
|
|
06-18-2008, 02:53 PM
|
#4
|
LQ Newbie
Registered: May 2008
Posts: 4
Original Poster
Rep:
|
That seems a little clearer thanks. So basically chmod -R only looks down through one layer of subdirectories and no further, unlike rsync -r which goes down to the bottom. So it isn't really recursive then.
|
|
|
06-18-2008, 03:10 PM
|
#5
|
Member
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384
Rep:
|
Quote:
Originally Posted by ramayana
So basically chmod -R only looks down through one layer of subdirectories and no further
|
No, that's not it. The -R option will cause chmod to change permissions in all subdirectories of the arguments you specify on the command line. In other words, it takes full file paths as arguments. It does not search based on a pattern.
So, for example, when you ran "chmod -R 600 *.doc", chmod doesn't actually see that "*.doc". That wildcard is expanded by your shell, which substitutes all files in the current directory ending with ".doc". Likewise, when you used 2007_Training_Record/*.doc, the shell expanded that to all the .doc files in the 2007_Training_Record directory.
So, bottom line, chmod doesn't understand wildcards. You give it the path to a file and it will change the permissions on that file. You give it the path to a directory, and the -R option will change the permissions on that directory and on every file and directory under it. If you want to change the permissions on just the .doc files, or filter the changes in some other way, then you need to incorporate another command such as find.
|
|
|
06-18-2008, 03:33 PM
|
#6
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
To make this clearer, supposed ls -lR produced the following output:
Code:
.:
total 8
drwxr-xr-x 3 user group 4096 2008-06-18 16:16 2007_Training_Record
drwxr-xr-x 2 user group 4096 2008-06-18 16:14 asdf.doc
-rw-r--r-- 1 user group 0 2008-06-18 16:15 somedoc3.doc
-rw-r--r-- 1 user group 0 2008-06-18 16:15 somedoc4.doc
-rw-r--r-- 1 user group 0 2008-06-18 16:15 somedoc.doc
./2007_Training_Record:
total 4
drwxr-xr-x 2 user group 4096 2008-06-18 16:16 1234.doc
-rw-r--r-- 1 user group 0 2008-06-18 16:15 somedoc4.doc
./2007_Training_Record/1234.doc:
total 0
-rw-r--r-- 1 user group 0 2008-06-18 16:16 somedoc4.doc
./asdf.doc:
total 0
-rw-r--r-- 1 user group 0 2008-06-18 16:16 somedoc.doc
The command "chmod -R 600 *.doc" will expand to the command:
Code:
chmod -R asdf.doc somedoc3.doc somedoc4.doc somedoc.doc
It will recursively change the mode on the asdf.doc directory and the files somedoc3.doc, somedoc4.doc, and somedoc.doc.
The command "chmod -R 600 2007_Training_Record/*.doc" will expand to the command:
Code:
chmod -R 2007_Training_Record/1234.doc 2007_Training_Record/somedoc4.doc
It will recursively change the mode on the 2007_Training_Record/1234.doc directory and the file 2007_Training_Record/somedoc4.doc.
If the contents of 2007_Training_Record were directories like:
Code:
drwxr-xr-x 2 user group 4096 2008-06-18 16:16 01-Jan
drwxr-xr-x 2 user group 4096 2008-06-18 16:16 02-Feb
drwxr-xr-x 2 user group 4096 2008-06-18 16:16 03-Mar
drwxr-xr-x 2 user group 4096 2008-06-18 16:16 04-Apr
drwxr-xr-x 2 user group 4096 2008-06-18 16:16 05-May
drwxr-xr-x 2 user group 4096 2008-06-18 16:16 06-Jun
drwxr-xr-x 2 user group 4096 2008-06-18 16:16 07-Jul
Then when the shell tried to expand the "*" it wouldn't find anything that matched (i.e. there are no files or directories inside the 2007_Training_Record that end in ".doc") which causes the error you see.
HTH
Forrest
p.s. One more thing. To see what the shell will expand your wild card to run echo instead of chmod
Code:
echo 2007_Training_Record/*/*.doc
Last edited by forrestt; 06-18-2008 at 03:36 PM.
Reason: Added p.s.
|
|
|
All times are GMT -5. The time now is 03:13 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|