or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

file-operations.mdindex.mdlogic-math.mdmisc-utilities.mdpath-utilities.mdsystem-utilities.mdtext-processing.md
tile.json

path-utilities.mddocs/

Path Utilities

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.

Capabilities

Path Component Extraction

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
 */

Link Resolution

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
 */

Path Resolution

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
 */

Path Validation

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
 */

Usage Examples

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 runtime

Common 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/to

Link 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 links

Path 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 exist

Path 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

Practical Use Cases

Script Path Processing

#!/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"

File Extension Handling

# Remove file extension
filename="document.pdf"
base=$(basename "$filename" .pdf)     # Returns: document

# Get file extension
extension="${filename##*.}"           # Returns: pdf

Path Normalization

# 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

Symbolic Link Management

# 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"
fi

Return Values and Exit Codes

basename and dirname

  • Success: Exit code 0, outputs path component
  • Error: Exit code 1, typically due to invalid arguments

readlink

  • Success: Exit code 0, outputs link target
  • Not a link: Exit code 1
  • Broken link: Exit code 1 (unless following chain)

realpath

  • Success: Exit code 0, outputs canonical path
  • Path not found: Exit code 1 (unless -m used)
  • Permission denied: Exit code 1

pathchk

  • Valid path: Exit code 0
  • Invalid path: Exit code 1
  • Path too long: Exit code 1
  • Non-portable characters: Exit code 1 (with -p)

WebAssembly Environment Notes

  • All path operations work within the WASM virtual file system
  • Absolute paths are relative to the WASM root filesystem
  • Symbolic link support depends on WASM runtime capabilities
  • Path validation follows POSIX standards within WASM constraints
  • Cross-platform path handling works consistently across WASM environments

Integration Examples

These 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