Mastering basic shell commands is foundational for efficient, reliable work on Unix-like systems. This reference covers navigation, file manipulation, permissions, processes, pipes, redirection, and common patterns you can apply immediately. Commands are presented with practical intent and expected behavior, emphasizing accuracy and repeatability. Understand options, paths, and typical output to reduce friction in daily workflows. Use these patterns safely, verify destructive actions, and build reliable scripts and automations. The focus here is durable, evergreen knowledge rather than transient updates.
Navigation and directory commands
Effective navigation starts with understanding your current location and moving intentionally through the filesystem.
pwd and identifying current location
The pwd (print working directory) command outputs the full path of the current directory. This is helpful when you lose context in deeply nested paths or after switching directories with cd.
cd and changing directory
Use cd (change directory) to move between directories. Key patterns include:
cdorcd ~: Move to your home directory.cd /: Move to the filesystem root.cd ..: Move up one directory level.cd -: Toggle between the previous two directories.
Always confirm your location with pwd after changing directories when precision matters.
ls and listing contents
The ls command lists directory contents. Common options include:
ls: Show basic names.ls -l: Long format with permissions, links, owner, size, and timestamp.ls -a: Include entries starting with.(hidden files).ls -lh: Human-readable file sizes.ls -t: Sort by modification time.
Combine options as needed, such as ls -lha.
File inspection and content viewing
Quickly inspect file details and content without opening editors.
file and identifying file types
The file command analyzes file content and reports likely type, for example whether a file is text, binary, compressed, or a directory.
head and tail for partial content
head prints the beginning of a file (default 10 lines); tail prints the end. Use options like -n 20 for a different number of lines, or -f to follow growing files such as logs in real time.
cat for simple concatenation and display
cat can display small files, concatenate files, and create simple input. For large files, prefer a pager to avoid scrollback issues.
File and directory operations
Create, remove, and move files and directories with deliberate, verified actions.
mkdir and creating directories
mkdir creates directories. Use -p to create parent directories as needed and avoid errors when intermediate directories do not exist.
cp and copying files
cp copies files and directories. Key options include:
-ror-R: Recursive copy for directories.-i: Interactive prompt before overwrite.-v: Verbose output showing copies.
mv and moving or renaming
mv moves or renames files and directories. It can also serve as a rename operation. As with cp, prefer -i for interactive confirmation when overwriting.
rm and removing files and directories
rm removes files and directories. Use cautiously:
rm file: Remove a file.rm -r directory: Remove directories recursively.rm -rf: Force remove without prompts; ensure the target is correct before using this combination.
Prefer moving files to a quarantine directory before permanent deletion when possible.
Working with permissions and ownership
Control access with permissions and ownership managed through chmod, chown, and visibility tools.
chmod and changing permissions
Set read, write, and execute bits using symbolic or numeric modes. Examples:
chmod u+x script.sh: Add execute for the user.chmod 755 script.sh: Owner can read/write/execute; group and others can read/execute.
chown and changing ownership
chown changes owner and optionally group. Use -R recursively. This typically requires elevated privileges.
sudo and running commands as another user
sudo executes commands with another user’s privileges, commonly root. Configure judiciously and follow the principle of least privilege.
Process management
View and manage running processes to monitor system behavior and intervene when necessary.
ps for listing processes
ps reports snapshot of processes. Common options:
ps aux: Show processes for all users with detailed info.ps -ef: Full-format listing.
Piping to grep or pgrep helps filter process lists.
top and htop for dynamic views
top provides a dynamic, real-time view of processes and resource use. htop offers a more user-friendly interface with mouse support if available.
kill and sending signals
Use kill with a PID to send signals, commonly to terminate processes gracefully:
kill PID: Send SIGTERM (gentle termination).kill -9 PID: Send SIGKILL (forceful, cannot be caught).
Prefer terminating gracefully before using forceful signals.
Pipes, filters, and redirection
Combine simple tools to accomplish complex tasks efficiently.
Understanding pipes
The pipe | sends the output of one command as input to another. For example, ls -l | less enables paging through long directory listings.
Common filters and utilities
Text-oriented tools are often chained:
grep: Filter lines matching a pattern.awk: Pattern-scanning and processing language.sed: Stream editor for basic text transformations.sort,uniq,cut,wc: Sort, deduplicate, extract fields, and count.
Redirection and appending
Control where output goes:
> file: Redirect output to file, overwriting if it exists.>> file: Append output to file.2> file: Redirect standard error to a file.&> file: Redirect both stdout and stderr.
Useful reference table
The following table summarizes command purpose and a safe example usage for quick reference.
| Command | Purpose | Example | Typical Output |
|---|---|---|---|
| pwd | Print working directory | pwd | /home/user |
| cd dir | Change current directory | cd /tmp | (no output) |
| ls -l | List files in long format | ls -l | Permissions links owner size date name |
| mkdir -p a/b/c | Create nested directories | mkdir -p a/b/c | (no output) |
| cp -r src dst | Copy files/directories recursively | cp -r src dst | (no output unless -v) |
| mv old new | Move or rename files | mv old new | (no output unless -v) |
| rm -r dir | Remove directories recursively | rm -r dir | (no output unless -v) |
| chmod 755 f | Set file permissions | chmod 755 f | (no output) |
| ps aux | List processes | ps aux | Process table rows |
| grep word file | Search for text pattern | grep word file | Matching lines |
| cmd > file | Redirect output to file | echo hi > out.txt | (no output) |
| cmd >> file | Append output to file | echo hi >> out.txt | (no output) |
Path basics and command discovery
Understanding path resolution helps avoid common errors.
Absolute vs relative paths
An absolute path starts from root (/), while a relative path is interpreted from the current directory. Use cd projects to enter a subdirectory, or cd ../.. to go up two levels.
The PATH variable and locating commands
The PATH environment variable lists directories searched for executables. Use which command or command -v command to locate an executable and type command to see how the shell would interpret it.
Safety and workflow habits
Minimize risk with a few disciplined habits.
- Use
-iwithcpandmvwhere accidental overwrites are possible. - Back up important files before recursive removals:
cp -r important/ important-backup/. - Use tab completion to reduce typos in paths and command names.
- Quote paths with spaces and prefer double quotes for variables, e.g.,
cp "$file" ./backup/. - Test commands that operate recursively or destructively on non-critical data first.
Aliases and functions for frequent commands
Short aliases reduce typing and prevent mistakes for commonly used flags.
alias ll='ls -la': Common listing alias placed in shell startup file.alias ..='cd ..': Quick parent-directory navigation.- For more complex behavior, define shell functions in your startup file and source them as needed.
Conclusion
These basic shell commands form a reliable toolkit for everyday system interaction. Internalize navigation, safe file operations, effective use of pipes and redirection, and standard utilities. Practice them consistently, verify critical actions, and expand into scripts to automate routine work. This foundation supports durable productivity across many environments and remains relevant over time.