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
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 otherfind
options after-exec
(so they will not mix up). try:find ./ -type d -maxdepth 1 -exec echo
Reference: man find
-
-
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 otherfind
options after-exec
. tryfind ./ -type d -maxdepth 1 -exec echo
and thisfind ./ -type d -maxdepth 1 -exec echo hi{}low \;
– user.dzMay 9, 2014 at 11:12 -
For me,
find
warns about putting-maxdepth
after other arguments. To silence the warning, dofind ./ -maxdepth 1 -type d -exec chmod 755 {} \;
instead. May 27, 2020 at 17:44