Catalog / Command-Line & Shell Cheatsheet
Command-Line & Shell Cheatsheet
A comprehensive guide to navigating and utilizing command-line interfaces, covering essential commands, shell scripting, and environment management for increased productivity.
Navigation & File Management
Basic Navigation
|
Print Working Directory - Displays the current directory path. |
|
Change Directory - Navigates to the specified directory. Use |
|
List - Displays files and directories in the current directory. Use |
|
Represents the current directory. |
|
Represents the parent directory. |
|
Represents the user’s home directory. |
File Operations
|
Make Directory - Creates a new directory. |
|
Creates a new empty file. |
|
Copy - Copies a file or directory from source to destination. Use |
|
Move/Rename - Moves a file or directory, or renames it if the destination is in the same directory. |
|
Remove - Deletes a file. Use with caution. Use |
|
Create a symbolic link. A symbolic link (also known as a soft link) is a type of file that contains a reference to another file or directory in the form of an absolute or relative path. |
File Content Examination
|
Concatenate - Displays the entire content of a file. |
|
Displays the first few lines of a file (default 10 lines). |
|
Displays the last few lines of a file (default 10 lines). |
|
Opens a file in a pager, allowing you to navigate through the content. Use |
|
Word Count - Displays the number of lines, words, and characters in a file. |
|
Determines the file type. |
Searching & Filtering
Basic Searching
|
Globally search a Regular Expression and Print. Searches for a specific pattern in a file. |
|
Finds files in a directory hierarchy based on the specified name. |
|
Finds files by name using a pre-built database. Requires the |
|
Locates the executable file associated with a command. |
|
Locates the binary, source, and manual page files for a command. |
|
Searches command history for a specific pattern. |
Filtering and Redirection
|
Passes the output of one command as input to another command. Example: |
|
Redirects the output of a command to a file, overwriting the file if it exists. Example: |
|
Appends the output of a command to a file. Example: |
|
Redirects standard error to a file. Example: |
|
Redirects both standard output and standard error to a file. Example: |
|
Sorts the lines of a text file. Example: |
Advanced Text Manipulation
|
Stream EDitor - A powerful tool for text transformation. Example: |
|
Pattern scanning and processing language - Useful for extracting and manipulating data from text files. Example: |
|
Removes sections from each line of files. |
|
Translates or deletes characters. Example: |
|
Reports or omits repeated lines. Often used with sort. |
Shell Scripting
Basic Script Structure
Every shell script typically starts with a shebang (
|
Make the script executable: |
Variables
Variable Assignment |
Example: |
Accessing Variables |
Example: |
Read-only Variables |
Example: |
Unsetting Variables |
Example: |
Environment Variables |
Variables that are set in the environment and available to all processes. Examples: |
Control Structures
|
|
|
|
|
|
|
|
Functions
Defining a Function |
|
Calling a Function |
|
Passing Arguments |
Inside the function: Example: |
Returning Values |
Use |
Input/Output
Reading Input |
Example:
|
Printing Output |
|
Formatted output |
Example: |
System Information & Process Management
System Information
|
Displays kernel information. |
|
Displays the system’s hostname. |
|
Shows how long the system has been running, along with the current time and average system load. |
|
Displays disk space usage in a human-readable format. |
|
Displays memory usage in megabytes. |
|
Displays the current user. |
Process Management
|
Displays a snapshot of the current processes. |
|
Displays a dynamic real-time view of running processes. Press |
|
An interactive process viewer. Must be installed separately on most systems. |
|
Sends a signal to a process, usually to terminate it. Use |
|
Kills processes by name. |
|
Resumes a suspended process in the background. |
|
Moves a background process to the foreground. |
|
Lists the active jobs. |
User and Group Management
|
Displays user and group IDs. |
|
Displays the groups a user belongs to. |
|
Changes the user’s password. |
|
Executes a command with superuser privileges. |
|
Substitute User - Allows switching to another user account. |