Linux/Mac Command Line Cheat Sheet

command-line-cheat-sheet

Access this comprehensive Command Line cheat sheet for quick reference to essential commands across different operating systems. From file management and navigation to process control and advanced shortcuts, this guide helps both beginners and seasoned users streamline tasks and boost productivity. Ideal for anyone looking to enhance their command line skills and efficiency.

FREE "Software Testing Fundamentals" Course Learn More Software Testing Fundamentals

Command Line Cheat Sheet

Navigating the File System

pwd - Print the current working directory.

pwd

ls - List directory contents, showing files and subdirectories.

ls -l

ls -a

cd - Change the current directory to the specified path.

cd /home/user

cd .. (move up one directory)

touch - Create a new empty file.

touch newfile.txt

clear - Clear the terminal screen.

clear

▼ Show more commands

File and Directory Management

mkdir - Create new directories.

mkdir new_folder

mkdir -p new_folder/nested_folder (create nested directories)

rm - Remove files or directories.

rm file.txt

rm -r folder (remove directory and its contents)

mv - Move or rename files and directories.

mv old_name.txt new_name.txt (rename a file)

mv file.txt /new/location/ (move file to a new location)

cp - Copy files or directories.

cp file.txt copy_of_file.txt (copy a file)

cp -r dir1 dir2 (copy a directory)

Viewing and Editing Files

cat - Concatenate and display file content.

cat file.txt

cat file1.txt file2.txt (display multiple files)

nano - Simple text editor for basic editing tasks.

nano file.txt

nano /path/to/file.txt

less - View the content of a file one screen at a time.

less file.txt

head - Output the first part of files.

head file.txt (first 10 lines)

head -n 20 file.txt (first 20 lines)

tail - Output the last part of files.

tail file.txt (last 10 lines)

tail -n 20 file.txt (last 20 lines)

tail -f file.txt (follow file changes)

Searching and Filtering Text

grep - Search text using patterns within files.

grep "search_term" file.txt

grep -i "search_term" file.txt (case insensitive)

grep -r "search_term" /path/to/directory (recursive search)

grep -ri "search_term" /path/to/directory (recursive search that is case insensitive)

grep -rl "search_term" /path/to/directory (only show file names and not the content)

find - Search for files in a directory hierarchy.

find /path -name "filename"

find . -type f -name "*.txt" (find all .txt files)

awk - Pattern scanning and processing language.

awk '{print $1}' file.txt (print first column)

sed - Stream editor for filtering and transforming text.

sed 's/old/new/g' file.txt (replace text)

Scripting Basics

bash - Execute a bash script to automate tasks.

bash script.sh

bash -x script.sh (debug mode)

sh - Execute a shell script.

sh script.sh

sh -v script.sh (verbose mode)

Command Line for Networking

ping - Send ICMP ECHO_REQUEST to network hosts to test connectivity.

ping google.com

ping -c 4 google.com (limit to 4 packets)

ssh - OpenSSH SSH client for remote login to another host.

ssh user@hostname

ssh -i ~/.ssh/id_rsa user@hostname (use specific key)

Command Line Tools and Utilities

tar - Archive files.

tar -cvf archive.tar file1 file2 (create archive)

tar -xvf archive.tar (extract archive)

💡 Commands & Examples

Explore practical command examples with real-world use cases. These examples complement the cheat sheet above and demonstrate how to combine commands for powerful workflows.

`tail` - Display the last part of a file including real-time updates Click to expand tutorial ▼

Use Case: View the end of files, especially useful for monitoring log files that are constantly being updated.

Overview: The tail command is one of the most essential tools for viewing file contents, particularly the end of files. By default, it shows the last 10 lines, but you can customize how many lines to display. The real power comes when you combine it with the follow flag (-f) to watch files in real-time as they're being written to. This is incredibly useful for log files, configuration files, or any file that's actively being updated.

Basic Usage - View Last 10 Lines

tail filename

This is the simplest form of the tail command. When you run tail filename, it displays the last 10 lines of the specified file. This is perfect for quickly checking what happened most recently in a log file or seeing the end of a configuration file. For example, if you have a log file with thousands of entries, you don't need to scroll through everything - just use tail to see the most recent activity.

Customize Line Count - View Last N Lines

tail -n 100 filename

The -n flag (or --lines) allows you to specify exactly how many lines you want to see from the end of the file. In this example, tail -n 100 filename will display the last 100 lines. You can use any number here - tail -n 50 for 50 lines, tail -n 500 for 500 lines, and so on. This is especially useful when you need more context than the default 10 lines but don't want to see the entire file. For instance, if you're debugging an issue and need to see the last hour of log entries, you might use tail -n 200 to get a broader view of recent activity.

The Magic Combination - Follow Mode with Custom Line Count

tail -n 100 -f filename

This is where tail becomes truly powerful. The -f flag (or --follow) puts the command into "follow mode," which means it doesn't just show you the current end of the file and exit - it keeps watching the file and displays new lines as they're added in real-time. When you combine -n 100 with -f, you first see the last 100 lines, and then any new content that gets added to the file will appear on your screen automatically.

This is incredibly useful for monitoring log files because log files are constantly being written to. Instead of running tail repeatedly to check for new entries, you run it once with -f and watch the updates stream in real-time. For example, if you're debugging a web application, you might run tail -n 100 -f your-app.log to see the last 100 log entries and then watch as new errors, warnings, or information messages appear as they happen. The command will continue running until you stop it.

Tip: Press the stop key combination to stop following the file when you're done monitoring.

🎓 FREE Software Testing Fundamentals

14+ Hours of Premium Content

Master the fundamentals of software testing with our comprehensive free course. Perfect for beginners and those looking to strengthen their testing skills.

What You'll Learn
Testing Basics
Test cases, bug reporting & more
SQL & Database Testing
Query databases and test data integrity
API Testing
Test REST APIs and endpoints
JIRA
Manage bugs and track issues
Start Learning Free

100% Free • No Credit Card Required