Catalog / Command-Line & Shell Cheatsheet
Command-Line & Shell Cheatsheet
A comprehensive cheat sheet for navigating and utilizing command-line interfaces and shell environments effectively. This guide covers essential commands, scripting techniques, and environment configurations for improved productivity.
Basic Navigation & File Management
Navigation Commands
|
Print working directory (shows the current directory). |
|
Change directory to |
|
List directory contents (files and subdirectories). |
|
List directory contents in long format (permissions, size, etc.). |
|
List all files, including hidden files (starting with |
|
List files sorted by modification time (newest first). |
File & Directory Manipulation
|
Create a new directory named |
|
Create an empty file named |
|
Copy the file or directory |
|
Move or rename the file or directory |
|
Remove (delete) the file |
|
Remove the directory |
File Viewing
|
Display the entire contents of |
|
View the contents of |
|
Display the first few lines of |
|
Display the last few lines of |
|
Display the last few lines of |
|
Word count - Display number of lines, words, and bytes in file. |
Piping, Redirection, and Permissions
Piping and Redirection
|
Pass the output of one command as input to another command. Example: |
|
Redirect the output of a command to a file, overwriting the file if it exists. Example: |
|
Append the output of a command to a file without overwriting it. Example: |
|
Redirect standard error to a file. Example: |
|
Redirect standard output and standard error to a file. Example: |
|
Redirect input from a file to a command. Example: |
File Permissions
|
Change the permissions of a file or directory. Permissions can be specified numerically (e.g., |
|
Change the owner and group of a file or directory. |
|
The output shows permissions in the format |
Numeric Permissions |
|
Symbolic Permissions |
Example: |
|
Sets default permissions for newly created files and directories. Common value is |
Process Management
|
Display a snapshot of the current processes. |
|
Display a comprehensive list of all processes. |
|
Display a dynamic real-time view of running processes. |
|
Terminate the process with the specified process ID (PID). Example: |
|
Forcefully terminate the process (use as a last resort). Example: |
|
Place a stopped job in the background. |
|
Move a background job to the foreground. |
|
List active jobs. |
Shell Scripting Basics
Script Structure
A shell script is a text file containing a sequence of commands.
|
Variables
Defining a variable |
|
Accessing a variable |
|
Environment variables |
Accessed like regular variables. Examples: |
Read-only variables |
|
Unsetting a variable |
|
Control Structures
If statement:
For loop:
While loop:
Until loop:
|
Functions
Defining a function |
or
|
Calling a function |
|
Passing arguments |
Inside the function, access arguments using |
Returning a value |
Use |
Command Substitution
|
Execute Example: |
|
(Deprecated) - An older form of command substitution (using backticks). |
Advanced Shell Techniques
Regular Expressions (grep)
|
sed (Stream Editor)
|
awk (Pattern Scanning and Processing Language)
|
find
|
Find all files with the |
|
Find all directories named |
|
Find all files larger than 1MB in the current directory. |
|
Find files modified in the last 7 days. |
|
Find all files owned by |
|
Execute the |