Pls help with this "find" command line

Pls help with this "find" command line

am 28.12.2007 17:39:19 von Patrice

I somehow messed up ( as root - my bad ) an important directory
and made many subdirectories _not_ executable.

I wish to change these directories to executable by user so I can
enter them.

I've worked out the following find command line.
Will it work to do what I want?

find . -type d -execdir chmod u+x '{}' \;

Questions:
1. Is there an easy way to test this before I run it on the important
directory? I can't see any way to test when running chmod.

2. Is there a way to first test whether the directory is not
chmod u+x
and only then mess with the directories?

3. Is -execdir necessary here or could I use -exec?

Thank you.
Larry
--
My real sig is much better.

Re: Pls help with this "find" command line

am 28.12.2007 18:17:07 von Icarus Sparry

On Fri, 28 Dec 2007 10:39:19 -0600, larryalk wrote:

> I somehow messed up ( as root - my bad ) an important directory and made
> many subdirectories _not_ executable.
>
> I wish to change these directories to executable by user so I can enter
> them.
>
> I've worked out the following find command line. Will it work to do what
> I want?
>
> find . -type d -execdir chmod u+x '{}' \;
>
> Questions:
> 1. Is there an easy way to test this before I run it on the important
> directory? I can't see any way to test when running chmod.

Make a test set of directories somewhere (/tmp if you can not think of
anywhere else), with various permissions, then run the command and see if
they have changed.

> 2. Is there a way to first test whether the directory is not chmod u+x
> and only then mess with the directories?

Yes. Essentially you want to have a shell script to do the work. This can
either be inserted inline by using a construct such as
find .... -exec sh -c "do something or other" '{}' '{}' ';'
or putting the script in say ~/FIXUP and running
find .... -exec ~/FIXUP '{}' ';'
However you almost certainly don't need to do this. "chmod u+x" will
probably not change a directory if it already has 'x' permission for
'user'.

> 3. Is -execdir necessary here or could I use -exec?

You can use '-exec' here, which is more standard.

> Thank you.
> Larry

Re: Pls help with this "find" command line

am 28.12.2007 20:01:49 von James Michael Fultz

* larryalk :
> I somehow messed up ( as root - my bad ) an important directory
> and made many subdirectories _not_ executable.
>
> I wish to change these directories to executable by user so I can
> enter them.
>
> I've worked out the following find command line.
> Will it work to do what I want?
>
> find . -type d -execdir chmod u+x '{}' \;
>
> Questions:
> 1. Is there an easy way to test this before I run it on the important
> directory? I can't see any way to test when running chmod.

Use the '-print' command in place of '-execdir ...' to see what
directories would be acted upon.

> 2. Is there a way to first test whether the directory is not
> chmod u+x
> and only then mess with the directories?

See find's '-perm' option, for instance:

find . -type d ! -perm -100 -execdir chmod u+x {} \;

> 3. Is -execdir necessary here or could I use -exec?

It is not necessary but is considered a more secure alternative to '-exec'.
On the other hand, '-exec' is more portable than '-execdir'.

--
James Michael Fultz
Remove this part when replying ^^^^^^^^

Re: Pls help with this "find" command line

am 31.12.2007 07:56:05 von Kiri

On Dec 28, 6:39 pm, larryalk wrote:
> I somehow messed up ( as root - my bad ) an important directory
> and made many subdirectories _not_ executable.
>
> I wish to change these directories to executable by user so I can
> enter them.
>
> I've worked out the following find command line.
> Will it work to do what I want?
>
> find . -type d -execdir chmod u+x '{}' \;

That should do it.

>
> Questions:
> 1. Is there an easy way to test this before I run it on the important
> directory? I can't see any way to test when running chmod.

Yes, create a test directory tree like below:

cd /tmp
mkdir test
mkdir -p test/1/2/3
mkdir -p test/1/5/7/3
mkdir -p test/1/a/b/2/3

# do a normal ls and see the directory permissions
# then do something like:

find . -type d -execdir chmod 770 '{}' \;

# If you check permissions on all those directories we created you
will see something like:

$ ls -al
total 6
drwxrwx--- 3 user user 72 Dec 31 08:27 .
drwx------ 63 user user 6224 Dec 31 08:31 ..
drwxrwx--- 4 user user 96 Dec 31 08:27 1

# do check the other subdirectories as well.

# Then try to change permissions as follows:

find . -type d -execdir chmod 755 '{}' \;

# ...checking the permissions again, you will see something like:

$ ls -al
total 6
drwxr-xr-x 3 user user 72 Dec 31 08:27 .
drwx------ 63 user user 6224 Dec 31 08:31 ..
drwxr-xr-x 4 user user 96 Dec 31 08:27 1

>
> 2. Is there a way to first test whether the directory is not
> chmod u+x
> and only then mess with the directories?

Yes, you might need to write a shell script here.

>
> 3. Is -execdir necessary here or could I use -exec?

There are security concerns with using -exec. Check the manual pages,
but I guess you can use both.

Re: Pls help with this "find" command line

am 31.12.2007 14:24:25 von Sven Mascheck

larryalk wrote:
> [...] made many subdirectories _not_ executable.
> [...]
> find . -type d -execdir chmod u+x '{}' \;

Interestingly, chmod implements something similar
with the capital X flag,

chmod -R u+X .