4

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

1 Answer 1

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? May 9, 2014 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, 2014 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. May 27, 2020 at 17:44

You must log in to answer this question.

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