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 {} \;
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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 {} \;
In this very case, you can use chmod -R
as per user26112'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 (like {\}
or {"}"
) so that the inner {}
is not expanded by the outer find
(accounting for those implementations of find
that still expand {}
even when it's only part of an argument).
GNU find
4.9.0 or newer also supports getting the list of files to work on from stdin as NUL delimited records, so you can do:
find -files0-from <(
find ~/.config/google-chrome -type d -name test -print0
) -exec chmod 555 {} +
With older versions, you can do something similar using GNU xargs
and sh
to reorder the arguments:
xargs -r0a <(
find ~/.config/google-chrome -type d -name test -print0
) sh -c 'exec find "$@" -exec chmod 555 {} +' sh
(<(...)
is process substitution, a ksh feature also supported by zsh and bash, not to be confused with yash
's process redirection).
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 '{}' \;
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.
{}
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
.
Jul 25, 2013 at 8:23
$0
parameters.. Escaping the internal closing }
curly brace is very tricky and smart) didn't notice that.
+
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.
Jul 25, 2013 at 12:20
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 '{}' +