3

I want to execute chmod 555 on all files and directories I get with the following command (it returns the directory test and all files and directories in it):

find ~/.config/google-chrome -type d -name test -exec find {} \;
5

In this very case, you can use chmod -R as per Evan's answer, but in the general case, you can do:

find ~/.config/google-chrome -type d -name test -exec sh -c '
  for i do
    find "$i" -exec chmod 555 {\} +
  done' sh {} +

The trick being to use a shell and use quoting so that the inner {} is not expanded by the outer find.

3

You can use chmod's -R flag to change the permissions of the directory and all of the files and directories in the hierarchy beneath it.

find ~/.config/google-chrome -type d -name test -exec chmod -R 555 '{}' \;
  • 1
    -R is POSIX so should be supported everywhere. – Stéphane Chazelas Jul 24 '13 at 14:46
  • @StephaneChazelas: Thanks. I didn't know that. – user26112 Jul 24 '13 at 14:50
2

If you really want two find -exec, try the following

find /etc -type f -exec sh -c 'find "$0" -exec echo {\} \;' {} \;

(that will just pass to echo command what find did find)

find /etc -type d -exec sh -c 'find "$0" -type f -exec echo "$0" {\} \;' {} \;

(some useful example: prints dir name and list of files in it)

With using sh -c you can also resolve issue with pipes in the find -exec clause, also notice that passed arguments to sh -c will start from $0 not from $1 as usually do with shell script.

  • 1
    That doesn't work with the GNU find (as usually found on Linux based systems as the OP mentions he's using) and some other find implementations, because those expand {} even when they're not on their own as a single argument. So in your case, the first find would expand both {}. You forgot the quotes around $0. – Stéphane Chazelas Jul 25 '13 at 8:23
  • @Stephane Chazelas great, you're right! That is why I sometimes had to doublequote $0 parameters.. Escaping the internal closing } curly brace is very tricky and smart) didn't notice that. – rook Jul 25 '13 at 9:21
  • 1
    Note that you can avoid running one shell per directory, by using + instead of \; and a loop in the shell. I prefer not using $0 there because $0 is used for instance by the shell to display error messages which can cause confusion. Hence the extra sh argument to set $0 in the solution I gave. – Stéphane Chazelas Jul 25 '13 at 12:20
0

You can also run this command. It will run on each directory, but the + argument to find will run the chmod with multiple "test" directories passed as arguments:

$ find ~/.config/google-chrome -type d -name test -exec chmod -R 555 '{}' +

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.