LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-17-2008, 05:38 PM   #1
ramayana
LQ Newbie
 
Registered: May 2008
Posts: 4

Rep: Reputation: 0
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?
 
Old 06-17-2008, 05:46 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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 05:49 PM. Reason: Clarification
 
Old 06-17-2008, 07:03 PM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 69
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.
 
Old 06-18-2008, 03:53 PM   #4
ramayana
LQ Newbie
 
Registered: May 2008
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 06-18-2008, 04:10 PM   #5
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
Quote:
Originally Posted by ramayana View Post
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.
 
Old 06-18-2008, 04:33 PM   #6
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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 04:36 PM. Reason: Added p.s.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
chmod 775 to only the directories and chmod 664 to only the files? apachenew Linux - Security 6 09-27-2007 04:26 PM
chmod, external usb, vfat - can't chmod a directory itsjustme Slackware 2 04-02-2006 05:23 PM
What can we do if we type chmod ugo-x /bin/chmod ?????? bunny123 Linux - Software 3 02-01-2005 09:53 PM
CHMOD in shell : chmod 777 /usr/ <---is that right? cpanelskindepot Programming 5 07-16-2004 06:37 AM
CHMOD problem michn77 Linux - Newbie 0 02-12-2004 06:15 AM


All times are GMT -5. The time now is 04: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration