Path manipulation and resolution utilities for working with file and directory paths. These utilities provide essential path processing capabilities for file system navigation and path validation.
Extract filename and directory components from paths.
/**
* Extract filename from path
* WebAssembly binary: dist/wasm/bin/basename
*
* Usage patterns:
* - basename path [suffix]
* - basename [options] path...
* - Supports --suffix option, multiple path processing
* - Removes directory path and optional suffix
*//**
* Extract directory from path
* WebAssembly binary: dist/wasm/bin/dirname
*
* Usage patterns:
* - dirname path...
* - Extracts directory portion of file paths
* - Returns parent directory of given path
*/Resolve symbolic links and display link targets.
/**
* Display symbolic link targets
* WebAssembly binary: dist/wasm/bin/readlink
*
* Usage patterns:
* - readlink [options] file...
* - Supports -f (follow all links), -n (no newline)
* - Shows the target of symbolic links
*/Resolve absolute paths and canonicalize path names.
/**
* Resolve absolute paths
* WebAssembly binary: dist/wasm/bin/realpath
*
* Usage patterns:
* - realpath [options] path...
* - Supports -e (require existence), -m (allow missing)
* - Returns canonical absolute path names
*/Check path name validity and portability.
/**
* Check path validity
* WebAssembly binary: dist/wasm/bin/pathchk
*
* Usage patterns:
* - pathchk [options] path...
* - Supports -p (portability), -P (empty names and leading hyphens)
* - Validates path names for POSIX compliance
*/Path component extraction:
const { path } = require("@cowasm/coreutils");
const { join } = require("path");
// Access path utility binaries
const binPath = join(path, "bin");
const basenameBinary = join(binPath, "basename");
const dirnameBinary = join(binPath, "dirname");
// These would be executed via your WebAssembly runtimeCommon path operations:
# Extract filename
basename /path/to/file.txt # Returns: file.txt
basename /path/to/file.txt .txt # Returns: file
# Extract directory
dirname /path/to/file.txt # Returns: /path/to
dirname relative/path/file.txt # Returns: relative/path
# Handle multiple paths
basename -s .txt /path/file1.txt /path/file2.txt # Returns: file1, file2
dirname /path/to/file1 /path/to/file2 # Returns: /path/to, /path/toLink resolution:
# Show symbolic link target
readlink symlink_file # Shows target of symlink
# Follow all symbolic links
readlink -f symlink_chain # Resolves complete chain
# Multiple links
readlink link1 link2 link3 # Shows targets of multiple linksPath resolution:
# Get canonical absolute path
realpath relative/path # Returns: /absolute/canonical/path
realpath ../parent/file # Resolves .. references
# Allow missing components
realpath -m non/existent/path # Works even if path doesn't exist
# Require existence
realpath -e existing/path # Fails if path doesn't existPath validation:
# Check POSIX path validity
pathchk /valid/unix/path # Exit code 0 for valid paths
# Check portability
pathchk -p portable_filename # Checks for portable filename
# Check multiple paths
pathchk path1 path2 path3 # Validates multiple paths#!/bin/bash
# Get script directory and filename
SCRIPT_DIR=$(dirname "$0")
SCRIPT_NAME=$(basename "$0")
SCRIPT_PATH=$(realpath "$0")
echo "Script directory: $SCRIPT_DIR"
echo "Script name: $SCRIPT_NAME"
echo "Full path: $SCRIPT_PATH"# Remove file extension
filename="document.pdf"
base=$(basename "$filename" .pdf) # Returns: document
# Get file extension
extension="${filename##*.}" # Returns: pdf# Convert relative to absolute paths
relative_path="../data/file.txt"
absolute_path=$(realpath "$relative_path")
# Validate before processing
if pathchk "$user_input_path"; then
echo "Valid path: $user_input_path"
else
echo "Invalid path: $user_input_path"
fi# Check if file is a symbolic link and get target
if [ -L "$filename" ]; then
target=$(readlink "$filename")
echo "$filename is a link to: $target"
# Get final target (resolve chain)
final_target=$(readlink -f "$filename")
echo "Final target: $final_target"
fiThese utilities are commonly used together for robust path handling:
# Robust file processing
input_file="$1"
if pathchk "$input_file"; then
abs_path=$(realpath "$input_file")
dir_name=$(dirname "$abs_path")
base_name=$(basename "$abs_path")
echo "Processing: $base_name in $dir_name"
fi