Skip to content

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
FlagDescription
-r, -RCopy directories recursively
-vExplain what is being done
-nDo not overwrite an existing file
-fForce: remove existing destination directory before copying

No output by default. With -v, returns PsBash.TextOutput showing each copy operation.

Copy a single file:

Terminal window
cp config.json config.backup.json

Copy a directory recursively with verbose output:

Terminal window
cp -rv src/ src-backup/
'src/main.ps1' -> 'src-backup/main.ps1'
'src/util.ps1' -> 'src-backup/util.ps1'

Copy without overwriting existing files:

Terminal window
cp -n defaults.json settings.json

Copy multiple files into a directory:

Terminal window
cp file1.txt file2.txt dest/
  • When DEST is an existing directory, sources are copied into it.
  • -n silently skips files that already exist at the destination.
  • -f removes an existing destination directory before copying (useful for replacing a directory tree).
  • -r is required for directories; without it, cp emits an error and skips.

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
FlagDescription
-vExplain what is being done
-nDo not overwrite an existing file
-fForce (do not prompt before overwriting)

No output by default. With -v, returns PsBash.TextOutput showing each move operation.

Rename a file:

Terminal window
mv old-name.txt new-name.txt

Move files into a directory with verbose output:

Terminal window
mv -v *.log archive/
'error.log' -> 'archive/error.log'
'access.log' -> 'archive/access.log'

Move without overwriting:

Terminal window
mv -n draft.md final.md
  • When DEST is an existing directory, sources are moved into it.
  • -n silently skips when the destination already exists.
  • Works on both files and directories without needing a recursive flag.

Identical syntax and semantics.


Remove files or directories.

rm [OPTION]... FILE...
FlagDescription
-r, -RRemove directories and their contents recursively
-fIgnore nonexistent files, never prompt
-vExplain what is being done

No output by default. With -v, returns PsBash.TextOutput listing each removed path.

Remove a single file:

Terminal window
rm temp.txt

Remove a directory tree with verbose output:

Terminal window
rm -rv build/
removed 'build/output.js'
removed 'build/output.css'
removed 'build/'

Silently remove files that may not exist:

Terminal window
rm -f maybe-missing.txt
  • Without -r, attempting to remove a directory produces an error.
  • -f suppresses 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.

Identical flags. The safety guards against rm / and rm ~ match common GNU coreutils --preserve-root behavior, enabled by default.


Create directories.

mkdir [OPTION]... DIRECTORY...
FlagDescription
-pCreate parent directories as needed, no error if existing
-vPrint a message for each created directory

No output by default. With -v, returns PsBash.TextOutput for each directory created.

Create a single directory:

Terminal window
mkdir output

Create nested directories:

Terminal window
mkdir -p src/components/ui

Create with verbose output:

Terminal window
mkdir -v logs
mkdir: created directory 'logs'
  • Without -p, creating a directory that already exists produces an error.
  • Without -p, creating a directory whose parent does not exist produces an error.
  • -p silently succeeds when the directory already exists.

Identical syntax and semantics.


Remove empty directories.

rmdir [OPTION]... DIRECTORY...
FlagDescription
-pRemove directory and its empty ancestors
-vPrint a message for each removed directory

No output by default. With -v, returns PsBash.TextOutput for each directory removed.

Remove an empty directory:

Terminal window
rmdir old-output

Remove a chain of empty parent directories:

Terminal window
rmdir -pv a/b/c
rmdir: removing directory, 'a/b/c'
rmdir: removing directory, 'a/b'
rmdir: removing directory, 'a'

Attempt to remove a non-empty directory (produces an error):

Terminal window
rmdir src
# rmdir: failed to remove 'src': Directory not empty
  • Only removes empty directories. Use rm -r for directories with contents.
  • -p walks up the path removing each ancestor only while it is empty.
  • Errors on non-directory targets and missing paths.

Identical syntax and semantics.


Create files or update timestamps.

touch [OPTION]... FILE...
FlagDescription
-dUse the specified date string instead of the current time

No output. Creates missing files as empty files and updates LastWriteTime and LastAccessTime on existing files.

Create a new empty file:

Terminal window
touch notes.txt

Update the timestamp of an existing file to now:

Terminal window
touch README.md

Set a specific timestamp:

Terminal window
touch -d '2025-01-15 09:30:00' report.txt

Create multiple files at once:

Terminal window
touch file1.txt file2.txt file3.txt
  • If the file does not exist and its parent directory exists, an empty file is created.
  • If the parent directory does not exist, touch emits an error instead of creating intermediate directories.
  • -d accepts any date string parseable by [System.DateTime]::Parse().

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
FlagDescription
-sCreate a symbolic link instead of a hard link
-fRemove existing destination file before creating the link
-vExplain what is being done

No output by default. With -v, returns PsBash.TextOutput showing the created link.

Create a symbolic link:

Terminal window
ln -s /path/to/target shortcut

Create a hard link with verbose output:

Terminal window
ln -v original.txt hardlink.txt
'hardlink.txt' => 'original.txt'

Force-replace an existing link:

Terminal window
ln -sf /new/target existing-link

Create a symbolic link with verbose output:

Terminal window
ln -sv config.json config-link.json
'config-link.json' -> 'config.json'
  • 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).
  • -f removes the existing destination before creating the link.
  • Verbose output uses -> for symbolic links and => for hard links.

Identical flags. The Windows symlink restriction is a platform limitation, not a PsBash difference.


All commands accept both / and \ as path separators on all platforms. Verbose output always uses forward slashes (/) for consistency with bash conventions.