Skip to content
Linux Basics: Check Portable Path Names with GNU pathchk

Linux Basics: Check Portable Path Names with GNU pathchk

October 27, 2026

An export format may need conservative filenames even when the current filesystem accepts more characters. GNU pathchk checks path validity and optional portability constraints. It validates names without creating the named files.

Prepare a scratch directory

Tested on Linux with GNU coreutils 9.7 and GNU Bash 5.2.37. You need the named runtime, Bash, a text editor, and the standard mktemp, rm, and rmdir utilities. The script creates a separate temporary fixture directory and removes only that directory on exit. It does not operate on your existing files.

Keep using the same terminal. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-gnu-pathchk.sh:

#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
pathchk -p -- reports/run_01.txt
test ! -e reports
printf 'Portable relative path accepted without creation\n'
if pathchk -p -- 'report name.txt' 2> diagnostic.txt; then
  exit 1
else
  printf 'Space outside portable filename set: rejected\n'
fi
test -s diagnostic.txt
if pathchk -P -- '-option-like' 2> diagnostic.txt; then
  exit 1
else
  printf 'Leading hyphen under -P: rejected\n'
fi
pathchk -- 'report name.txt'
printf 'Same spaced name accepted by ordinary local check\n'
test ! -e 'report name.txt'

Run it:

bash demonstrate-gnu-pathchk.sh

Observed output:

Portable relative path accepted without creation
Space outside portable filename set: rejected
Leading hyphen under -P: rejected
Same spaced name accepted by ordinary local check

Read the result

-p applies conservative POSIX portability checks, including the portable filename character set. That rejects the space even though this Linux filesystem accepts it. -P adds checks for empty names and leading hyphens in components. -- separates options from the path being checked, so the hyphenated fixture is passed as data.

Apply it carefully

A successful check does not create the path, reserve its name, establish permissions, or guarantee that a later open will succeed. Filesystem state can change between checking and use. Portability limits are intentionally stricter than many modern filesystems, so apply them only when the target interchange policy needs them. This is not a path-containment or symlink-security check; it does not establish that a path stays below a trusted directory.

The official reference describes the API and its options. The checks above cover the specific fixture, not every possible input or environment.

Clean up

Remove the source and then the empty practice directory:

rm -- demonstrate-gnu-pathchk.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect the remaining files rather than deleting unknown contents.

Acknowledgements

This post was created with assistance from AI.

Last updated on