Cheat sheet for Shell script

AndReda Mind
4 min readJan 30, 2023

Introduction to the Unix shell. You’ll learn why it is still in use after almost 50 years, how it compares to the graphical tools you may be more familiar with, how to move around in the shell, and how to create, modify, and delete files and folders.

to clear the terminal:

clear

to show the time:

who am i

what is all the orders that you have:

ls

open “X” folder:

cd X

create ‘S’ folder:

mkdir S

create ‘S.txt’ file

touch S.txt

move ‘S.txt’ file to X:

mv S.txt X

remove ‘S.txt’ file:

rm S.txt

remove ‘X’ folder:

rm -r X

explain to me ls command line:

what is ls

to check that you're there:

pwd

cp seasonal/summer.csv backup/summer.bck: copy of seasonal/summer.csv in the backup directory:

Copy spring.csv and summer.csv from the seasonal

cp, which is short for "copy"

cp seasonal/spring.csv seasonal/summer.csv backup

Rename the file winter.csv to be winter.csv.bck

mv winter.csv winter.csv.bck

go back:

cd

delete folder:

rmdir people

Move /home/repl/people/agarwal.txt into /tmp/scratch.

mv ~/people/agarwal.txt scratch

Print the contents of course.txt to the screen.

cat course.txt

view those two files, use cat to print large files

less seasonal/spring.csv seasonal/summer.csv | cat

prints the first 10 lines of a file

head seasonal/summer.csv

prints the first 5 lines of a file

head -n 5 seasonal/winter.csv

list everything below a directory (flag -R (which means "recursive"))

ls -R /home/repl

backup bin course.txt people seasonal

ls -R -F /home/repl

flag -F that prints a / after the name of every directory

backup bin course.txt people seasonal

get help for tail command (cat for the print)

man tail | cat

tail with the flag -n +7 to display all but the first six lines of seasonal/spring.csv.

tail -n +7 seasonal/spring.csv

Re-run the head command with !head

!head

Use history to look at what you have done.

history

rerun the 3ed line from history:

!3

Print the contents of all of the lines containing the word molar in seasonal/autumn.csv

grep molar seasonal/autumn.csv

Invert the match to find all of the lines that don’t contain the word molar in seasonal/spring.csv

grep -v -n molar seasonal/spring.csv

Count how many lines contain the word incisor in autumn.csv and winter.csv

grep -c incisor seasonal/autumn.csv seasonal/winter.csv

Big hints:

  • -c: print a count of matching lines rather than the lines themselves
  • -h: do not print the names of files when searching multiple files
  • -i: ignore case (e.g., treat "Regression" and "regression" as matches)
  • -l: print the names of files that contain matches, not the matches
  • -n: print line numbers for matching lines
  • -v: invert the match, i.e., only show lines that don't match
  • -r : reverse the sort order

to save any command’s output anywhere:

head -n 5 seasonal/summer.csv > top.csv

then run this

cat top.csv

it will print what you print what you saved in top.csv (the top 5 lines of seasonal/summer.csv)

Running 2 commands together:

head -n 5 seasonal/summer.csv | tail -n 3

this will get the first 5 lines then print the last 3 of these 5 lines.

let's make more complicated command:

1- select all of the names from column 2: “cut -d , -f 2 seasonal/summer.csv”

2- and get rid of the header “Tooth”: grep -v Tooth

3- print just the first line: head -n 1

cut -d , -f 2 seasonal/summer.csv | grep -v Tooth | head -n 1

get any line if the date = 2017–07, then count them with wc -l

grep 2017-07 seasonal/spring.csv | wc -l

Work on many files:

Most shell commands will work on multiple files if you give them multiple filenames.

cut -d , -f 1 seasonal/winter.csv seasonal/spring.csv seasonal/summer.csv seasonal/autumn.csv

or

cut -d , -f 1 seasonal/*

or

cut -d , -f 1 seasonal/*.csv

or here just get from files that start with s:

head -n 3 seasonal/s*

The shell has other wildcards as well, though they are less commonly used:

  • ? matches a single character, so 201?.txt will match 2017.txt or 2018.txt, but not 2017-01.txt.
  • [...] matches any one of the characters inside the square brackets, so 201[78].txt matches 2017.txt or 2018.txt, but not 2016.txt.
  • {...} matches any of the comma-separated patterns inside the curly brackets, so {*.txt, *.csv} matches any file whose name ends with .txt or .csv, but not files whose names end with .pdf.

the flags -n and -r can be used to sort numerically and reverse

cut -d , -f 2 seasonal/winter.csv | grep -v Tooth | sort -r

to remove duplicate lines: use uniq -c to display unique lines with a count of how often each occurs

cut -d , -f 2 seasonal/winter.csv | grep -v Tooth | sort | uniq -c

save the output of a pipe:

> result.txt head -n 3 seasonal/winter.csv

1. wc with appropriate parameters to list the number of lines in all of the seasonal data files. (-l to list only the lines)

2. remove total word

3. sort numerically

4. to find the file containing the fewest lines. (head -n 1)

wc -l seasonal/*.csv | grep -v total | sort -n | head -n 1

print variable:

echo $variable

OR

training=seasonal/summer.csv
echo $training
# Then you can use it like:
head -n 1 $testing

Repeat a command many times:

print the files types:

for filetype in docx odt pdf; do echo $filetype; done

print the files names:

for filename in people/*; do echo $filename; done

print the last entry from July 2017 (2017-07) in every seasonal file:

for file in seasonal/*.csv; do grep 2017-07 $file | tail -n 1; done

~ as a shortcut for the path to your home directory.

cp seasonal/s* ~ 
#This will send the cope to your home directory.

Use bash to run the file dates.sh then output to teeth.out:

bash all-dates.sh > dates.out
cat dates.out

--

--