Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Need help with a script to set permissions
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
wilburpan
l33t
l33t


Joined: 21 Jan 2003
Posts: 977

PostPosted: Mon Jul 06, 2009 9:37 pm    Post subject: Need help with a script to set permissions Reply with quote

I have a directory that holds a lot of document/media files, called FileCabinet. There are a lot of subdirectories and they are nested pretty deeply.

Here's my issue: I'd like to make sure that all of these files have 0644 permissions. No problem, right? I entered this command:

Code:
chmod 0644 -R FileCabinet


Which should convert everything in this folder to 0644, permission-wise.

Problem is, that command also converted the folder permissions to 644, which played havoc with the subdirectories. I would get this issue:

Code:
ls -l FileCabinet
ls: cannot access FileCabinet/Pictures: Permission denied
ls: cannot access FileCabinet/Movies: Permission denied
ls: cannot access FileCabinet/Documents: Permission denied
total 0
?????????? ? ? ? ?                ? Documents
?????????? ? ? ? ?                ? Movies
?????????? ? ? ? ?                ? Pictures


Obviously, what happened is that with the last command, I changed the directories to 0644 as well, which my system doesn't like. This is fixable -- I just change everything to 0755.

But is there a way to write a script that traverses the directory and changes everything to 0644 except for the directories themselves?
_________________
I'm only hanging out in OTW until I get rid of this stupid l33t ranking.....Crap. That didn't work.
Back to top
View user's profile Send private message
Nerevar
l33t
l33t


Joined: 31 May 2008
Posts: 720

PostPosted: Mon Jul 06, 2009 9:56 pm    Post subject: Reply with quote

Code:
find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644
Back to top
View user's profile Send private message
wilburpan
l33t
l33t


Joined: 21 Jan 2003
Posts: 977

PostPosted: Tue Jul 07, 2009 2:16 pm    Post subject: Reply with quote

Thanks -- that did the trick!

I need to spend more time with the 'find' and 'xargs' commands.
_________________
I'm only hanging out in OTW until I get rid of this stupid l33t ranking.....Crap. That didn't work.
Back to top
View user's profile Send private message
alex.blackbit
Advocate
Advocate


Joined: 26 Jul 2005
Posts: 2397

PostPosted: Tue Jul 07, 2009 2:29 pm    Post subject: Reply with quote

Nerevar wrote:
Code:
find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644
Code:
$ find <path> -type d -exec chmod 0755 {} \; && find <path> -type f -exec chmod 0644 {} \;
is possible too, IMHO a little bit more elegant.
Back to top
View user's profile Send private message
Nerevar
l33t
l33t


Joined: 31 May 2008
Posts: 720

PostPosted: Tue Jul 07, 2009 3:33 pm    Post subject: Reply with quote

alex.blackbit wrote:
Nerevar wrote:
Code:
find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644
Code:
$ find <path> -type d -exec chmod 0755 {} \; && find <path> -type f -exec chmod 0644 {} \;
is possible too, IMHO a little bit more elegant.

I don't like the exec switch, but if you insist on using it you should look at the "-exec command {} +" variant in the manpage. It will speed up the operation on directories with many files.
Back to top
View user's profile Send private message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2719
Location: here, there or in transit

PostPosted: Tue Jul 07, 2009 3:36 pm    Post subject: Reply with quote

alex.blackbit wrote:
Code:
$ find <path> -type d -exec chmod 0755 {} \; && find <path> -type f -exec chmod 0644 {} \;
is possible too, IMHO a little bit more elegant.
In addition, use + instead of \; to get behavior more like an xargs pipeline (fewer spawned processes).

Edit: Bah. Too slow. What he (Nerevar) said. :wink:
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
alex.blackbit
Advocate
Advocate


Joined: 26 Jul 2005
Posts: 2397

PostPosted: Tue Jul 07, 2009 6:51 pm    Post subject: Reply with quote

k, thx.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum