Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-07-2007, 05:06 AM   #1
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 524

Rep: Reputation: 31
changing permissions only for directories

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?
Old 03-07-2007, 05:09 AM   #2
LQ Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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:
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:
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
man find
and perhaps
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:
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):
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..)

Last edited by b0uncer; 03-07-2007 at 05:30 AM.
Old 03-11-2007, 06:09 AM   #3
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 524

Original Poster
Rep: Reputation: 31
Thanks b0uncer for this tutorial! Very helpfull!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
finding directories w/ certain permissions secular Linux - Security 1 12-28-2004 05:36 PM
home directories permissions black_man Debian 1 11-20-2004 10:48 AM
change file permissions recursivly without changing permission of directories eNTi Linux - Software 8 10-15-2004 04:37 PM
Changing Permissions on Directories... bob4432 Linux - Newbie 6 06-25-2004 03:50 PM
permissions home directories powadha Debian 6 06-11-2004 12:45 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration