Linux find: cannot stat current directory: Permission denied

RegProctor
RegProctor used Ask the Experts™
on
OS: openSUSE 11.2

As you can see from the script below this find command with sudo in front of it won't work when in a subdir of the root user.  The line itself is inside a shell script (run as root user) however the result is the same whether on the command line or in the script file.

The error seems self-explanatory to me, that is, it can't "stat" the current directory. I assume "stat" means read the attributes or something similar about the directory, which makes sense. However, as you can see from the line there is no reason for it to read the current directory as it's being directed to read an entirely different directory.

My first question is, is this normal or is this a bug? To me it looks like a bug but could just be something required because of security issues of some sort.

My second question is, if it is not a bug, is their some sort of cool command line switch to make the error go away or is just "cd'ing" first to the directory I'm going to do the find on the best approach?
 
neutrino:/data/archive # sudo -u reg find "/data/archive/" -maxdepth 1 -type f
/data/archive/drupal-6.9.tar.gz
/data/archive/drupal-6.11.tar.gz
/data/archive/drupal-6.10.tar.gz
/data/archive/drupal-6.13.tar.gz
/data/archive/drupal-5.2.tar.gz
/data/archive/drupal-5.1.tar.gz

neutrino:/data/archive # cd ~/scripts/drupal/

(we are root user)
neutrino:~/scripts/drupal # sudo -u reg find "/data/archive/" -maxdepth 1 -type f
find: cannot stat current directory: Permission denied

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
This is most likely caused by sudo.

The following sudoers attribute, means the default behaviour is not to set the HOME environment of the target user.

       always_set_home
                   If set, sudo will set the HOME environment variable to the home directory of the target user (which is root unless the -u option is used).  This effectively means that the -H flag is always implied.  This flag is off by default.

This means when you do

sudo -u reg command

as the root user, $HOME is left set as the root's home dir.

You could either do

sudo -Hu reg .....

or

sudo -u reg ... 2>/dev/null


Also, why are you using sudo if you are invoking it from root?  You could just do

su reg -c "find ....."




That's beacuse you run the find as user reg and that one probably doesn't have access to current directory.

For information about stat, see command stat, it finds out information about a file/directory and that's what find does, but the purpose I don't know, maybe anyone else would know.
Actually the reason is that find changes it's current directory into a directory it's being searched and when it's finished, it wants to get back to the current working directory, but there it doesn't have privileges, so that's why the error. I guess. I was just thinking about that.

On my fedora linux it says: find: failed to restore initial working directory: Permission denied

And that's more informative.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Kerem ERSOYPresident

Commented:
Hi,

You do sudo as user reg. Did you go and add find to /etc/sudoees file ? So that the user has permsison to run find without needing a password such as:

reg               ALL=(ALL)   NOPASSWD: /usr/bin/find

You might also want to comment out :

Defaults requiretty

if you are using the commands from scripts very often.

It has nothing to do with a bug. It is a permission issue as the error indicates. Since it is not a bug there's no need to respond to the second q.

Cheers,
K.

Author

Commented:
Thanks everyone.

Regarding your comments, starting from the top:
Tintin:
Re: "always_set_home" - interesting, "sudo -Hu reg ..." it didn't help but it's good to know about thanks.
Re: 'su reg -c "find ....."' - I just learned something new.
It's not sudo, it's find for sure, but thanks.

DalHorinek:
You were certainly on the right track but I tracked down exactly what it was by using the -D find option. It fails in the optimzer because the optimizer is doing some test(s) relative the the current directory as part of it's optimizing algorithm.

Since find is so flexible I think the solution is to use the -user command in find and call it as root. However, doing that I'll then have to see if the su or sudo will work on the -exec command which I didn't include above but it's just a "mv" command. Sometimes however it is a command that can change the user (like cp) which is why I use sudo, otherwise I would just do everything as root.

KeremE:
As you can guess it's not really in the alias, I have had that setup for a long time now.

Author

Commented:
It wasn't the exact answer but it gave me an idea to find the answer, and that is enough.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial