83

Please someone assist me with chmod, I have the following file structure

-dir1
--file1
--file2
--dir1a
---file1a1
---file1a2
--dir1b
---file1b1
---file1b2

How do I chmod 655 to all files under dir1 and all subdir? So all files will have 655 permission and all dir will remain as they were.

Thank you

2 Answers 2

159

Better to use

find . -type f -exec chmod 655 -- {} +

The other proposed solution from @sagarchalise will not work if filenames contain spaces or start with a dash.

5
  • 21
    Relatedly, to chmod all directories you can use find . -type d -exec chmod 755 {} + to change all directories to 755. Aug 24, 2011 at 13:02
  • 4
    What is the + for?
    – Brettski
    Jan 27, 2017 at 22:14
  • 6
    @Brettski: the + sign is expanded by find to the list of the file paths (or part of it, if too long).
    – enzotib
    Jan 28, 2017 at 7:44
  • 4
    What is the -- for? Dec 4, 2017 at 11:32
  • 4
    The -- indicates the end of command line options. This prevents a file starting with a hyphen from being interpreted as a command line option as it would come after the --. Mar 1, 2019 at 20:43
11

I think going inside dir1 and

find . -type f | xargs chmod 655

will do the trick.

Or this version, which supports file names with spaces:

find . -type f -print0 | xargs -0 chmod 655

4
  • this works too but i've got some warning with "No such file or directory"
    – netic
    Aug 27, 2011 at 2:39
  • @manetic: it depends, as said in my answer, on filenames containing spaces.
    – enzotib
    Aug 27, 2011 at 5:51
  • 4
    For completeness, changing it to find . -type f -print0 | xargs -0 chmod 655 would fix the space issue I believe, although @enzotib's answer is more concise Aug 20, 2015 at 10:22
  • what matt freake said Apr 4, 2017 at 10:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .