Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Mar 2005 21:53:02 +0200
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        Eric McCoy <emccoy@haystacks.org>, Fafa Diliha Romanova <fteg@london.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: chmod equivalent to find commands
Message-ID:  <20050312195302.GA77874@gothmog.gr>
In-Reply-To: <42330B26.2040508@haystacks.org>
References:  <20050312115359.C14EF4BE6D@ws1-1.us4.outblaze.com> <42330B26.2040508@haystacks.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2005-03-12 10:30, Eric McCoy <emccoy@haystacks.org> wrote:
>Fafa Diliha Romanova wrote:
>> hello.
>>
>> i know there's an equivalent to these two find commands that
>> can be summed up in one chmod command:
>>
>> find . -type d -exec chmod 755 {} \;
>> find . -type f -exec chmod 644 {} \;

Uhm, why?  Even if that were possible, isn't clarity more important that
stuffing as many actions as possible in one line?

What you list above is similar to the way I use for changing the
permissions of files/dirs and it works all the time.

There's no reason to try to write one, long, complicated command just
for the sake of making it one command instead of two.  Otherwise, you
may as well do more complex stuff like:

	find . | while read line; do
		mode=''
		[ -d "${line}" ] && mode=0755
		[ -f "${line}" ] && mode=0644

		[ -n "${mode}" ] && echo "chmod ${mode} \"${line}\""
	done | sh

But this is getting quickly very difficult to remember easily and repeat
consistently every time you want to do something similar :)

>> what would be the best solution here?
>
> I would do it the same way you do, but with xargs instead:
>
> find . -type X -print0 | xargs -0 chmod XXX

This is an excellent way to do this, IMHO.

> If you were feeling crazy and use sh:
>
> find . | while read path; do \
>   if [ -d "$path" ]; then chmod 755;
>   else chmod 644; fi; \
> done

I guess you meant to write:

    find . | while read path; do \
      if [ -d "$path" ]; then chmod 755 "${path}";
      else chmod 644 "${path}"; fi; \
    done

Otherwise, many chmod failures are the only result.

But this has a minor buglet.  It will change everything that is not a
directory to mode 0644.  This mode is ok for files, but it may not be ok
(or it may even fail) for other stuff (symbolic links, for instance).

- Giorgos



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050312195302.GA77874>