Solved

Set Permisssions differently on files and folders

Posted on 2006-07-03
10
1,411 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 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
 
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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 …
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now