What permission should i apply to a folder so that everyone can see the hierarchy and all the files inside but they should not be able to read and write them. The folder can have multiple other folders with files inside.


Listing directories uses the executable bit, I don't think read actually affects directories at all. You will have to set permissions on each individual file to prevent them from being read or written to. The following commands will do all this for you, execute them from the folder you want this to start at:

find . -type d | xargs chmod o=rx 
find . -type f | xargs chmod o=

find will recursively find files (-type f) and directories (-type d) and print their names (lacking another specified action), and this gets piped to xargs, which executes its arguments with the files it reads from the pipe. chmod changes the permissions for others (o, meaning neither you nor others in the group owning the file) to exactly (=) read/list (rx) for folders and nothing for plain files.

As grumbel pointed out in a comment, this will only work on well-behaved names. To get them all, either use his solution:

find . -type d -print0 | xargs -0 chmod o=rx 
find . -type f -print0 | xargs -0 chmod o=

or find's builtin -exec:

find . -type d -exec chmod o=rx '{}' ';'
find . -type f -exec chmod o= \{\} \;

Note that you can either quote the braces and semicolon like the first line, or escape them like the second, they just have to be there in separate arguments. Also, -exec will execute chmod once for each file, while xargs will do it in as few commands as possible (based on the max command line length). That's why I generally prefer xargs.

  • Use "find . -type f -print0 | xargs -0 chmod o=" to avoid issues with filenames containing unusual characters (i.e. newlines). – Grumbel Nov 5 '11 at 21:24
  • Braces do not need to be escaped – enzotib Nov 5 '11 at 21:43
  • You can use + instead of ; to have find using as few commands as possible. – enzotib Nov 5 '11 at 21:44
  • Without the r bit on a directory, you can cd to it, but cannot list its content. – enzotib Nov 5 '11 at 21:47
  • I see. I actually looked at the man pages to double check and interpreted "search" to mean "list files", but it seems it only allows you to pass through if you already know the name of a folder inside. Poor naming, I think. (incidentally, I found that if you have r but not x, you can list the files but not see their permissions.) – Kevin Nov 5 '11 at 22:02
sudo chown -R username:root "/directory you want"

-R represent Recursion for all it's sub directory's and files.

username- current user

It helpful for read/write/execute permission

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.