Additional utility programs for various specialized tasks including process control, file searching, data processing, and output management. These utilities provide essential functionality for system administration and data manipulation tasks.
Control timing and execution flow.
/**
* Delay execution for specified time
* WebAssembly binary: dist/wasm/bin/sleep
*
* Usage patterns:
* - sleep number[suffix]
* - Suffixes: s (seconds), m (minutes), h (hours), d (days)
* - Default unit is seconds
* - Pauses execution for the specified duration
*/Create temporary files and directories securely.
/**
* Create temporary files and directories
* WebAssembly binary: dist/wasm/bin/mktemp
*
* Usage patterns:
* - mktemp [options] [template]
* - Supports -d (directory), -p (directory prefix), -t (template)
* - Creates secure temporary files with unique names
* - Default template: tmp.XXXXXXXXXX
*/Execute commands with arguments from standard input.
/**
* Execute commands with arguments from input
* WebAssembly binary: dist/wasm/bin/xargs
*
* Usage patterns:
* - xargs [options] [command [initial-arguments]]
* - Supports -n (max args), -I (replace string), -0 (null delimiter)
* - Builds command lines from standard input
* - Handles argument limits and parallel execution
*/Generate continuous output streams.
/**
* Repeatedly output strings
* WebAssembly binary: dist/wasm/bin/yes
*
* Usage patterns:
* - yes [string]
* - Default outputs "y" repeatedly
* - Outputs specified string infinitely until terminated
* - Useful for automating interactive prompts
*/Format files for printing with pagination and headers.
/**
* Format files for printing
* WebAssembly binary: dist/wasm/bin/pr
*
* Usage patterns:
* - pr [options] [file...]
* - Supports -n (line numbers), -h (header), -l (page length)
* - Creates paginated output with headers and page breaks
* - Multiple column formatting options
*/Write output to multiple destinations simultaneously.
/**
* Write output to multiple destinations
* WebAssembly binary: dist/wasm/bin/tee
*
* Usage patterns:
* - tee [options] [file...]
* - Supports -a (append), -i (ignore interrupts)
* - Copies input to both stdout and specified files
* - Essential for logging and debugging pipelines
*/Search for files and directories with complex criteria.
/**
* Search for files and directories
* WebAssembly binary: dist/wasm/bin/find
*
* Usage patterns:
* - find [path...] [expression]
* - Supports -name, -type, -size, -mtime, -exec actions
* - Complex search expressions with logical operators
* - Powerful file system traversal and filtering
*/Perform topological sort on input data.
/**
* Topological sort of input
* WebAssembly binary: dist/wasm/bin/tsort
*
* Usage patterns:
* - tsort [file]
* - Reads pairs of items and outputs topological ordering
* - Useful for dependency resolution and ordering
* - Detects cycles in dependency graphs
*/Process timing and control:
const { path } = require("@cowasm/coreutils");
const { join } = require("path");
// Access miscellaneous utility binaries
const binPath = join(path, "bin");
const sleepBinary = join(binPath, "sleep");
const mktempBinary = join(binPath, "mktemp");
const xargsBinary = join(binPath, "xargs");
// These would be executed via your WebAssembly runtimeSleep and timing:
# Basic delays
sleep 5 # Sleep for 5 seconds
sleep 2m # Sleep for 2 minutes
sleep 1h # Sleep for 1 hour
sleep 0.5 # Sleep for half a second
# In scripts
echo "Starting process..."
sleep 3
echo "Process started"Temporary file management:
# Create temporary file
temp_file=$(mktemp)
echo "Temporary file: $temp_file"
# Create temporary directory
temp_dir=$(mktemp -d)
echo "Temporary directory: $temp_dir"
# Custom template
temp_file=$(mktemp /tmp/myapp.XXXXXX)
# Secure temporary file in specific directory
temp_file=$(mktemp -p /var/tmp mydata.XXXXXX)Command execution with xargs:
# Basic usage
echo "file1 file2 file3" | xargs ls -l
# Process files one by one
find . -name "*.txt" | xargs -n 1 wc -l
# Replace string pattern
find . -name "*.bak" | xargs -I {} mv {} /backup/
# Handle filenames with spaces (null delimiter)
find . -name "*.txt" -print0 | xargs -0 grep "pattern"
# Parallel execution
find . -name "*.jpg" | xargs -P 4 -I {} convert {} {}.thumbnail.jpgContinuous output:
# Default "y" output
yes | head -5 # Outputs: y y y y y
# Custom string
yes "hello" | head -3 # Outputs: hello hello hello
# Automate interactive commands
yes | apt-get upgrade # Automatically answer "yes" to prompts
yes n | rm -i *.tmp # Automatically answer "no" to promptsOutput duplication with tee:
# Save output to file and display
command | tee output.log
# Append to file
command | tee -a logfile.txt
# Multiple files
command | tee file1.txt file2.txt
# Complex pipelines
command | tee intermediate.log | process | tee final.logFile searching with find:
# Basic searches
find . -name "*.txt" # Find all .txt files
find /path -type d # Find all directories
find . -size +100M # Find files larger than 100MB
# Time-based searches
find . -mtime -7 # Modified in last 7 days
find . -atime +30 # Accessed more than 30 days ago
# Execute commands on results
find . -name "*.log" -exec rm {} \;
find . -type f -exec chmod 644 {} \;
# Complex expressions
find . \( -name "*.jpg" -o -name "*.png" \) -and -size +1MFile formatting with pr:
# Basic formatting
pr filename.txt # Add page headers and numbers
# Custom headers
pr -h "My Document" file.txt
# Line numbering
pr -n file.txt
# Multiple columns
pr -2 file.txt # Two-column format
# Custom page length
pr -l 60 file.txt # 60 lines per pageTopological sorting:
# Dependency sorting
echo -e "a b\nb c\nc d" | tsort # Returns: a b c d
# Build order resolution
cat dependencies.txt | tsort # Resolves build dependencies
# Cycle detection
echo -e "a b\nb c\nc a" | tsort # Detects circular dependency# Process all files in parallel
find . -name "*.data" | xargs -P $(nproc) -I {} process_file.sh {}
# Create batch temporary files
for i in $(seq 1 10); do
temp_file=$(mktemp /tmp/batch.$i.XXXXXX)
echo "Processing batch $i in $temp_file"
done# Log all output while processing
complex_command 2>&1 | tee full.log | grep ERROR | tee errors.log
# Time-stamped logging
while true; do
date
system_status_command
sleep 60
done | tee -a system.log# Automated test responses
test_command() {
echo -e "option1\nyes\n3\nquit" | interactive_program
}
# Timeout with sleep
timeout_command() {
command &
local pid=$!
sleep 10
kill $pid 2>/dev/null
}# Clean up old temporary files
find /tmp -name "*.tmp" -mtime +7 -exec rm {} \;
# Archive old logs
find /var/log -name "*.log" -mtime +30 | xargs gzip
# Backup with temporary staging
backup_dir=$(mktemp -d)
find . -name "*.important" -exec cp {} "$backup_dir" \;
tar -czf backup.tar.gz -C "$backup_dir" .
rm -rf "$backup_dir"# Build order from Makefile dependencies
grep "^[^#].*:" Makefile | sed 's/:/ /' | tsort > build_order.txt
# Package dependency resolution
cat package_deps.txt | tsort | tac > install_order.txt-P for parallel execution when appropriate-n to limit arguments per command invocation-0 with find -print0 for filenames with spaces-type early in expressions for faster filtering-and and -or-prune to skip directories-a to avoid overwriting existing filesThese utilities provide essential functionality for system automation, file processing, and data management within the WebAssembly environment.