Skip to main content
AllDevToolsHub
Back to all patterns

Unix/Linux File Path Validation

File & Path

Validates Unix absolute file paths like /usr/local/bin/node.

/^(\/[^\/\x00]+)+\/?$/

How it works

Unix absolute paths start with a forward slash. Each path segment can contain any character except forward slash and null byte (\x00). This pattern requires at least one path segment after the root slash.

Test Cases

Should Match

  • /usr/local/bin
  • /home/user/documents/file.txt
  • /var/log/nginx/access.log

Should NOT Match

  • relative/path
  • C:\Windows\System32
  • /path/withnull

Quick Summary

Validates Unix absolute file paths starting with /. Each segment can contain any character except / and null bytes. Does not validate path existence, use fs.access() for that.

Key Takeaways

Key Takeaways

  • Must start with / to be an absolute path
  • Segments can contain spaces, dots, hyphens, and most special characters
  • Null bytes (\x00) are the only truly forbidden character in Unix filenames
  • Trailing slash is optional, both /usr/bin and /usr/bin/ are valid
Use Cases

When to use it

  • Validating file path inputs in Linux/macOS applications
  • Checking configuration paths in Docker or Kubernetes manifests
  • Sanitizing user-provided paths in CLI tools
Watch out

Common Mistakes

  • Forgetting that Unix paths can contain spaces, don't split on spaces
  • Not checking for path traversal (../), validate and resolve paths with path.resolve() before use
FAQ

Unix/Linux File Path Validation, Frequently Asked

How do I prevent path traversal attacks?

After validating format, use path.resolve() to get the canonical path and check that it starts with your expected base directory.

Does this match relative paths?

No. Relative paths don't start with /. Use ^[^\/\x00][^\/\x00]*(\/[^\/\x00]+)*$ for relative paths.