Bash Shell CheatSheet
Bash Shell CheatSheet
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
> 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
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
>
Rename a file by moving it into the current directory
Getting help mv sales.csv sales-2023.csv
elif [ $x < 50 ] ; then
Relative paths can start with the current working directory, . Redirect the output from a command to a file with >
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 [...]
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