How can I use two bash commands in -exec of find command?

As for the find command, you can also just add more -exec commands in a row:

find . -name "*" -exec chgrp -v new_group '{}' \; -exec chmod -v 770 '{}' \;

Note that this command is, in its result, equivalent of using

chgrp -v new_group file && chmod -v 770 file

on each file.

All the find's parameters such as -name, -exec, -size and so on, are actually tests: find will continue to run them one by one as long as the entire chain so far has evaluated to true. So each consecutive -exec command is executed only if the previous ones returned true (i.e. 0 exit status of the commands). But find also understands logic operators such as or (-o) and not (!). Therefore, to use a chain of -exec tests regardless of the previous results, one would need to use something like this:

find . -name "*" \( -exec chgrp -v new_group {} \; -o -true \) -exec chmod -v 770 {} \; 

find . -name "*" -exec sh -c 'chgrp -v new_group "$0" ; chmod -v 770 "$0"' {} \;

Your command is first parsed by the shell into two commands separated by a ;, which is equivalent to a newline:

find . -name "*" -exec chgrp -v new_group {}
chmod -v 770 {} \;

If you want to run a shell command, invoke a shell explicitly with bash -c (or sh -c if you don't care that the shell is specifically bash):

find . -name "*" -exec sh -c 'chgrp -v new_group "$0"; chmod -v 770 "$0"' {} \;

Note the use of {} as an argument to the shell; it's the zeroth argument (which is normally the name of the shell or script, but this doesn't matter here), hence referenced as "$0".

You can pass multiple file names to the shell at a time and make the shell iterate through them, it'll be faster. Here I pass _ as the script name and the following arguments are file names, which for x (a shortcut for for x in "$@") iterates over.

find . -name "*" -exec sh -c 'for x; do chgrp -v new_group "$x"; chmod -v 770 "$x"; done' _ {} +

Note that since bash 4, or in zsh, you don't need find at all here. In bash, run shopt -s globstar (put it in your ~/.bashrc) to activate **/ standing for a recursive directory glob. (In zsh, this is active all the time.) Then

chgrp -v new_group -- **/*; chmod -v 770 -- **/*

or if you want the files to be iterated on in order

for x in **/*; do
  chgrp -v new_group -- "$x"
  chmod -v 770 -- "$x"
done

One difference with the find command is that the shell ignores dot files (files whose name begins with a .). To include them, in bash, first set GLOBIGNORE=.:..; in zsh, use **/*(D) as the glob pattern.

Tags:

Bash

Find