Command Line

Linux/Mac Command Line Cheat Sheet

A comprehensive Linux and Mac command line cheat sheet for beginners covering navigation, file management, text search, scripting, networking, and practical examples.

Linux/Mac Command Line Cheat Sheet thumbnail

Access this command line cheat sheet for quick reference to essential Linux and Mac terminal commands. It covers file management, navigation, viewing and editing files, searching text, scripting basics, networking commands, and practical command examples.

For QA engineers and automation students, terminal confidence matters. You will use the command line to run tests, inspect logs, manage files, work with Git, start Docker containers, connect to servers, and debug automation projects.

Table Of Contents

Command Line Cheat Sheet

CommandWhat It DoesExample
pwdPrint the current working directory.pwd
lsList directory contents.ls -l
ls -aList all files, including hidden files.ls -a
cdChange the current directory.cd /home/user
cd ..Move up one directory.cd ..
touchCreate a new empty file.touch newfile.txt
clearClear the terminal screen.clear
pwd
ls -l
ls -la
cd /home/user
cd ..
touch newfile.txt
clear

File And Directory Management

CommandWhat It DoesExample
mkdirCreate a new directory.mkdir new_folder
mkdir -pCreate nested directories.mkdir -p new_folder/nested_folder
rmRemove a file.rm file.txt
rm -rRemove a directory and its contents.rm -r folder
mvMove or rename files and directories.mv old_name.txt new_name.txt
cpCopy files.cp file.txt copy_of_file.txt
cp -rCopy directories recursively.cp -r dir1 dir2
mkdir new_folder
mkdir -p new_folder/nested_folder
rm file.txt
rm -r folder
mv old_name.txt new_name.txt
mv file.txt /new/location/
cp file.txt copy_of_file.txt
cp -r dir1 dir2

Viewing And Editing Files

CommandWhat It DoesExample
catDisplay file content.cat file.txt
cat file1 file2Display multiple files.cat file1.txt file2.txt
nanoOpen a simple terminal text editor.nano file.txt
lessView a file one screen at a time.less file.txt
headShow the first part of a file.head file.txt
head -nShow a specific number of starting lines.head -n 20 file.txt
tailShow the last part of a file.tail file.txt
tail -nShow a specific number of ending lines.tail -n 20 file.txt
tail -fFollow file changes in real time.tail -f file.txt
cat file.txt
cat file1.txt file2.txt
nano file.txt
less file.txt
head file.txt
head -n 20 file.txt
tail file.txt
tail -n 20 file.txt
tail -f file.txt

Searching And Filtering Text

CommandWhat It DoesExample
grepSearch for text in a file.grep "search_term" file.txt
grep -iSearch case-insensitively.grep -i "search_term" file.txt
grep -rSearch recursively through a directory.grep -r "search_term" /path/to/directory
grep -riRecursive case-insensitive search.grep -ri "search_term" /path/to/directory
grep -rlShow only file names with matches.grep -rl "search_term" /path/to/directory
findSearch for files in a directory hierarchy.find /path -name "filename"
find . -type fFind files by pattern.find . -type f -name "*.txt"
awkProcess columns and patterns.awk '{print $1}' file.txt
sedFilter and transform text.sed 's/old/new/g' file.txt
grep "search_term" file.txt
grep -i "search_term" file.txt
grep -r "search_term" /path/to/directory
grep -ri "search_term" /path/to/directory
grep -rl "search_term" /path/to/directory
find /path -name "filename"
find . -type f -name "*.txt"
awk '{print $1}' file.txt
sed 's/old/new/g' file.txt

Scripting Basics

CommandWhat It DoesExample
bashExecute a bash script.bash script.sh
bash -xRun a script in debug mode.bash -x script.sh
shExecute a shell script.sh script.sh
sh -vRun a shell script in verbose mode.sh -v script.sh
bash script.sh
bash -x script.sh
sh script.sh
sh -v script.sh

Command Line For Networking

CommandWhat It DoesExample
pingTest network connectivity.ping google.com
ping -cLimit ping to a specific number of packets.ping -c 4 google.com
sshLog in to another host over SSH.ssh user@hostname
ssh -iUse a specific SSH key.ssh -i ~/.ssh/id_rsa user@hostname
ping google.com
ping -c 4 google.com
ssh user@hostname
ssh -i ~/.ssh/id_rsa user@hostname

Command Line Tools And Utilities

CommandWhat It DoesExample
tar -cvfCreate a tar archive.tar -cvf archive.tar file1 file2
tar -xvfExtract a tar archive.tar -xvf archive.tar
tar -cvf archive.tar file1 file2
tar -xvf archive.tar

Commands And Examples

The cheat sheet above gives quick command references. The examples below explain how to use important commands in real workflows.

Tail Command Tutorial

The tail command displays the last part of a file. It is especially useful for monitoring log files that are constantly being updated.

Basic Usage: View The Last 10 Lines

tail filename

This displays the last 10 lines of filename. It is useful when you want to quickly check the newest content in a log file or configuration file without opening the whole file.

Customize Line Count

tail -n 100 filename

The -n flag lets you specify exactly how many lines you want to see from the end of the file. For example, tail -n 100 filename shows the last 100 lines.

This is useful when you need more context than the default 10 lines but do not want to read the whole file.

Follow A File In Real Time

tail -n 100 -f filename

The -f flag puts tail into follow mode. Instead of showing the current end of the file and exiting, it keeps watching the file and displays new lines as they are added.

This is one of the most useful commands for QA and debugging. For example, if a web application is writing to app.log, you can run:

tail -n 100 -f app.log

You will see the last 100 log entries first, then new log entries will appear live as the app runs.

Use this when:

  • Monitoring test logs
  • Watching server logs
  • Debugging application behavior
  • Confirming whether an action triggers expected backend events

Stop following the file with Ctrl+C.

Grep Command Tutorial

The grep command searches for text patterns inside files and command output. It is one of the most useful tools for debugging, log analysis, and searching through code.

Search In A Single File

grep "search_term" file.txt

This searches for the exact text search_term inside file.txt and prints matching lines. The search is case-sensitive by default, so Error and error are different matches.

Example:

grep "ERROR" app.log

This shows every line in app.log that contains ERROR.

grep -i "search_term" file.txt

The -i flag ignores letter case. This means it matches error, Error, ERROR, and other case variations.

Example:

grep -i "error" app.log

Use this when logs or files are inconsistent about capitalization.

grep -r "search_term" /path/to/directory

The -r flag searches recursively through all files in the directory and its subdirectories.

Example:

grep -r "function_name" /path/to/project

This is useful when searching through a codebase for a function, setting, test name, endpoint, or error message.

grep -ri "search_term" /path/to/directory

You can combine flags. The -ri combination searches recursively and ignores case.

Example:

grep -ri "database" /path/to/project

This finds database, Database, DATABASE, and other case variations across the project.

Show Only File Names With Matches

grep -rl "search_term" /path/to/directory

The -l flag shows only file names that contain matches instead of printing every matching line.

Example:

grep -rl "TODO" /path/to/project

This lists every file that contains TODO.

Filter Command Output With Grep

You can combine grep with other commands using pipes:

ls -la | grep ".txt"

This filters the directory listing and only shows lines containing .txt.

Free Software Testing Fundamentals

If you are learning the command line because you want to move into QA or automation, start with the testing foundations too.

The free Software Testing Fundamentals course covers:

  • Testing basics, test cases, bug reporting, and core QA concepts
  • SQL and database testing
  • API testing
  • Jira and real team workflows

Start Learning Free

Linux/Mac Command Line Cheat Sheet | SuperSQA