pwd— print working directoryls,ls -lah— list files with optionscd,cd ..,cd ~— navigate directorieshead,tail,less— view file contents|pipe — chain commands;>redirect output to a file*wildcard — match any number of characters
We'll start with a fresh copy of the shell data today.
$ cd ~/Desktop/
$ rm -r shell-lesson-data # remove old copy
$ wget https://swcarpentry.github.io/shell-novice/data/shell-lesson-data.zip
$ unzip shell-lesson-data.zip
$ cd shell-lesson-data
$ mkdir thesis # make a new directory
$ ls -F
Try making a directory called "Thesis version1". What happens? Why? Remember: avoid spaces in file and directory names. Use underscores or hyphens instead.
$ rm -r thesis version1 # -r flag required for directories (recursive)
$ mkdir thesis_v1
$ cd thesis_v1
Creating Files with nano
nano is a simple text editor that runs inside the terminal. Write something, then save with Ctrl+O and exit with Ctrl+X.
$ nano draft.txt
File Extensions
$ cp draft.txt draft.fasta
$ cp draft.txt draft.mp3
# Open these files — do they look any different?
# Extensions are just labels — Unix doesn't care what they are
Moving and Copying Files
$ mkdir oldversions
$ mv draft.txt oldversions/ # move the file
$ cp oldversions/draft.txt . # copy it back (. means "current directory")
$ nano draft.txt # edit the copy
Creating Files with echo
$ echo This is my final draft
$ echo This is my final draft > thesis_final.txt # write to file (overwrites)
$ echo This is my final draft >> thesis_final.txt # append (adds to end)
$ cd north-pacific-gyre
$ mkdir rawdata
# All of these do the same thing:
$ cp *.txt rawdata/
$ cp NEN* rawdata/
$ cp N*txt rawdata/
# Wildcards work on folders too
$ cp */*.txt all_text/
The ? Wildcard
? matches exactly one character — useful when you don't want to match everything.
$ cd ../thesis
$ cp draft.txt draft_v1.txt
$ cp draft.txt draft_v2.txt
$ cp draft.txt draft_v3.txt
$ cp draft.txt draft_v11.txt
$ cp draft.txt draft_v12.txt
$ mkdir old
$ mv draft_v?.txt old # matches v1, v2, v3 but NOT v11, v12 (two chars after v)
$ ls old
Navigate to exercise-data/alkanes — this directory has *.pdb files (atomic coordinate files from crystallography).
$ wc -l *.pdb # count lines in each file
$ wc *.pdb # lines, words, and characters
$ wc -l *.pdb > lengths.txt
$ less lengths.txt
$ sort -n lengths.txt # sort numerically
Create a new version of lengths.txt that does NOT include the "total" line at the end. Hint: combine wc, head, and a pipe.
Answer: wc -l *.pdb | head -6 > lengths.txt
Extracting Columns with cut
$ cd ../exercise-data/animal-counts
$ less animals.csv
$ cut -d , -f 2 animals.csv # -d sets delimiter (comma), -f 2 = second column
# Remove duplicates — must sort first for uniq to work
$ cut -d , -f 2 animals.csv | sort | uniq
$ cut -d , -f 2 animals.csv | sort | uniq -c # -c adds counts
$ cd ../north-pacific-gyre
$ less goodiff.sh # a shell script — do you recognize any commands?
# The first line (shebang) identifies the interpreter
$ which bash
$ which python
$ which R
$ ls -lh # look at permissions (rw-r--r-- etc.)
$ chmod +x *.sh # make scripts executable
$ ls -lh # notice the x in permissions now
$ ./goodiff.sh # run the script
Combining commands with pipes produces powerful but hard-to-read one-liners. Use AI to understand them. Try this prompt:
After you understand it, ask AI: "How would I modify this to show only animals that appear more than 3 times?"
No additional setup needed. We'll continue working in shell-lesson-data for loops and scripts.