Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

pathchk - check pathnames

&pagelevel(4)&pagelevel

The pathchk command checks that one or more pathnames are valid (i.e. they can be used to access or create a file without causing syntax errors) and portable (i.e. the name need not be adjusted). More extensive portability checks can be carried out -p option.

By default, the pathchk command checks the components of all pathname arguments based on the underlying file system. An error message is output for the pathname argument if:

  • It is longer than the maximum permitted pathname length ({PATH_MAX} bytes).

  • It contains a component that is longer than the maximum permitted filename length ({NAME_MAX} bytes) in the relevant directory.

  • It contains a component in a directory that is not searchable.

  • It contains a component with characters that are invalid in the directory.

  • The name length of a file or directory in a bs2fs file system does not contradict the rules for a bs2fs file system.

pathchk does not consider it an error if one or more components of a pathname argument do not exist, as long as the file with the specified pathname can be created and does not violate any of the checks described above.


Syntax


pathchk[ -p] pathname...

option

-p

pathchk does not perform a check on the underlying file system, but on generic portability conditions. An error message relating to the pathname argument is output if:

  • It is longer than the maximum permitted length for portable pathnames ({_POSIX_PATH_MAX} bytes).

  • It contains a component longer than the maximum length for portable filenames ({_POSIX_NAME_MAX} bytes).

  • It contains a component with characters not contained in the portable character set for filename.

pathname

The pathnames to be checked.

Locale

The following environment variables affect the execution of pathchk:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Hint

Using the test command you can check whether a certain pathname specifies an existing file. However there is no information about whether a component of the pathname was truncated (in a directory where the {_POSIX_NO_TRUNC} function is not activated. The pathchk command does not check for the existence of a file. It only checks if a certain pathname exists or if it can be created without truncating the name. The noclobber option in the shell (compare with the description of set) can create a unique file.

Example

You can check if all pathnames in an imported data interchange archive are legitimate and unambiguous on the current system as follows:

pax -f archive | sed -e \*(hO/ == .*/s///\*(hO | xargs pathchk
if [ $? -eq 0 ]
then
    pax -r -f archive
else
    echo Investigate problems before importing files.
    exit 1
fi

You can check whether all files in the current directory hierarchy can be transferred to another system, that the general portability conditions are met, and that the pax command is available as follows:

find . -print | xargs pathchk -p
if [ $? -eq 0 ]
then
    pax -w -f archive .
else
    echo Portable archive cannot be created.
    exit 1
fi

You can check whether a specified pathname names a readable file and whether an application can create a file by extending the filename without truncating the pathname and without overwriting any existing files.

case $- in
    *C*)    reset="";;
    *)      reset="set +C"
            set -C;;
esac
test -r "$path" && pathchk "$path.out" &&
    rm "$path.out" > "$path.out"
if [ $? -ne 0 ]; then
    printf "%s: %s not found or %s.out fails \
creation checks.\n" $0 "$path" "$path"
    $reset  # reset the noclobber option in case a trap
            # on EXIT depends on it
    exit 1
fi
$reset
PROCESSING < "$path" > "$path.out"


This example is based on the following:

  1. PROCESSING displays the code used by the application in order to use $path once a check has been made that $path.out meets the required conditions.

  2. The state of the noclobber option is unknown if this code was called, and should be reset on exit to the state it was in when the code was called. (The reset variable is used to restore the initial state in this example.)

  3. Note the usage of the following construction:

    rm "$path.out" > "$path.out"

    1. The pathchk command has by now already checked that $path.out is not truncated.

    2. If the noclobber option is set, the shell checks that $path.out does not already exist before rm is called.

    3. If the shell has successfully created $path.out, rm removes it again so that the application can create the file in the PROCESSING step.

    4. If the PROCESSING step wants the file to exist already when it is called, rm "$path.out" > "$path.out" should be replaced by > "$path.out" 
      This confirms that the $path.out file does not exist yet, and it will be created for use by PROCESSING.

See also

test