{"record":{"id":"5ab722da38761ef9","repo":"apache/hadoop","slug":"is-not-a-directory-5ab722","errorCode":null,"errorMessage":"Is not a directory","messagePattern":"Is not a directory","errorType":"exception","errorClass":"PathIsNotDirectoryException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Mkdir.java","lineNumber":65,"sourceCode":"    \"-p: Do not fail if the directory already exists\";\n\n  private boolean createParents;\n  \n  @Override\n  protected void processOptions(LinkedList<String> args) {\n    CommandFormat cf = new CommandFormat(1, Integer.MAX_VALUE, \"p\");\n    cf.parse(args);\n    createParents = cf.getOpt(\"p\");\n  }\n\n  @Override\n  protected void processPath(PathData item) throws IOException {\n    if (item.stat.isDirectory()) {\n      if (!createParents) {\n        throw new PathExistsException(item.toString());\n      }\n    } else {\n      throw new PathIsNotDirectoryException(item.toString());\n    }\n  }\n\n  @Override\n  protected void processNonexistentPath(PathData item) throws IOException {\n    if (!createParents) {\n      // check if parent exists. this is complicated because getParent(a/b/c/) returns a/b/c, but\n      // we want a/b\n      final Path itemPath = new Path(item.path.toString());\n      final Path itemParentPath = itemPath.getParent();\n\n      if(itemParentPath == null) {\n        throw new PathNotFoundException(String.format(\n            \"Item: %s parent's path is null. This can happen if mkdir is \" +\n                \"called on root, so there's no parent.\", itemPath.toString()));\n      }\n\n      if (!item.fs.exists(itemParentPath)) {","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Mkdir.java#L47-L83","documentation":"PathIsNotDirectoryException ('Is not a directory') thrown by Mkdir.processPath (Mkdir.java:65) when 'hadoop fs -mkdir' targets an existing path that is a regular FILE. mkdir can only create/reuse directories; a colliding file name is always an error, even with -p (the -p flag only relaxes the existing-DIRECTORY case, not a file collision).","triggerScenarios":"'hadoop fs -mkdir /data/file.txt' where file.txt is an existing file; a flat-file naming convention that later becomes a directory path (e.g. /topic/name as file, then /topic/name/part-0 expected); 'mkdir -p' on an existing file hits this same branch.","commonSituations":"Schema evolution in data lakes: a path first used as a file marker, later expected as a directory; scripts choosing output paths that collide with a _SUCCESS-style file; case-insensitive filesystems colliding with differently-cased files.","solutions":["Pick a different directory name that does not collide with the file","Move or delete the file if it is stale: 'hadoop fs -rm /data/file.txt' then mkdir","Audit the producer that created a file where your layout expects a directory and fix its path convention"],"exampleFix":"# before\nhadoop fs -mkdir -p /topic/events    # /topic/events already exists as a FILE -> Is not a directory\n\n# after\nhadoop fs -mv /topic/events /topic/events.bak\nhadoop fs -mkdir -p /topic/events","handlingStrategy":"validation","validationCode":"if (fs.exists(path)) {\n  FileStatus st = fs.getFileStatus(path);\n  if (!st.isDirectory()) {\n    throw new PathIsNotDirectoryException(path.toString());\n  }\n}","typeGuard":"static boolean canCreateDirectoryAt(FileSystem fs, Path p) throws IOException {\n  return !fs.exists(p) || fs.getFileStatus(p).isDirectory();\n}","tryCatchPattern":"try {\n  fs.mkdirs(path);\n} catch (PathIsNotDirectoryException e) {\n  // a FILE occupies the path: rename/remove it, or choose another name\n}","preventionTips":["Never rely on -p to bypass a file/dir name collision — it cannot","Keep marker files and directory paths in separate namespaces in lake layouts","Check exists()+isDirectory() before programmatic mkdirs"],"tags":["hadoop","mkdir","shell","path-not-directory","argument-validation"],"backgroundTag":"path-not-directory","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}