Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Sale ends in
�d ��h ��m ��s
EN
C H E AT S H E E T S Category
Home Cheat sheets Data Science
Bash & zsh Shell Terminal Basics Cheat Sheet
Improve your Bash & zsh Shell skills with the handy shortcuts featured in
this convenient cheat sheet!
Contents Nov 14, 2023 · 6 min read
Richie Cotton
Webinar & podcast host, course and book author, spends all day chit-chatting about data
TO P I C S
Data Science
1 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Have this cheat sheet at your fingertips
Download PDF
What are Bash & zsh Terminals?
Shell terminals, such as Bash and zsh, are text-based user interfaces for interacting with
an operating system. They allow you to input commands through a command line,
offering direct communication with the system for tasks like file manipulation, program
execution, and system control. Bash is common on Linux systems and zsh is the default
on MacOS systems.
Definitions
The working directory is the directory that commands are executed from. By default,
commands will read and write files to this directory.
The root directory is the top of the file system. All other directories are contained within
the hierarchy of this directory.
An absolute path starts from the root directory. Think of it like latitude and longitude -
the values to a location don't change wherever you are.
A relative path starts from the working directory. Think of it like directions from where
you are, like "20 kilometers West from here".
A glob pattern is a way of specifying multiple files at once.
A regular expression is a more complex way of specifying text fragments. Learn more
in DataCamp's Regular Expressions Cheat Sheet.
Getting Help
Display the manual for a command with man
2 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
man head
POWERED BY
File System Navigation
Print the current working directory with pwd
pwd
POWERED BY
Change the current working directory with cd
cd data/raw # Go to raw dir inside data dir inside current dir
POWERED BY
Absolute paths start with the root directory, /
cd /home
POWERED BY
Relative paths can start with the current working directory, .
cd ./images
POWERED BY
Move up to the parent directory with .. (can be used repeatedly)
cd ../.. # Go to grandparent directory
POWERED BY
3 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
List files and folders in the current working directory with ls
ls
POWERED BY
List all files and folders, including hidden ones (names starting .) with ls -a
ls -a
POWERED BY
List files and folders in a human-readable format with ls -lh
ls -lh
POWERED BY
List files and folders matching a glob pattern with ls pattern
ls *.csv # Returns all CSV files
POWERED BY
Recursively list all files below the current working directory with ls -R
ls -R
POWERED BY
List estimated disk usage of files and folders in a human-readable format with du -ah
du -ah
POWERED BY
Find files by name in the current directory & its subdirectories with find . -type f -name
4 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
pattern
find . -type f -name *.ipynb # Find Juypter notebooks
POWERED BY
Displaying Files
Display the whole file with cat
cat README.txt
POWERED BY
Display a whole file, a page at a time with less
less README.txt
POWERED BY
Display the first few lines of a file with head
head -n 10 filename sales.csv # Get first 10 lines of sales.csv
POWERED BY
Display the last few lines of a file with tail
tail -n 10 filename sales.csv # Get last 10 lines of sales.csv
POWERED BY
Display columns of a CSV file with cut
5 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
cut -d , -f 2-5, 8 sales.csv # Using comma delimiter, select fields 2 to 5 & 8
POWERED BY
Display the lines of a file containing text matching a regular expression with grep
grep [0-9]+ sales.csv # Matches lines containing numbers
POWERED BY
Display the names of files with filenames containing text matching a regular expression
with grep -r
grep -r sales[0-9]+\.csv # Matches filesnames with "sales", numbers, dot "csv"
POWERED BY
Get the line word & character count of a file with wc
wc README.txt
POWERED BY
Copying, Moving and Removing Files
Copy (and paste) a file to a new directory with cp
cp sales.csv data/sales-2023.csv # Copy to data dir and rename
POWERED BY
Copy files matching a glob pattern with cp pattern newdir
cp *.csv data/ # Copy all CSV files to data dir
POWERED BY
6 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Move (cut and paste) a file to a new directory with mv
mv sales.csv data/sales-2023.csv # Move to data dir and rename
POWERED BY
Rename a file by moving it into the current directory
mv sales.csv sales-2023.csv
POWERED BY
Move files matching a glob pattern with mv pattern newdir
mv *.csv data/ # Move all CSV files to data dir
POWERED BY
Prevent overwriting existing files with mv -n
mv -n sales.csv data/sales-2023.csv # Rename unless new filename exists
POWERED BY
Remove (delete) a file with rm
rm bad_data.json
POWERED BY
Remove a directory with rmdir
rmdir temp_results
POWERED BY
7 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Combining commands
Redirect the output from a command to a file with >
head -n 5 sales.csv > top_sales.csv
POWERED BY
Pipe the output from a command to another command with |
head -n 5 sales.csv | tail -n 1
POWERED BY
Redirect the input to a command with <
head -n 5 < sales.csv
POWERED BY
Glob Patterns
Match one or more character with *
*.txt # Match all txt files
POWERED BY
Match a single character with ?
sales202?.csv # Match this decade's sales data files
POWERED BY
Match any character in the square brackets with [...]
8 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
sales202[0123].csv # Match sales data files for 2020 to 2023
POWERED BY
Match any patterns listed in the curly braces with {...}
{*.csv *.tsv} # Matches all CSV and TSV files
POWERED BY
Manipulating File Contents
Sort lines of a file with sort
sort random_order.txt
POWERED BY
Sort in descending order using sort -r
sort -r random_order.txt
POWERED BY
Combine cut and sort using a pipe to sort a column of a CSV file
cut -d , -f 2 | sort
POWERED BY
Remove adjacent duplicate lines with uniq
uniq sales.csv
POWERED BY
9 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Get counts of (adjacent) duplicate lines with uniq -c
uniq -c sales.csv
POWERED BY
Combine sort and uniq using a pipe to remove duplicate lines
sort random_order.txt | uniq
POWERED BY
Variables
List all environment variables with set
set
POWERED BY
Create a shell variable with name=value (no spaces around =)
mydata=sales.csv
POWERED BY
Create a shell variable from the output of a command with name=$(command)
pyfiles=$(ls *.py)
POWERED BY
Print an environment variable or shell variable name with echo $value
10 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
echo $HOME
POWERED BY
Convert a shell variable into an environment variable with export
export mydata
POWERED BY
Loops and Flow Control
Execute a command for multiple values with for variable in values; do command; done
datafiles=*.csv
for file in datafiles; do echo $file; done
POWERED BY
Spread loops over multiple lines for increased readability
datafiles=*.csv
for file in datafiles
do
echo $file
done
POWERED BY
Conditionally execute code with if [ condn ]; then command; else alt_command; fi
11 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
x=99;
if [ $x > 50 ] ; then
echo "too high";
elif [ $x < 50 ] ; then
echo "too low";
else
echo "spot on";
fi
POWERED BY
Reusing Commands
See your previous commands with history
history
POWERED BY
Save commands in a shell file (extension .sh) and run them with bash or zsh
bash mycommands.sh
zsh mycommands.sh
POWERED BY
AUTHOR
Richie Cotton
Richie helps individuals and organizations get better at using data and AI. He's been a
data scientist since before it was called data science, and has written two books and
created many DataCamp courses on the subject. He is a host of the DataFramed
podcast, and runs DataCamp's webinar program.
TO P I C S
Data Science
12 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Related
C H E AT- S H E E T
Excel Shortcuts Cheat Sheet
C H E AT- S H E E T
LaTeX Cheat Sheet
C H E AT- S H E E T
Markdown Cheat Sheet
See More
Grow your data skills with DataCamp for Mobile
Make progress on the go with our mobile courses and daily 5-minute coding challenges.
LEARN
Learn Python
Learn R
Learn AI
Learn SQL
Learn Power BI
13 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Learn Tableau
Learn Data Engineering
Assessments
Career Tracks
Skill Tracks
Courses
Data Science Roadmap
DATA C O U R S E S
Python Courses
R Courses
SQL Courses
Power BI Courses
Tableau Courses
Alteryx Courses
Azure Courses
Google Sheets Courses
AI Courses
Data Analysis Courses
Data Visualization Courses
Machine Learning Courses
Data Engineering Courses
Probability & Statistics Courses
DATA L A B
14 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Get Started
Pricing
Security
Documentation
C E R T I F I C AT I O N
Certifications
Data Scientist
Data Analyst
Data Engineer
SQL Associate
Power BI Data Analyst
Tableau Certified Data Analyst
Azure Fundamentals
AI Fundamentals
RESOURCES
Resource Center
Upcoming Events
Blog
Code-Alongs
Tutorials
Docs
Open Source
RDocumentation
15 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Course Editor
Book a Demo with DataCamp for Business
Data Portfolio
Portfolio Leaderboard
PLANS
Pricing
For Business
For Universities
Discounts, Promos & Sales
DataCamp Donates
FO R B U S I N E S S
Business Pricing
Teams Plan
Data & AI Unlimited Plan
Customer Stories
Partner Program
ABOUT
About Us
Learner Stories
Careers
Become an Instructor
Press
Leadership
16 of 17 2/12/24, 2:38
Bash & zsh Shell Terminal Basics Cheat Sheet | DataCamp https://www.datacamp.com/cheat-sheet/bash-and-zsh-s...
Contact Us
DataCamp Español
DataCamp Português
DataCamp Deutsch
DataCamp Français
S U P PO R T
Help Center
Become an Affiliate
Privacy Policy Cookie Notice Do Not Sell My Personal Information Accessibility Security
Terms of Use
© 2024 DataCamp, Inc. All Rights Reserved.
17 of 17 2/12/24, 2:38