Solved

Set Permisssions differently on files and folders

Posted on 2006-07-03
10
1,427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

635 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