Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Set Permisssions differently on files and folders

Posted on 2006-07-03
10
Medium Priority
?
1,439 Views
Last Modified: 2013-12-16
Hi all,
My problem is as follows:

What i want to do is set the mode of files and directories differently (including hidden files and hidden directories)



So lets say i want to make all files 0640 and the directories 6750

I know i can always recursively set the permissions on everything by typing:
        cd /my/root/dir/
      chmod -R 0640 *

but is there a way to pick up every subdir and give it different values without manually going through each directory?

The directories i'm refering to have many nested files and folders so the manual process of first setting the files permissions is easy, but setting the folders is boring.

I presume this can be achieved with the recursive option in chmod and some intelligent use of grep, but i have no idea how.

Any help would be greatly appreciated.

couldntcarebear
0
Comment
Question by:couldntcarebear
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 22

Expert Comment

by:pjedmond
ID: 17030109
The following changes directories only :

find /my/root/dir -type d | awk {'print "chmod +755 " $0'} | /bin/sh

Lets talk through it:

find /my/root/dir -type d

lists only the directories - This is passed to

awk {'print "chmod +755 " $0'}    where $0 is the directory path. awk merely builds up the command. If you run

find /my/root/dir -type d | awk {'print "chmod +755 " $0'}

you can see the commands that you would like to run:) A good test to run this to confirm what commands you will get before the next step!

...As we'd like to run these commands, lets pipe them to a shell:


find /my/root/dir -type d | awk {'print "chmod +755 " $0'} | /bin/sh

This is an extrmely powerful process for building up very selective commands!

(   (()
(`-' _\
 ''  ''



0
 
LVL 22

Accepted Solution

by:
pjedmond earned 2000 total points
ID: 17030116
Best I read the Q!

>So lets say i want to make all files 0640 and the directories 6750

1. Directories:

find /my/root/dir -type d | awk {'print "chmod 6750 " $0'} | /bin/sh

2. Files:

find /my/root/dir -type f | awk {'print "chmod 0640 " $0'} | /bin/sh

There are other 'types' for symbolic links etc

(   (()
(`-' _\
 ''  ''
0
 
LVL 16

Expert Comment

by:xDamox
ID: 17037281
Hi,

You could try the followng:

find /path -type d -exec chmod 6750 {} \;
find /path -type f -exec chmod 0640  {} \;
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 3

Expert Comment

by:bryanlloydharris
ID: 17164373
I like Damox' way, but here is another:

cd /path/
chmod -R 6750 .
find . -type f -exec chmod 0640 {} \;

PJ, why do you have to pipe to "sh"?  Won't it do it correctly without that?
0
 
LVL 16

Expert Comment

by:xDamox
ID: 17333809
why should pjedmond just get the points it should be a point split between

pjedmond xDamox and bryanlloydharris
0
 
LVL 25

Expert Comment

by:Cyclops3590
ID: 17333926
I was debating whether to split the points or not between pjedmond and you (xDamox) since they were different solutions to the same issue.  Reason I didn't was because  because pjedmond's solution is correct and was first.

However, I wasn't going to split to bryanlloydharris, because it just seemed like too much of a duplicate of your (xDamox) post.

The moderator can split however they please as this one is questionable, but if there is a split I'd recommend only between pjedmond and xDamox; however, I will stand by my original recommendation.
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 17335571
I'm also a little dubious about this as a process. I personally feel that for this site to perform in the best possible manner for those searching for solutions, then given the option to split points between accurate and useful solutions helps indicate to the person browsin the site that those marked are valid solutions. Then again, the solutions marked as being correct by some users (or indeed occassionally the moderators) does sometimes leave something to be desired.

However a guess that is site 'policy' decision as to guidelines provided. I'm happy with the decision either way, and appreciate the work done by the cleanup voluteers - Having done a bit myself, I have to confess that it doesn't really excite me that much! Considering that, Cyclops does an exceptionally good job in keeping this TA tidy - keep up the good work.

...and to answer bryanlloydharris' Q - You need to pipe it to a shell to execute the commands, otherwise they are merely printed to the console.

(   (()
(`-' _\
 ''  ''

0
 
LVL 25

Expert Comment

by:Cyclops3590
ID: 17335684
pjedmond, thank you for the kind remark.

However I do want to say that if someone does disagree with my recommendation, I do want to hear it.  While keeping a TA tidy is probably the key function of a CV, I want to make sure to be as fair as possible to all the experts while making sure that the recommendations I make (since they are accepted most of the time) will give the greatest value to the EE database. (Being as I am one who searches the database for answers as well)   If I am being unfair or am using bad logic in coming to a recommendation let me know and I'll change if it is sufficiently pointed out to me.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question