Command to change permissions only on files not directories

19,053

Without xargs

find . -type f -exec chmod 644 {} \;

or with xargs

find . -type f -print0 | xargs -0 -I {} chmod 644 {}

the used xargs switches

  • -0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.

  • -I Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.

Explanation taken from here

Share:
19,053

Related videos on Youtube

Leo Simon
Author by

Leo Simon

Updated on September 18, 2022

Comments

  • Leo Simon
    Leo Simon over 1 year

    I have the following command

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

    which would successfully change to 644 the permissions on all files in ., provided that the filenames contained no embedded spaces. However it doesn't work in general.

    For example

    touch "hullo world"
    chmod 777 "hullo*"
    find . -type f -print0 | xargs -0 chmod 644 
    

    returns

    /bin/chmod: cannot access `./hello': No such file or directory
    /bin/chmod: cannot access `world': No such file or directory
    

    Is there a way to modify the command so that it can deal with files with embedded spaces?

    Thanks very much for any advice.

  • Leo Simon
    Leo Simon over 8 years
    Thanks very much everybody. As serg noticed something was indeed getting in the way, something incredibly stupid. Once moved out of the way, all of the above suggestions work fine.