Catalog / Linux/Bash Terminal Essentials
Linux/Bash Terminal Essentials
A handy cheat sheet for navigating and manipulating the Linux/Bash terminal environment, covering essential commands, shortcuts, and scripting tips.
Basic Navigation & File Management
Navigation Commands
|
Print working directory (current directory). |
|
Change directory. Use |
|
List files and directories in the current directory. |
|
List files with detailed information (permissions, size, modification date, etc.). |
|
List all files, including hidden files (files starting with |
|
List files sorted by modification time (newest first). |
File Operations
|
Create a new directory. |
|
Create an empty file or update the modification timestamp of an existing file. |
|
Copy a file or directory. Use |
|
Move or rename a file or directory. |
|
Remove a file. Warning: This is permanent! Use |
|
Remove an empty directory. Use |
File Content Viewing
|
Display the entire content of a file. |
|
View file content page by page. Use |
|
Display the first few lines of a file (default 10 lines). |
|
Display the last few lines of a file (default 10 lines). |
|
Display the last few lines and follow the file as it grows. Useful for log files. |
|
Word count - displays number of lines, words, and characters in a file. |
Searching & Text Manipulation
Searching
|
Search for a pattern within a file. Use |
|
Recursively search for a pattern within all files in a directory. |
|
Find files by name within a directory. |
|
Find all files within a directory. |
|
Find all directories within a directory. |
|
Find files by name using a pre-built database. Requires |
Text Manipulation
|
Replace all occurrences of |
|
Print the first column of each line in a file using AWK. |
|
Sort the lines of a file. |
|
Remove duplicate lines from a file (usually used with |
|
Cut out specific fields from a file based on a delimiter. |
|
Convert all lowercase characters to uppercase in a file. |
Piping and Redirection
|
Pipe the output of one command to the input of another. 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. Example: |
|
Redirect standard error to a file. Example: |
|
Redirect both standard output and standard error to a file. Example: |
|
Redirect the content of a file to the input of a command. Example: |
System Information & Process Management
System Information
|
Display kernel information. |
|
Display the system’s hostname. |
|
Display disk space usage in a human-readable format. |
|
Display the disk usage of a directory in a human-readable format. |
|
Display memory usage in megabytes. |
|
Show how long the system has been running. |
Process Management
|
Display all running processes. |
|
Display a dynamic real-time view of running processes. |
|
Terminate a process with the given PID (Process ID). |
|
Forcefully terminate a process (use with caution). |
|
Put a stopped process in the background. |
|
Bring a background process to the foreground. |
User Management
|
Display the current username. |
|
Display user and group IDs. |
|
Change the password for the current user. |
|
Execute a command with superuser privileges. |
|
Switch to another user. |
|
Display the groups the current user belongs to. |
Bash Scripting Basics
Script Structure
All bash scripts should start with a shebang line, which tells the system which interpreter to use:
|
Comments are denoted by
|
Variables
Setting a variable: |
Example: |
Accessing a variable: |
Example: |
Environment Variables: |
Variables that are available system-wide (e.g., |
Read-only variables: |
|
Conditional Statements
|
|
|
|
Looping
|
Example: |
|
|
|
|
Functions
Defining a function: |
Or:
|
Calling a function: |
Example:
|
Returning a value: |
Use |