Solved

Set Permisssions differently on files and folders

Posted on 2006-07-03
10
1,409 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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 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…

744 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

14 Experts available now in Live!

Get 1:1 Help Now