LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   changing permissions only for directories (https://www.linuxquestions.org/questions/linux-newbie-8/changing-permissions-only-for-directories-535320/)

xpucto 03-07-2007 04:06 AM

changing permissions only for directories
 
Hi!
I've installed a web-application and would like to set the permission 700 for directories and 600 for files. How may I use chmod -R so that it first changes permissions for directories only, and then only for the files?
thanks.

b0uncer 03-07-2007 04:09 AM

Directories are files as well, only special kinds of files. Have you already tried with find with the option to print only files that are directories, and then pass the list to chmod command?

EDIT: I forgot, it's always nice to have a ready example to use.

Example #1: change all files' permissions to 600 and all directories permissions to 700, so that the directory where you run this command from is chmod'ed as well as subdirectories:
Code:

find ./ -depth -type f -exec chmod 600 {} \;
find ./ -depth -type d -exec chmod 700 {} \;

Example #2: the same (file permissions -> 600, directory permissions -> 700) except that don't chmod the parent (=current) working directory, only deal with files inside current directory and subdirectories:
Code:

find ./ -depth -type f -exec chmod 600 {} \;
find ./ -depth -mindepth 1 -type d -exec chmod 700 {} \;

The above lines are easy and there are surely other ways around for this too. The idea above is basically to use find to find files of given type and then do (-exec) something for them. Type 'f' stands for 'file' (regular file) and type 'd' stands for directory. Depth tells find to start from the bottom (might not be necessary though), and in the lower example '-mindepth 1' tells find that the minimum depth to be inspected is one, meaning that the parent directory (the one you're doing this in) is number zero and isn't what we're looking for (1 is for the subdirs and files under that directory, 2 for sub-subdirectories, ...). Exec works easily: after the '-exec' switch you type the command you'd like to do on each file, then put the braces {} which gets replaced by each file find finds, one at a time, and at the end you put semicolon ';' that tells find where the command ends (and here you use \ to protect the semicolon from being interpreted by your shell). Actually you could just first run 'chmod -R 600' for everything in the directory and then run the find command to chmod directories only, which would be effectively the same, but it's your choice -- I like this method more, that way you deal with regular files and directories only, and don't touch fifos for example if you happened to have them lying around for some bizarre reason ;).

find is a great tool for doing tasks for files, I recommend you to learn to use it, at least the simplest ways. More information and some examples you get by issuing
Code:

man find
and perhaps
Code:

info find
if you have texinfo pages installed. I myself use find regularly because of it's ability to do tasks quickly that I'd otherwise have to build a shell script for, and because it's just so overall handy. For actually finding files on the system that I can't remember where they are I often use (s)locate:
Code:

locate search_string_here
unless your machine is on day and night and it's done automatically, it's a good idea to once in a while refresh the database (as root):
Code:

updatedb
because locate only "locates" files that are in it's database, so newly created files will only show up after running updatedb. Database is usually updated via cronjob but if it's scheduled for the night and you power off your computer, for example, the database might not be up-to-date. When it is up to date, it's generally a whole lot faster than find when you want to locate some file(s), but if you want to do something with the files found (and control more what you want to find), don't forget find.

An other cool tool you might need one day: sed -- check it's man page out too when you have time, unless you already know it; it's about the same for files' contents than find is for files themselves (that's actually a very bad compare, but..)

xpucto 03-11-2007 05:09 AM

Thanks b0uncer for this tutorial! Very helpfull!


All times are GMT -5. The time now is 03:42 PM.