LinuxQuestions.org
Register a domain and help support LQ
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 10-30-2007, 11:18 AM   #1
simonx
LQ Newbie
 
Registered: Oct 2007
Posts: 1

Rep: Reputation: 0
Recursively chmod directories


...how do I?
I have a dir structure with .html, .php etc.
I can use:
chmod -R 644 *.html
chmod -R 755 *.php
ok, but can't find a way to change all dirs to e.g. 755
tried getting a list of dirs and pipe to chmod:
sudo ls -R | grep :$ | sed s/:// | chmod 755
(am running ubuntu)
just comes back chmod: missing operand after `755'

yours frustratedly
Simon.
 
Old 10-30-2007, 11:36 AM   #2
guelzimtr
LQ Newbie
 
Registered: Sep 2004
Distribution: Fedora 7
Posts: 15

Rep: Reputation: 0
chmod

you can just cd to the directory you want to change it's permissions and type:

chmod -R 755 ./
 
Old 10-30-2007, 11:49 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Piping a list of directories, that literally are strings separated by newlines, does not tell to chmod what to do. You can use xargs to build and execute commands from the standard input, as in
Code:
sudo ls -R | grep :$ | sed s/:// | xargs chmod 755
A better way to do can be
Code:
sudo find ./test_dir -type d -exec chmod 755 {} \;
this looks for directories under test_dir and execute the specified command on every item found. See man xargs and man find for details.
 
Old 10-30-2007, 11:51 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
You have directory names with extensions like .html?? seems unusual, but I'm not sure how it is relevant.

chmod -R will change everything (directories and files)--I think you are saying you want to change directories only.

I would suggest a loop starting with:
for f in `ls -R` do
...
...
done

what this does is make a list of values in the variable "f", you can then test each one using--eg--the "file" command. If the test shows it is a directory, then:
chmod 755 $f

Note: In your other code, you were grepping using ":$" I assume you meant this to mean ":" at the end of the line. Trouble is, the ":" is not part of the actual directory name---I suspect it is just the character that your terminal uses to designate a directory.
 
Old 10-30-2007, 12:23 PM   #5
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Fedora 14, Ubuntu , Slax 5.1.8, OpenSolaris, Centos 4.8
Posts: 296

Rep: Reputation: 30
Code:
 for i in 
`ls -al | grep '^d' | awk ' NR > 2 ' | awk '{print $9}'` ; 
do chmod 7XX $i ; 
done
There must be a way of combining the two awk commands ?
i.e print only the 9th field of all but the first 2 lines ?
It would make the above script look a bit shorter.

Last edited by uncle-c; 10-30-2007 at 12:29 PM.
 
Old 10-30-2007, 12:28 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I've always done it in two steps. The -print0 argument to find and the -0 argument to xargs mean that the list of files is de-limited with the NULL character - this can save you problems if there are file names with spaces in:
Code:
find /path/to/files -type d -print0 |xargs -0 chmod 755
find /path/to/files -type f -print0 |xargs -0 chmod 644
 
Old 10-31-2007, 01:01 PM   #7
cconstantine
Member
 
Registered: Dec 2005
Distribution: RedHat, Ubuntu
Posts: 101

Rep: Reputation: 15
IGNORE ME: someone else posted this higher up...

why not just
Code:
find . -type d -exec chmod 0755 {} \;
pitfall: besure to back-escape that semicolon to protect it from your shell. The semi needs to go as an arg to find, not a command sep eaten by your shell

Last edited by cconstantine; 10-31-2007 at 01:02 PM.
 
  


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
Using chmod to recursively change directories / files [GOD]Anck Linux - Software 6 11-10-2008 06:16 PM
Recursively zipping directories AckSed Linux - Newbie 2 10-19-2007 07:29 PM
recursively remove directories iAlta Linux - Software 7 10-06-2007 09:25 AM
chmod 775 to only the directories and chmod 664 to only the files? apachenew Linux - Security 6 09-27-2007 03:26 PM
chmod directories recursively mfilippa Linux - Newbie 3 04-17-2006 07:24 PM


All times are GMT -5. The time now is 05:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration