Basic Shell Scripting
Category: Linux Command Basics
Type: Linux Commands
Generated on: 2025-07-10 03:08:09
For: System Administration, Development & Technical Interviews
Basic Shell Scripting Cheat Sheet (Linux Commands)
Section titled “Basic Shell Scripting Cheat Sheet (Linux Commands)”This cheat sheet covers essential Linux commands for shell scripting, useful for both system administrators and developers.
1. Command Overview
- Purpose: Provides a quick reference for frequently used Linux commands, covering their purpose, syntax, examples, and troubleshooting tips.
- Use Cases: System administration tasks, automating repetitive tasks, scripting deployments, monitoring system health, and more.
2. Basic Syntax
Most commands follow this general syntax:
command [options] [arguments]command: The name of the command to execute (e.g.,ls,mkdir,rm).options: Modify the behavior of the command (e.g.,-l,-r,-v). Options usually start with a hyphen (-).arguments: Data or targets for the command to operate on (e.g., filenames, directories).
3. Practical Examples
-
ls(List Directory Contents)- Example:
ls -l /home/user/documents(Lists files in/home/user/documentswith detailed information).
- Example:
-
mkdir(Make Directory)- Example:
mkdir my_new_directory(Creates a directory namedmy_new_directoryin the current working directory).
- Example:
-
rm(Remove Files or Directories)- Example:
rm myfile.txt(Deletes the filemyfile.txt). WARNING: This is permanent! - Example:
rm -r my_directory(Recursively deletes the directorymy_directoryand all its contents). EXTREMELY DANGEROUS - DOUBLE CHECK BEFORE RUNNING
- Example:
-
cp(Copy Files or Directories)- Example:
cp myfile.txt /tmp/(Copiesmyfile.txtto the/tmp/directory). - Example:
cp -r my_directory /backup/(Recursively copies the directorymy_directoryto the/backup/directory).
- Example:
-
mv(Move or Rename Files or Directories)- Example:
mv myfile.txt newfile.txt(Renamesmyfile.txttonewfile.txt). - Example:
mv myfile.txt /opt/(Movesmyfile.txtto the/opt/directory).
- Example:
-
cat(Concatenate and Display Files)- Example:
cat myfile.txt(Displays the contents ofmyfile.txton the terminal). - Example:
cat file1.txt file2.txt > combined.txt(Concatenatesfile1.txtandfile2.txtand saves the output tocombined.txt).
- Example:
-
echo(Display a Line of Text)- Example:
echo "Hello, world!"(Prints “Hello, world!” to the terminal). - Example:
echo $USER(Prints the current username).
- Example:
-
grep(Search for Patterns in Files)- Example:
grep "error" logfile.txt(Searches for the word “error” inlogfile.txt). - Example:
grep -i "error" logfile.txt(Case-insensitive search).
- Example:
-
find(Find Files and Directories)- Example:
find . -name "*.txt"(Finds all files with the.txtextension in the current directory and its subdirectories). - Example:
find / -type d -name "config"(Finds all directories named “config” starting from the root directory).
- Example:
-
chmod(Change File Permissions)- Example:
chmod 755 myscript.sh(Makesmyscript.shexecutable by the owner and readable/executable by others). (755 is a common permission set for scripts).
- Example:
-
chown(Change File Ownership)- Example:
chown user:group myfile.txt(Changes the owner ofmyfile.txttouserand the group togroup).
- Example:
-
sudo(Execute a Command as Superuser)- Example:
sudo apt update(Updates the package list as root). Use with caution!
- Example:
-
ps(Process Status)- Example:
ps aux(Lists all running processes with detailed information).
- Example:
-
kill(Terminate a Process)- Example:
kill 1234(Sends a SIGTERM signal to process with PID 1234). - Example:
kill -9 1234(Sends a SIGKILL signal - use only as a last resort, as it doesn’t allow the process to clean up).
- Example:
-
df(Disk Free Space)- Example:
df -h(Displays disk space usage in a human-readable format).
- Example:
-
du(Disk Usage)- Example:
du -sh /var/log(Displays the total size of the/var/logdirectory in a human-readable format).
- Example:
-
top(Display Top CPU Processes)- Example:
top(Interactive display of resource usage).
- Example:
-
head(Display the Beginning of a File)- Example:
head -n 10 myfile.txt(Displays the first 10 lines ofmyfile.txt).
- Example:
-
tail(Display the End of a File)- Example:
tail -f logfile.txt(Displays the last lines oflogfile.txtand follows the file, showing new lines as they are added - useful for monitoring logs in real-time).
- Example:
-
wc(Word Count)- Example:
wc -l myfile.txt(Counts the number of lines inmyfile.txt).
- Example:
-
wget(Download Files from the Web)- Example:
wget https://example.com/myfile.txt(Downloadsmyfile.txtfrom the specified URL).
- Example:
-
curl(Transfer data with URLs)- Example:
curl https://example.com(Fetches the content of the website). - Example:
curl -X POST -d "name=John&age=30" https://example.com/api/users(Performs a POST request to the API endpoint with the provided data).
- Example:
-
tar(Tape Archive - Archiving and Compression)- Example:
tar -czvf myarchive.tar.gz my_directory(Creates a compressed archive ofmy_directory). - Example:
tar -xzvf myarchive.tar.gz(Extracts the contents ofmyarchive.tar.gz).
- Example:
-
gzip(Compress Files)- Example:
gzip myfile.txt(Compressesmyfile.txtintomyfile.txt.gz). - Example:
gzip -d myfile.txt.gz(Decompressesmyfile.txt.gz).
- Example:
-
gunzip(Decompress Files)- Example:
gunzip myfile.txt.gz(Decompressesmyfile.txt.gzintomyfile.txt).
- Example:
-
zip(Compress files into a zip archive)- Example:
zip myarchive.zip myfile.txt myotherfile.txt(Creates a zip archive named myarchive.zip containing the specified files).
- Example:
-
unzip(Extract files from a zip archive)- Example:
unzip myarchive.zip(Extracts all files from myarchive.zip).
- Example:
-
ssh(Secure Shell - Remote Login)- Example:
ssh user@remote_host(Connects toremote_hostasuser). - Example:
ssh -i /path/to/private_key user@remote_host(Connects using a specific private key).
- Example:
-
scp(Secure Copy - Remote File Transfer)- Example:
scp myfile.txt user@remote_host:/tmp/(Copiesmyfile.txtto the/tmp/directory onremote_host). - Example:
scp user@remote_host:/tmp/myfile.txt .(Copiesmyfile.txtfrom the/tmp/directory onremote_hostto the current directory).
- Example:
4. Common Options
| Command | Option | Description |
|---|---|---|
ls | -l | Long listing format (detailed information). |
ls | -a | Show all files, including hidden files (starting with .). |
ls | -h | Human-readable file sizes (e.g., 1K, 234M, 2G). |
ls | -t | Sort by modification time (newest first). |
rm | -r | Recursive (remove directories and their contents). EXTREMELY DANGEROUS! |
rm | -f | Force (ignore nonexistent files, never prompt). EXTREMELY DANGEROUS! |
cp | -r | Recursive (copy directories and their contents). |
cp | -v | Verbose (show files being copied). |
mv | -i | Interactive (prompt before overwriting existing files). |
grep | -i | Case-insensitive search. |
grep | -v | Invert match (show lines that do not match the pattern). |
grep | -r | Recursive search (search in all files under each directory, recursively). |
grep | -n | Show line numbers. |
find | -name | Find files by name. |
find | -type | Find files by type (e.g., f for file, d for directory). |
find | -size | Find files by size (e.g., +10M for files larger than 10MB, -10k for files smaller than 10KB). |
df | -h | Human-readable output. |
du | -h | Human-readable output. |
du | -s | Summarize disk usage (show only the total). |
tail | -f | Follow the file (display new lines as they are added). |
head | -n | Specify the number of lines to display. |
tail | -n | Specify the number of lines to display. |
wc | -l | Count lines. |
wc | -w | Count words. |
wc | -c | Count bytes. |
tar | -c | Create archive. |
tar | -x | Extract archive. |
tar | -v | Verbose (list files being processed). |
tar | -z | Compress archive with gzip. |
tar | -f | Specify archive filename. |
gzip | -d | Decompress. |
ssh | -i | Specify private key file. |
ssh | -p | Specify port number. |
5. Advanced Usage
-
Piping (
|): Connect the output of one command to the input of another.Terminal window cat logfile.txt | grep "error" | wc -l(Counts the number of lines in
logfile.txtthat contain the word “error”). -
Redirection (
>,>>,<): Redirect input and output.Terminal window ls -l > filelist.txt # Redirect standard output to a file (overwrites).ls -l >> filelist.txt # Redirect standard output to a file (appends).cat < input.txt # Redirect standard input from a file. -
Command Substitution (
$(),`): Execute a command and use its output as part of another command.Terminal window echo "Today is $(date)"USER_HOME=`echo ~` # Be careful using backticks, prefer $() -
Loops (
for,while): Automate repetitive tasks.Terminal window # For loopfor i in *.txt; doecho "Processing file: $i"# Do something with the file heredone# While loopwhile read line; doecho "Line: $line"done < input.txt -
Conditional Statements (
if,then,else): Execute different commands based on conditions.Terminal window if [ -f myfile.txt ]; thenecho "File exists."elseecho "File does not exist."fi -
Variables: Store and reuse values.
Terminal window MY_VARIABLE="Hello"echo $MY_VARIABLE -
Functions: Define reusable blocks of code.
Terminal window my_function() {echo "This is a function."}my_function # Call the function -
Combining
findandxargs: Process files found byfindin batches.Terminal window find . -name "*.log" -print0 | xargs -0 grep "error"(Finds all
.logfiles and searches for “error” in each of them.-print0andxargs -0handle filenames with spaces correctly.) -
Using
sed(Stream EDitor): Powerful text manipulation.Terminal window sed 's/old_text/new_text/g' myfile.txt # Replace all occurrences of "old_text" with "new_text".sed '/pattern/d' myfile.txt # Delete lines containing "pattern". -
Using
awk(Pattern Scanning and Processing Language): Advanced text processing.Terminal window awk '{print $1}' myfile.txt # Print the first column of each line.awk -F',' '{print $2}' data.csv # Print the second column of a CSV file.
6. Tips & Tricks
-
Tab Completion: Use the Tab key to autocomplete commands, filenames, and directory names.
-
History: Use the Up and Down arrow keys to navigate through your command history.
historycommand to view history.!nto execute the nth command from history. -
Aliases: Create shortcuts for frequently used commands. Add to
~/.bashrc.Terminal window alias la='ls -la' -
Wildcards:
*: Matches zero or more characters.ls *.txt?: Matches exactly one character.ls file?.txt[]: Matches a range of characters.ls file[1-5].txt
-
manpages: Use themancommand to access the manual page for any command (e.g.,man ls). -
apropos: Search for commands by keyword (e.g.,apropos disk). -
Ctrl+C: Interrupt the currently running command. -
Ctrl+Z: Suspend the currently running command. Usefgto bring it back to the foreground, orbgto run it in the background. -
!!: Execute the last command again. -
$_: Refers to the last argument of the previous command.mkdir mydir; cd $_ -
set -x: Debug a shell script by printing each command before it is executed. -
set -e: Exit immediately if a command exits with a non-zero status.
7. Troubleshooting
- “Command not found”: The command is not installed or is not in your
PATH. Check the spelling and ensure the command is installed. Usewhich commandnameto find the location of the executable. - “Permission denied”: You do not have the necessary permissions to execute the command or access the file. Use
chmodto change file permissions orsudoto run the command as superuser (with caution!). - “No such file or directory”: The file or directory does not exist or the path is incorrect. Double-check the spelling and path.
- Script doesn’t execute: Ensure the script has execute permissions (
chmod +x script.sh) and the shebang is correct (#!/bin/bashat the top of the script). - Unexpected output: Carefully review the command syntax, options, and arguments. Use
echostatements to debug your script. - Infinite loop: Interrupt the script with
Ctrl+C. Review the loop condition to ensure it will eventually terminate.
8. Related Commands
- Package Management:
apt,yum,dnf,pacman(depending on your distribution). - System Information:
uname,hostname,uptime. - Networking:
ifconfig,ip,ping,netstat,ss,traceroute,dig,nslookup. - Process Management:
top,htop,nice,renice. - Text Editors:
vi,vim,nano. - Version Control:
git. - Job Control:
nohup,screen,tmux. systemctl: Manage systemd services.journalctl: View systemd logs.
This cheat sheet provides a solid foundation for working with the Linux command line and writing shell scripts. Remember to consult the man pages for detailed information on each command. Practice is key to mastering these tools!