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

Shell Cheat Sheet

The shell is a program called bash that allows users to interact with the operating system via commands. The shell allows users to navigate directories, view and edit files, run programs, and automate tasks using scripts. Key shell commands and concepts covered include navigating directories, moving/deleting/renaming files, viewing file contents, searching files, counting lines, wildcards, variables, loops, and creating simple shell scripts.

Uploaded by

Denise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Shell Cheat Sheet

The shell is a program called bash that allows users to interact with the operating system via commands. The shell allows users to navigate directories, view and edit files, run programs, and automate tasks using scripts. Key shell commands and concepts covered include navigating directories, moving/deleting/renaming files, viewing file contents, searching files, counting lines, wildcards, variables, loops, and creating simple shell scripts.

Uploaded by

Denise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Shell Cheat Sheet

The shell is just a program called bash

Annotations
Starting with a / indicates an absolute file path: /Users/denisefath/Downloads
No slash indicates a relative file path (if pwd is /Users/denisefath/, I could type ls Downloads

.. means directory above one I’m currently in/parent directory


If I’m in /Users/denisefath/Downloads, cd .. would put me in /Users/denisefath

. means current directory

~ means home directory


~ means /Users/denisefath
cd ~/Downloads takes me to Downloads folders

Change Directory
cd path  cd /Users/denisefath/Downloads

Move a File (overwrites!)


mv current_file_path future_path  mv Downloads/spd.csv Documents (would move spd.csv
into Documents from Downloads)

mv Downloads/spd.csv Downloads/spd2.csv Documents (moves both files into Documents)

Rename a File (overwrites!)


mv info.txt desc.txt (simply renames it since the file isn’t moved anywhere)

If a file name has spaces, use apostrophes:


mv ‘lab info.txt’ ‘cat info.txt’

Delete a File
rm info.txt Downloads/tbd.csv (deletes both info.txt and tbd.csv)

Delete a Directory
rmdir Downloads (would delete Downloads folder only if empty)

Create a Directory
mkdir Downloads (would create Downloads directory)

View File’s Contents


cat file.txt (will display file contents in shell)
Save Output Elsewhere
Head -n 5 file.txt > top.csv will save the first 5 rows to top

View File’s Contents One Page at a Time


less file.txt (will display page 1 in shell. Use space bar to go to next page)

less file.txt file2.txt (use :n to jump to file2’s contents, :q to quit)

Show first 10 rows of a csv


head ~/Downloads/tbd.csv

head -n 100 ~/Downloads/tbd.csv will show first 100 lines

Show Files
ls -R will show all files recursively

Show Select Columns of a File


cut -f 2-5,8 -d , values.csv

Get Help Instructions


man head shows the manual for the head command and automatically invokes less

Example:
SYNOPSIS
head [-n count | -c bytes] [file ...]

Show Recently Run Commands


history
!## will re-run that command (output shows command serial numbers that you would use)
!head will re-run your most recent use of that command

GREP
Grep flags:
-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

grep -v -n molar seasonal/spring.csv shows lines (with row number) that have word molar
grep -c incisor seasonal/autumn.csv seasonal/winter.csv would show how many lines contain
that word in each of the files
Count Rows/Lines in a File
wc -l tbd.csv
Wild Card
wc -l Downloads/* will count the rows for all files in Downloads
wc -l Downloads/*.csv will count the rows for all csvs in Downloads

? 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

Show in Alphabetical Order


cat ~/Downloads/tbd.txt sort

Quit
To force quit, use control + C

Get Value of Environmental Variable


set – shows all variables
echo $HOME will display the value of HOME (/Users/denisefath)

Create a Local Variable


training=Downloads/tbd.csv

Access Variable’s Value


echo $tbd.txt will give the value

Loops
for filetype in gif jpg png; do echo $filetype; done
for file in seasonal/*.csv; do head -n 2 $file | tail -n 1; done

The structure is for …variable… in …list… ; do …body… ; done

Creating a Text/TXT File


nano filename, it will open filename for editing (or create it if it doesn't already exist)
Ctrl + K: delete a line
Ctrl + U: un-delete a line
Ctrl + O: save the file ('O' stands for 'output') + enter
Ctrl + X: exit the editor
If you a text editor to put a command in a .sh file and then run bash filename.sh – it will execute

bash filename.sh > output.txt will run script and save output

Can use $@ to make script variables generic, so you can input them

So if unique.sh contains the one line command: sort $@ | uniq


Running bash unique.sh data.csv will run data.csv in place of $@

Can also use $1 annotation for command-line specific parameters:


cut -d , -f $2 $1
bash column.sh info.csv 1
will run: cut -d , -f 1 info.csv

Scripts can have loops, using separate lines for clarity:


# Print the first and last data records of each file.
for filename in $@
do
head -n 2 $filename | tail -n 1
tail -n 1 $filename
done

bash date-range.sh seasonal/*.csv | sort

You might also like