3

I want to apply 644 permissions only for all files in current directory, and 755 permissions only for subdirectories in current directory with chmod

7

Use find:

find ./ -type f -maxdepth 1 -exec chmod 644 {} \;
find ./ -type d -maxdepth 1 -exec chmod 755 {} \;
  • -type f: Files
  • -type d: Directories
  • -maxdepth 1: first level (to avoid default recursive behavior)
  • -exec: execute command on with argument from result
  • {} will be replaced with a line from results. try:

    find ./ -type d -maxdepth 1 -exec echo hi{}low \;
    

    This useful for commands which have different arguments order like ln:

    ln -s {} ./otherfolder/{}
    

    or cp

    cp {} ./otherfolder/
    
  • \; to to tell -exec is the end of command because you can add other find options after -exec (so they will not mix up). try:

    find ./ -type d -maxdepth 1 -exec echo
    

Reference: man find

3
  • What is mean {} \;? It's really necessary? – Victor Bocharsky May 9 '14 at 11:03
  • 1
    @brainforce. yep, {} will be replaced with a line from results and \; to to tell -exec is the end of command because you can add other find options after -exec. try find ./ -type d -maxdepth 1 -exec echo and this find ./ -type d -maxdepth 1 -exec echo hi{}low \; – user.dz May 9 '14 at 11:12
  • For me, find warns about putting -maxdepth after other arguments. To silence the warning, do find ./ -maxdepth 1 -type d -exec chmod 755 {} \; instead. – Michael Dorst May 27 '20 at 17:44

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.