0

I attempting to run a command on all subdirectories in a directory using find and -exec, however on one of the directories, the user the script runs under does not have adequate permissions and I get an error (permission denied). I am attempting to ignore the directory using either ! -path or using -prune. Neither of these methods work. I have tried both of the commands down below.

I have tried every combination of subDirToExclude— with and without ./ at the beginning, with and without /* at the end. I've tried relative path, full path and every single combination of all of them that you can think of to try and match this path, but it simply does not work. The man page is unhelpful and no suggestions from any related questions on this forum produce any useful results. Why do none of the methods suggested in the man page work? How can this actually be done?

find /path/to/dir -maxdepth 1 -type d ! -path "subDirToExclude" -exec somecommand {} +
find /path/to/dir -maxdepth 1 -type d -path "subDirToExclude" -prune -o -exec somecommand {} +
find: ‘/path/to/dir/subDirToExclude’: Permission denied
Cyrus
  • 84,225
  • 14
  • 89
  • 153
arowland
  • 1
  • 1
  • `-name "subDirToExclude" -prune -o` would have worked too. – Charles Duffy Aug 15 '19 at 21:06
  • From `man path`: *"Note that the pattern match test applies to the whole file name, starting from one of the start points named on the command line. It would only make sense to use an absolute path name here if the relevant start point is also an absolute path. This means that this command will never match anything: `find bar -path /foo/bar/myfile -print`"* – 0x5453 Aug 15 '19 at 21:07
  • BTW, what I would suggest is to test your logic by breaking it down into smaller parts. If `find /path/to/dir -path "subDirToExclude" -print` doesn't print anything, then you know that that `-path "subDirToExclude"` won't find anything to *exclude* it either. – Charles Duffy Aug 15 '19 at 21:08

1 Answers1

1

The argument to the -path option should be a full pathname, not just the name of the directory. Use -name if you just want to match the name of the directory.

find /path/to/dir -maxdepth 1 -type d ! -name "subDirToExclude" -exec somecommand {} +

You could also do this without using find at all, since you're not recursing into subdirectories because of -maxdepth 1.

shopt -s extglob
somecommand /path/to/dir /path/to/dir/!(subDirToExclude)/

Putting / at the end of the filename makes the wildcard only match directories. Actually, this will also match symbolic links to directories; if that's a problem, you can't use this solution.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I have already tried find /path/to/dir -maxdepth 1 -type d ! -path "/path/to/dir/subDirToExclude" -exec somecommand {} + as well as find /path/to/dir -maxdepth 1 -type d -path "/path/to/dir/subDirToExclude" -prune -o -exec somecommand {} + neither of these commands work. I have tried every combination I can think of. Can you please show how you would do the command? – arowland Aug 15 '19 at 21:28
  • I have also tried find /path/to/dir -maxdepth 1 -type d ! -name "subDirToExclude" -exec somecommand {} + as you have above and it does not work either – arowland Aug 15 '19 at 21:29
  • Why would I still be getting permission denied on that very directory? – arowland Aug 15 '19 at 21:32
  • Are you getting the error from `find` or from `someCommand`? If you use `echo` as the command, do you see the directory name in the output? – Barmar Aug 15 '19 at 21:34
  • I removed all permissions from the directory I excluded, I didn't get any error. – Barmar Aug 15 '19 at 21:35
  • I am using this exact command format above, with the only difference being the actual path names, and I still get the error find: ‘/path/to/dir/subDirToExclude’: Permission denied Why would this happen? Does anyone have any actual ideas? It's inexplicable – arowland Aug 15 '19 at 21:39
  • That error should only happen if it needs to recurse into the directory. But `-maxdepth 1` means it doesn't need to go into the directory. I can't explain it. – Barmar Aug 15 '19 at 21:41
  • What operating system? I tested in Mac OS and Debian. – Barmar Aug 15 '19 at 21:43
  • I'm on Ubuntu 18.04.3 LTS – arowland Aug 15 '19 at 21:48
  • Also, if I use echo as the command, it prints the directory path and gives the warning – arowland Aug 15 '19 at 21:53