0% found this document useful (0 votes)
30 views

Bash Shell CheatSheet

The document discusses basic file manipulation commands in Bash and zsh terminals including cat, sort, head, tail, grep, cut, wc, cp, mv, and rm. It also covers loops, flow control, and getting help in terminals.

Uploaded by

brian koren
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Bash Shell CheatSheet

The document discusses basic file manipulation commands in Bash and zsh terminals including cat, sort, head, tail, grep, cut, wc, cp, mv, and rm. It also covers loops, flow control, and getting help in terminals.

Uploaded by

brian koren
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

> Displaying Files > Manipulating File Contents

Bash & zsh


Display the whole file with cat Sort lines of a file with sort

Shell Terminal Basics


cat README.txt sort random_order.txt

Display a whole file, a page at a time with less Sort in descending order using sort -r
less README.txt sort -r random_order.txt

Display the first few lines of a file with head Combine cut and sort using a pipe to sort a column of a CSV file

Learn Bash online at www.DataCamp.com head -n 10 filename sales.csv # Get first 10 lines of sales.csv cut -d , -f 2 | sort

Display the last few lines of a file with tail Remove adjacent duplicate lines with uniq
tail -n 10 filename sales.csv # Get last 10 lines of sales.csv uniq sales.csv

Display columns of a CSV file with cut Get counts of (adjacent) duplicate lines with uniq -c
> What are Bash & zsh Terminals? cut -d , -f 2-5, 8 sales.csv # Using comma delimiter, select fields 2 to 5 & 8 uniq -c sales.csv

Display the lines of a file containing text matching a regular expression with grep Combine sort and uniq using a pipe to remove duplicate lines
Shell terminals, such as Bash and zsh, are text-based user interfaces for interacting with an operating system. grep [0-9]+ sales.csv # Matches lines containing numbers sort random_order.txt | uniq
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 Display the names of files with filenames containing text matching a regular expression with grep -r
the default on MacOS systems. grep -r sales[0-9]+\.csv # Matches filesnames with "sales", numbers, dot "csv"

Get the line word & character count of a file with wc > Loops and Flow Control
Definitions wc README.txt
Execute a command for multiple values with for variable in values; do command; done
The working directory is the directory that commands are executed from. By default, commands will read and datafiles=*.csv

write files to this directory.

for file in datafiles; do echo $file; done


The root directory is the top of the file system. All other directories are contained within the hierarchy of this
directory.

> Copying, Moving and Removing Files Spread loops over multiple lines for increased readability
An absolute path starts from the root directory. Think of it like latitude and longitude - the values for a location datafiles=*.csv

don't change wherever you are.


Copy (and paste) a file to a new directory with cp for file in datafiles

A relative path starts from the working directory. Think of it like directions from where you are, like "20 cp sales.csv data/sales-2023.csv # Copy to data dir and rename do 

kilometers West from here".


echo $file

A glob pattern is a way of specifying multiple files at once.


Copy files matching a glob pattern with cp pattern newdir
done
A regular expression is a more complex way of specifying text fragments. Learn more in DataCamp's Regular cp *.csv data/ # Copy all CSV files to data dir
Expressions Cheat Sheet. Conditionally execute code with if [ condn ]; then command; else alt_command; fi
Move (cut and paste) a file to a new directory with mv
x=99;

mv sales.csv data/sales-2023.csv # Move to data dir and rename if [ $x > 50 ] ; then

echo "too high";

>
Rename a file by moving it into the current directory
Getting help mv sales.csv sales-2023.csv
elif [ $x < 50 ] ; then

echo "too low";

Move files matching a glob pattern with mv pattern newdir else

Display the manual for a command with man


echo "spot on";

mv *.csv data/ # Move all CSV files to data dir


man head fi
Prevent overwriting existing files with mv -n
mv -n sales.csv data/sales-2023.csv # Rename unless new filename exists

> File System Navigation Remove (delete) a file with rm


> Reusing Commands
rm bad_data.json
Print the current working directory with pwd Remove a directory with rmdir See your previous commands with history
pwd rmdir temp_results history
Change the current working directory with cd
Save commands in a shell file (extension .sh) and run them with bash or zsh
cd data/raw # Go to raw dir inside data dir inside current dir
bash mycommands.sh 

Absolute paths start with the root directory, /


> Combining commands zsh mycommands.sh
cd /home

Relative paths can start with the current working directory, . Redirect the output from a command to a file with >

cd ./images head -n 5 sales.csv > top_sales.csv

Move up to the parent directory with .. (can be used repeatedly) Pipe the output from a command to another command with |
cd ../.. # Go to grandparent directory head -n 5 sales.csv | tail -n 1

List files and folders in the current working directory with ls Redirect the input to a command with <
ls head -n 5 < sales.csv

List all files and folders, including hidden ones (names starting .) with ls -a
ls -a
Learn Power BI Online at
List files and folders in a human-readable format with ls -lh > Glob Patterns www.DataCamp.com
ls -lh

List files and folders matching a glob pattern with ls pattern Match one or more character with *
ls *.csv # Returns all CSV files *.txt # Match all txt files

Recursively list all files below the current working directory with ls -R Match a single character with ?
ls -R sales202?.csv # Match this decade's sales data files

List estimated disk usage of files and folders in a human-readable format with du -ah Match any character in the square brackets with [...]

du -ah head -n 5 < sales.csv

Find files by name in the current directory & its subdirectories with find . -type f -name pattern Match any patterns listed in the curly braces with {...}

find . -type f -name *.ipynb # Find Juypter notebooks {*.csv *.tsv} # Matches all CSV and TSV files

You might also like