File Operations
Commands for copying, moving, removing, and linking files and directories. All return PsBash.TextOutput objects when producing verbose output.
Copy files and directories.
cp [OPTION]... SOURCE... DEST| Flag | Description |
|---|---|
-r, -R | Copy directories recursively |
-v | Explain what is being done |
-n | Do not overwrite an existing file |
-f | Force: remove existing destination directory before copying |
Return type
Section titled “Return type”No output by default. With -v, returns PsBash.TextOutput showing each copy operation.
Examples
Section titled “Examples”Copy a single file:
cp config.json config.backup.jsonCopy a directory recursively with verbose output:
cp -rv src/ src-backup/'src/main.ps1' -> 'src-backup/main.ps1''src/util.ps1' -> 'src-backup/util.ps1'Copy without overwriting existing files:
cp -n defaults.json settings.jsonCopy multiple files into a directory:
cp file1.txt file2.txt dest/Behavior notes
Section titled “Behavior notes”- When
DESTis an existing directory, sources are copied into it. -nsilently skips files that already exist at the destination.-fremoves an existing destination directory before copying (useful for replacing a directory tree).-ris required for directories; without it,cpemits an error and skips.
Comparison with bash
Section titled “Comparison with bash”Identical syntax. The difference is that verbose output returns a PsBash.TextOutput object with a .BashText property instead of plain text.
Move or rename files and directories.
mv [OPTION]... SOURCE... DEST| Flag | Description |
|---|---|
-v | Explain what is being done |
-n | Do not overwrite an existing file |
-f | Force (do not prompt before overwriting) |
Return type
Section titled “Return type”No output by default. With -v, returns PsBash.TextOutput showing each move operation.
Examples
Section titled “Examples”Rename a file:
mv old-name.txt new-name.txtMove files into a directory with verbose output:
mv -v *.log archive/'error.log' -> 'archive/error.log''access.log' -> 'archive/access.log'Move without overwriting:
mv -n draft.md final.mdBehavior notes
Section titled “Behavior notes”- When
DESTis an existing directory, sources are moved into it. -nsilently skips when the destination already exists.- Works on both files and directories without needing a recursive flag.
Comparison with bash
Section titled “Comparison with bash”Identical syntax and semantics.
Remove files or directories.
rm [OPTION]... FILE...| Flag | Description |
|---|---|
-r, -R | Remove directories and their contents recursively |
-f | Ignore nonexistent files, never prompt |
-v | Explain what is being done |
Return type
Section titled “Return type”No output by default. With -v, returns PsBash.TextOutput listing each removed path.
Examples
Section titled “Examples”Remove a single file:
rm temp.txtRemove a directory tree with verbose output:
rm -rv build/removed 'build/output.js'removed 'build/output.css'removed 'build/'Silently remove files that may not exist:
rm -f maybe-missing.txtBehavior notes
Section titled “Behavior notes”- Without
-r, attempting to remove a directory produces an error. -fsuppresses errors for missing files and suppresses the “missing operand” error when called with no arguments.- With
-v, child items are listed before the parent directory.
Comparison with bash
Section titled “Comparison with bash”Identical flags. The safety guards against rm / and rm ~ match common GNU coreutils --preserve-root behavior, enabled by default.
Create directories.
mkdir [OPTION]... DIRECTORY...| Flag | Description |
|---|---|
-p | Create parent directories as needed, no error if existing |
-v | Print a message for each created directory |
Return type
Section titled “Return type”No output by default. With -v, returns PsBash.TextOutput for each directory created.
Examples
Section titled “Examples”Create a single directory:
mkdir outputCreate nested directories:
mkdir -p src/components/uiCreate with verbose output:
mkdir -v logsmkdir: created directory 'logs'Behavior notes
Section titled “Behavior notes”- Without
-p, creating a directory that already exists produces an error. - Without
-p, creating a directory whose parent does not exist produces an error. -psilently succeeds when the directory already exists.
Comparison with bash
Section titled “Comparison with bash”Identical syntax and semantics.
Remove empty directories.
rmdir [OPTION]... DIRECTORY...| Flag | Description |
|---|---|
-p | Remove directory and its empty ancestors |
-v | Print a message for each removed directory |
Return type
Section titled “Return type”No output by default. With -v, returns PsBash.TextOutput for each directory removed.
Examples
Section titled “Examples”Remove an empty directory:
rmdir old-outputRemove a chain of empty parent directories:
rmdir -pv a/b/crmdir: removing directory, 'a/b/c'rmdir: removing directory, 'a/b'rmdir: removing directory, 'a'Attempt to remove a non-empty directory (produces an error):
rmdir src# rmdir: failed to remove 'src': Directory not emptyBehavior notes
Section titled “Behavior notes”- Only removes empty directories. Use
rm -rfor directories with contents. -pwalks up the path removing each ancestor only while it is empty.- Errors on non-directory targets and missing paths.
Comparison with bash
Section titled “Comparison with bash”Identical syntax and semantics.
Create files or update timestamps.
touch [OPTION]... FILE...| Flag | Description |
|---|---|
-d | Use the specified date string instead of the current time |
Return type
Section titled “Return type”No output. Creates missing files as empty files and updates LastWriteTime and LastAccessTime on existing files.
Examples
Section titled “Examples”Create a new empty file:
touch notes.txtUpdate the timestamp of an existing file to now:
touch README.mdSet a specific timestamp:
touch -d '2025-01-15 09:30:00' report.txtCreate multiple files at once:
touch file1.txt file2.txt file3.txtBehavior notes
Section titled “Behavior notes”- If the file does not exist and its parent directory exists, an empty file is created.
- If the parent directory does not exist,
touchemits an error instead of creating intermediate directories. -daccepts any date string parseable by[System.DateTime]::Parse().
Comparison with bash
Section titled “Comparison with bash”Supports -d for setting timestamps. Bash touch also supports -t, -a, -m, and -c; those are not implemented.
Create hard or symbolic links between files.
ln [OPTION]... TARGET LINK_NAME| Flag | Description |
|---|---|
-s | Create a symbolic link instead of a hard link |
-f | Remove existing destination file before creating the link |
-v | Explain what is being done |
Return type
Section titled “Return type”No output by default. With -v, returns PsBash.TextOutput showing the created link.
Examples
Section titled “Examples”Create a symbolic link:
ln -s /path/to/target shortcutCreate a hard link with verbose output:
ln -v original.txt hardlink.txt'hardlink.txt' => 'original.txt'Force-replace an existing link:
ln -sf /new/target existing-linkCreate a symbolic link with verbose output:
ln -sv config.json config-link.json'config-link.json' -> 'config.json'Behavior notes
Section titled “Behavior notes”- Without
-s, creates a hard link (target must be a file on the same filesystem). - With
-s, creates a symbolic link (can target files or directories, and can cross filesystems). -fremoves the existing destination before creating the link.- Verbose output uses
->for symbolic links and=>for hard links.
Comparison with bash
Section titled “Comparison with bash”Identical flags. The Windows symlink restriction is a platform limitation, not a PsBash difference.
Cross-platform notes
Section titled “Cross-platform notes”All commands accept both / and \ as path separators on all platforms. Verbose output always uses forward slashes (/) for consistency with bash conventions.
Symbolic links (ln -s) on Windows require one of:
- Developer Mode enabled in Settings > Update & Security > For developers
- Running PowerShell as Administrator
Hard links (ln without -s) work without elevation but the target must be a file on the same NTFS volume.
File permission behavior differs by platform:
- Linux/macOS: File permissions are preserved during
cpandmvoperations via the underlying .NET APIs. - Windows: NTFS ACLs apply. Unix-style permission strings shown by
ls -lare approximated from file attributes.