Shell File and Directory Operations
A practical guide to locating, creating, copying, moving, renaming, and removing filesystem items safely, with verification and troubleshooting.
Locate and Inspect Items
Locate and inspect items
A path tells a command which filesystem item to use. A is interpreted from the current directory; an starts at the filesystem root. The symbols . and .. mean the current directory and its parent, respectively. Because a depends on where you are, begin by checking your location:
pwd
Use ls to list items. Add -a to include hidden names and -l for a long listing; combine them as ls -la. Quote a path containing spaces so the shell treats it as one path, as in ls -l "project files".
A typical Unix-like long listing includes the item type and permissions, link count, owner, group, size, modification time, and name. Exact formatting varies by system. Also, the size shown for a directory is not the total size of everything inside it. Use these checks to confirm the location and identify the item before changing it.
Create Directories and Files
Create directories and files
Use mkdir to make a directory. For a single directory, enter:
mkdir reports
If one or more parent directories may be missing, use -p:
mkdir -p project/data/raw
This also avoids an error if the requested directory already exists. On GNU systems, mkdir normally reports an error when the target already exists unless you use -p.
The common command for creating an empty file is touch:
touch notes.txt
However, touch also updates the timestamp if notes.txt already exists. If that change is unwanted, inspect the item first—for example, with ls -l notes.txt—before using the command.
In PowerShell, New-Item creates items and Get-ChildItem lists them. As with other file operations, check that you are in the intended location and that the path names the intended item.
Copy, Move, and Rename
Copy, move, and rename
Choose the command according to whether the original should remain. cp copies a file and leaves the original where it is. For example:
cp notes.txt backup.txt
The last path supplied to cp is the destination. If that destination is a directory, the source is copied into it under its existing name:
cp notes.txt archive/
To copy a directory and its contents, use -R:
cp -R project/ project-backup/
By contrast, mv moves an item. For instance, mv notes.txt archive/ moves the file to that directory. If the destination is a new name in the same directory, mv renames the item:
mv draft.txt final.txt
Before copying or moving, inspect the destination and confirm it is the one you intend. An existing destination may be replaced depending on the command, options, and shell configuration. Directory-copy behavior and overwrite prompts can also vary among systems.
PowerShell provides Copy-Item, Move-Item, and Rename-Item for these operations. Check the source and destination before running any of them.
Remove Carefully and Verify Results
Remove carefully and verify results
rm removes files, but does not remove directories by default. Use rmdir for an empty directory, or rm -R for a directory and its contents. The -i option prompts before each removal; for example, rm -i old-notes.txt asks before removing that file. For a directory, rm -Ri old-project/ prompts during recursive removal. Recursive deletion can affect many files, so inspect the path carefully before confirming. Avoid rm -rf unless you fully understand the target and consequences: it removes recursively while suppressing prompts. Command-line deletion is not necessarily a secure erase, and recovery is not guaranteed.
A safe routine is: check the location, inspect the source and destination, run one operation, then verify the result. Use pwd and ls to check where you are and what is there. After an operation, inspect the destination or directory rather than assuming it worked. A successful command may print a message—or nothing. Silence alone does not prove that the intended change occurred. In Unix-like shells, a zero usually indicates success; a nonzero status indicates failure.
Use error messages as clues. For “No such file or directory,” check spelling, capitalization, the current location, and each path component. For “Permission denied,” verify the path and why access is needed instead of reflexively using elevated privileges. For “File exists,” inspect the existing item before choosing another name or an overwrite option. A trailing directory path can change how command operands are interpreted, so list the destination after copying or moving.
A filename beginning with - can be read as an option. On GNU systems, use -- to separate options from the name, as in rm -- -draft; ./-draft also specifies a file in the current directory.
In PowerShell, Remove-Item removes items, and applicable commands support -WhatIf to preview an operation. PowerShell’s rm alias refers to Remove-Item, not necessarily the Unix rm program.
: Creates a directory; the
-poption also creates missing parent directories and avoids an error if the requested directory already exists.: Commonly used to create an empty file, and also updates the timestamp of an existing file.
: Copies an item while leaving the original in place.
: Moves an item to another location or, when given a new name in the same directory, renames it.
: Removes files; it does not remove directories by default.