title: linux directory permission check and/or dir exist or not
body: <p>I have a script which is running as a ROOT on linux collecting data from different users.  Given a nfs path for each  individual user 1) verify the director does not exist 2) verify permission denied</p>
  <pre><code>verify_model_path_not_found = '/usr/bin/su ' + userID + ' -c \'/usr/bin/ls ' +  u_model_path + '  | /usr/bin/grep \&quot;Permission denied\|No such file or directory\&quot;\''
  perm_denied_str = 'Permission denied'
  no_file_dir_str =  'No such file or directory'
  
  print(&quot;verify_model_path_not_found:&quot;, verify_model_path_not_found)
  
  #Run as a root
  try:
      verify_cmd_out = subprocess.check_output(verify_model_path_not_found, shell=True) 
      verify_cmd_out = str(verify_cmd_out)
      print(&quot;verify_cmd_out:&quot;, verify_cmd_out, &quot;\n&quot;)
  
  except subprocess.CalledProcessError as errorcatch:
      print(datetime.datetime.now(), &quot; Error while executing &quot;, verify_model_path_not_found)
      print(&quot;error code&quot;, errorcatch.returncode, errorcatch.output, &quot;\n&quot;)
      continue 
  
  #only add items that are not accessible (perm denied or no file found)
  if  ((perm_denied_str in verify_cmd_out) or (no_file_dir_str in verify_cmd_out)):
      #remaining actions .... send an email to the user ...
  </code></pre>
  <p>Example output Error:</p>
  <pre><code>verify_model_path_not_found: /usr/bin/su xxxx  -c '/usr/bin/ls /nfs/x/y/z  | /usr/bin/grep &quot;Permission denied\|No such file or directory&quot;'
  2021-08-10 17:00:31.827186  Error while executing  /usr/bin/su xxxx -c '/usr/bin/ls /nfs/x/y/z | /usr/bin/grep &quot;Permission denied\|No such file or directory&quot;'
  error code 1 b''  #I know this dir does not exist or perm denied - still getting error 
  </code></pre>
  <p>given /nfs/x/y/z, if the user does not have a read access, I would like to get &quot;Permission denied&quot; using grep - &quot;Permission denied&quot; should be the value of verify_cmd_out</p>
  <p>given /nfs/x/y/z, if the dir does not exist, I would like to get &quot;No such file or directory&quot; using grep - &quot;No such file or directory&quot; should be the value of verify_cmd_out</p>
  <p>once perm denied or no such file is confirmed for the user,  certain actions need to place.<br />
  The /usr/bin/su xxxx  -c ... command is not properly working, any thought or idea how to resolve the issue?</p>