







Open VirtualBox and click on the New button.
Enter VM Name and Location:

























Following these steps will help you successfully install Debian 12 on VirtualBox, allowing you to explore and utilize the Debian operating system in a virtual environment.
Humans and computers interact in various ways, such as through:
Using a GUI to copy the third line of 1000 text files in different directories is time-consuming and error-prone. The Unix shell, a CLI, can automate and speed up this process.
$.$ when entering commands.$ ls
$ ks
ks: command not found
Nelle Nemo, a marine biologist, needs to process 1520 samples using an imaginary program goostats.sh.
goostats.sh by hand using a GUI takes over 12 hours.$ pwd might output /Users/nelle.$ ls might output:
Applications Documents Library Music Public
Desktop Downloads Movies Pictures
-F: Classifies the output by adding markers to file and directory names.
/ indicates a directory.@ indicates a link.* indicates an executable.$ cd Desktop moves to the Desktop directory././Users/nelle./Users/nelle/Desktop).Desktop/shell-lesson-data).$ pwd
/Users/nelle
$ ls -F
Applications/ Documents/ Library/ Music/ Public/
Desktop/ Downloads/ Movies/ Pictures/
$ cd Desktop
$ pwd
/Users/nelle/Desktop
$ cd shell-lesson-data/exercise-data
$ pwd
/Users/nelle/Desktop/shell-lesson-data/exercise-data
$ cd ..
$ pwd
/Users/nelle/Desktop/shell-lesson-data
$ cd
$ pwd
/Users/nelle
-a option with ls to show hidden files:
$ ls -Fa
./ ../ .bash_profile Applications/ Documents/ Library/ Music/
--help:
$ ls --help
man:
$ man ls
-:
$ ls -F -a
$ ls -Fa
$ ls -F Desktop
shell-lesson-data/
$ cd /Users/nelle/Desktop/shell-lesson-data
~):
$ cd ~
$ cd ~/Desktop
-):
$ cd -
$ cd /Users/nelle/Desktop
$ cd Desktop
$ ls nor[TAB]
$ ls north-pacific-gyre/
pwd prints the current working directory.ls [path] lists a directory’s contents.cd [path] changes the current directory.-./ on Unix and \ on Windows./ alone represents the root directory.. means the current directory; .. means the parent directory.This detailed note should help you understand the basics of navigating files and directories in the shell. Practice these commands and explore your file system to become more comfortable with these concepts.
$ pwd
/Users/nelle/Desktop/shell-lesson-data
$ cd exercise-data/writing/
$ ls -F
haiku.txt LittleWomen.txt
$ mkdir thesis
$ ls -F
haiku.txt LittleWomen.txt thesis/
$ mkdir -p ../project/data ../project/results
$ ls -FR ../project
../project/:
data/ results/
../project/data:
../project/results:
- or _ instead.-: Commands treat names starting with - as options.., -, and _: Avoid special characters unless necessary.').$ cd thesis
$ nano draft.txt
Ctrl + O, then Enter.Ctrl + X.touch:
$ touch my_file.txt
$ ls -l my_file.txt
$ rm my_file.txt
.txt, .pdf, .cfg, .png, etc.).mv:
$ mv thesis/draft.txt thesis/quotes.txt
$ ls thesis
quotes.txt
$ mv thesis/quotes.txt .
$ ls thesis
$ ls quotes.txt
quotes.txt
$ mv sucrose.dat maltose.dat ../raw/
cp:
$ cp quotes.txt thesis/quotations.txt
$ ls quotes.txt thesis/quotations.txt
quotes.txt thesis/quotations.txt
cp -r:
$ cp -r thesis thesis_backup
$ ls thesis thesis_backup
thesis:
quotations.txt
thesis_backup:
quotations.txt
rm:
$ rm quotes.txt
$ ls quotes.txt
ls: cannot access 'quotes.txt': No such file or directory
rm -r:
$ rm -r thesis
$ mkdir backup
$ cp creatures/minotaur.dat creatures/unicorn.dat backup/
*: Represents zero or more characters.?: Represents exactly one character.???ane.pdb matches cubane.pdb, ethane.pdb, octane.pdb.$ ls *t*ane.pdb
ethane.pdb methane.pdb
$ mkdir recombined
$ mv proteins.dat recombined/
$ cp recombined/proteins.dat ../proteins-saved.dat
$ cp *dataset* backup/datasets
$ cp *calibration* backup/calibration
$ cp 2015-11-* send_to_bob/all_november_files/
$ cp 2015-10-23-* send_to_bob/all_datasets_created_on_a_23rd/
$ mv fructose.dat analyzed/
$ mv sucrose.dat analyzed/
$ mkdir 2016-05-20
$ cd 2016-05-20
$ mkdir data
$ cd data
$ mkdir raw processed
These notes should give you a solid understanding of creating, copying, moving, and deleting files and directories, as well as using wildcards and organizing files efficiently in a Unix shell environment.
Last updated on 2024-04-12 | Edit this page
Questions:
Objectives:
Now that we are familiar with some basic commands, we can explore one of the shell’s most powerful features: combining existing programs in new ways. We will use the shell-lesson-data/exercise-data/alkanes directory, which contains files in Protein Data Bank format, describing simple organic molecules.
Example Files in Directory:
$ ls
cubane.pdb methane.pdb pentane.pdb
ethane.pdb octane.pdb propane.pdb
Word Count Command:
$ wc cubane.pdb
Output:
20 156 1158 cubane.pdb
wc (word count) counts lines, words, and characters in files.
Using Wildcards with wc:
$ wc *.pdb
Output:
20 156 1158 cubane.pdb
12 84 622 ethane.pdb
9 57 422 methane.pdb
30 246 1828 octane.pdb
21 165 1226 pentane.pdb
15 111 825 propane.pdb
107 819 6081 total
wc *.pdb shows the total count of lines in the last line.
Displaying Only Line Counts:
$ wc -l *.pdb
Output:
20 cubane.pdb
12 ethane.pdb
9 methane.pdb
30 octane.pdb
21 pentane.pdb
15 propane.pdb
107 total
The -l option shows only the number of lines per file.
Issue with Missing Filename:
$ wc -l
If no filename is provided, wc waits for input from the command prompt. To exit this state, use Ctrl+C.
Redirecting Output to a File:
$ wc -l *.pdb > lengths.txt
The > operator redirects the output to a file. If lengths.txt exists, it will be overwritten.
Displaying File Content:
$ cat lengths.txt
Output:
20 cubane.pdb
12 ethane.pdb
9 methane.pdb
30 octane.pdb
21 pentane.pdb
15 propane.pdb
107 total
cat displays the content of files.
Viewing Content Page by Page:
$ less lengths.txt
less allows scrolling through the file page by page.
Using sort Command:
Example File (numbers.txt):
10
2
19
22
6
Sorting Numerically:
$ sort -n numbers.txt
Output:
2
6
10
19
22
The -n option sorts numerically rather than alphanumerically.
Sorting and Redirecting:
$ sort -n lengths.txt > sorted-lengths.txt
$ head -n 1 sorted-lengths.txt
Output:
9 methane.pdb
head -n 1 shows the first line of the sorted file.
Important Note: Avoid redirecting output to the same file being processed to prevent data loss.
Appending Data:
$ echo hello >> testfile02.txt
>> appends output to a file.
Using Pipes:
$ wc -l *.pdb | sort -n | head -n 1
The | operator pipes output from one command to another.
Combining Commands: Pipes can chain multiple commands together to avoid using intermediate files.
Example:
$ wc -l *.pdb | sort -n | head -n 3
This command finds the three files with the least number of lines.
Unix programs are designed to be simple and work well together using pipes and filters. Each program reads from standard input, processes the data, and writes to standard output.
Example Pipeline:
$ cat animals.csv | head -n 5 | tail -n 3 | sort -r > final.txt
Understanding what passes through each stage of the pipeline is crucial.
Using cut and uniq:
$ cut -d , -f 2 animals.csv | sort | uniq
The cut command extracts columns, and uniq removes duplicates.
Counting Animals:
$ cut -d, -f 2 animals.csv | sort | uniq -c
The -c option with uniq counts occurrences.
wc: Counts lines, words, and characters.cat: Displays file contents.sort: Sorts lines of text.head: Displays the first few lines.tail: Displays the last few lines.>: Redirects output to a file (overwriting).>>: Appends output to a file.|: Pipes output from one command to another.Use pipes and filters to link simple programs together, leveraging their ability to process and transform data streams.
Feel free to ask if you need further details or explanations!
| **Last updated on 2023-11-10 | Edit this page** |
Loops in programming allow for the repetition of commands or sets of commands for each item in a list. This feature enhances productivity through automation by reducing the need for manual repetition and minimizing typing errors.
You can use loops to apply one or more commands to each file in a set.
A loop in shell scripting is generally used to repeat commands for each item in a list. Here’s the basic structure of a for loop:
for variable in list_of_items
do
command_using $variable
done
for: Indicates the start of the loop.variable: Holds the current item from the list in each iteration.do: Starts the block of commands to be executed for each item.done: Ends the loop.Suppose we have several files: basilisk.dat, minotaur.dat, unicorn.dat. We want to print out the classification for each species (given on the second line of each file). Here’s how a loop can solve this:
for filename in basilisk.dat minotaur.dat unicorn.dat
do
echo $filename
head -n 2 $filename | tail -n 1
done
Output:
basilisk.dat
CLASSIFICATION: basiliscus vulgaris
minotaur.dat
CLASSIFICATION: bos hominus
unicorn.dat
CLASSIFICATION: equus monoceros
$ to > to indicate that a multi-line command is being entered.; can be used to separate commands on a single line.Inside a loop, the $ symbol is used to refer to the value of a variable. For instance, $filename gets replaced with the current filename during each iteration.
filename).x or misleading names like temperature.Loops can handle different types of lists, including filenames, numbers, or other data subsets.
Example: Listing files with .pdb extension:
for datafile in *.pdb
do
ls $datafile
done
ls *.pdb lists all .pdb files in each iteration.ls $datafile lists individual files, expanding each file’s name.To limit the files processed:
for filename in c*
do
ls $filename
done
c are listed.Using wildcards like *c* can match different patterns:
for filename in *c*
do
ls $filename
done
c anywhere in the name.If you save the content of each file into alkanes.pdb:
for alkanes in *.pdb
do
echo $alkanes
cat $alkanes > alkanes.pdb
done
propane.pdb) is saved to alkanes.pdb.Appending content to all.pdb:
for datafile in *.pdb
do
cat $datafile >> all.pdb
done
.pdb file contents are concatenated and saved to all.pdb.To select lines 81-100 from each file:
for filename in *.dat
do
echo $filename
head -n 100 $filename | tail -n 20
done
For filenames with spaces, use quotes:
for filename in "red dragon.dat" "purple unicorn.dat"
do
head -n 100 "$filename" | tail -n 20
done
To copy files with a prefix:
for filename in *.dat
do
cp $filename original-$filename
done
original-.Nelle builds up commands for processing data files with goostats.sh. She uses history commands and arrow keys to edit and re-run previous commands.
history: Displays recent commands.!number: Repeats a command by number.!!: Repeats the last command.!$: Refers to the last argument of the previous command.goostats.shfor datafile in NENE*A.txt NENE*B.txt
do
bash goostats.sh $datafile stats-$datafile
done
$variable to expand variable values.These notes cover the essentials of loops, their usage, and practical examples to enhance your shell scripting skills.
Last updated on 2023-08-05
Shell scripts are a powerful way to automate repetitive tasks by saving a series of commands in a file. This file can then be executed to run the commands as a program. Shell scripts enhance efficiency, accuracy, and reproducibility, making them a valuable tool for managing command-line operations.
Creating a Basic Script
Start by creating a new file called middle.sh in the alkanes/ directory:
$ cd alkanes
$ nano middle.sh
Insert the following line into middle.sh:
head -n 15 octane.pdb | tail -n 5
Save the file (Ctrl-O) and exit (Ctrl-X). Execute the script with:
$ bash middle.sh
This command will output lines 11 to 15 from octane.pdb.
Using Variables in Scripts
To make the script more flexible, edit middle.sh to use a variable for the filename:
$ nano middle.sh
head -n 15 "$1" | tail -n 5
Run the script with:
$ bash middle.sh octane.pdb
You can now use different filenames:
$ bash middle.sh pentane.pdb
Handling Multiple Arguments
Modify middle.sh to handle additional arguments for line ranges:
$ nano middle.sh
head -n "$2" "$1" | tail -n "$3"
Run the script with:
$ bash middle.sh pentane.pdb 15 5
This will extract lines 15 through 19 from pentane.pdb.
Adding Comments
Improve readability by adding comments at the top of middle.sh:
$ nano middle.sh
# Select lines from the middle of a file.
# Usage: bash middle.sh filename end_line num_lines
head -n "$2" "$1" | tail -n "$3"
Processing Multiple Files
Create a script sorted.sh to sort files by their length:
$ nano sorted.sh
# Sort files by their length.
# Usage: bash sorted.sh one_or_more_filenames
wc -l "$@" | sort -n
Run the script with:
$ bash sorted.sh *.pdb ../creatures/*.dat
Listing Unique Species
Write species.sh to list unique species from multiple files:
$ nano species.sh
# List unique species from files.
# Usage: bash species.sh filename1 filename2 ...
cut -d , -f 2 "$@" | sort | uniq
Run the script with:
$ bash species.sh animals.csv
Re-creating Commands from History
Save recent commands to a script:
$ history | tail -n 5 > redo-figure-3.sh
Edit redo-figure-3.sh to remove history command numbers and extra lines.
Debugging Scripts
If a script produces no output, use the -x option to debug:
$ bash -x do-errors.sh NENE*A.txt NENE*B.txt
The -x option will show the commands being executed, helping to identify errors.
bash [filename].$@ for all command-line arguments and $1, $2, etc., for specific arguments.Shell scripts can greatly streamline workflows, making repetitive tasks more efficient and less prone to errors. By leveraging the power of scripting, you can automate complex operations and ensure consistency in your command-line tasks.
This guide explains how to find files and lines within files using Unix commands. It covers the use of grep to search for patterns in text files and find to locate files and directories. It also discusses combining command outputs and handling different types of files, including binary files.
grep (Global Regular Expression Print) is a command-line utility used to search for lines in files that match a specified pattern. It is highly versatile and can handle a variety of search options.
To search for a pattern in a file:
grep pattern filename
$ grep not haiku.txt
OUTPUT
Is not the true Tao, until
"My Thesis" not found
Today it is not working
-w option:
$ grep -w The haiku.txt
OUTPUT
The Tao that is seen
-i option:
$ grep -n -w -i "the" haiku.txt
OUTPUT
1:The Tao that is seen
2:Is not the true Tao, until
6:and the presence of absence:
-v option:
$ grep -n -w -v "the" haiku.txt
OUTPUT
1:The Tao that is seen
3:You bring fresh toner.
5:With searching comes loss
7:"My Thesis" not found.
9:Yesterday it worked
10:Today it is not working
11:Software is like that.
-r option:
$ grep -r Yesterday .
OUTPUT
./LittleWomen.txt:"Yesterday, when Aunt was asleep and I was trying to be as still as a
./LittleWomen.txt:Yesterday at dinner, when an Austrian officer stared at us and then
./LittleWomen.txt:Yesterday was a quiet day spent in teaching, sewing, and writing in my
./haiku.txt:Yesterday it worked
$ grep -E "^.o" haiku.txt
OUTPUT
You bring fresh toner.
Today it is not working
Software is like that.
find with grep:
$ grep "searching" $(find . -name "*.txt")
OUTPUT
./writing/LittleWomen.txt:sitting on the top step, affected to be searching for her book, but was
./writing/haiku.txt:With searching comes loss
find is a command-line utility used to search for files and directories that match specified criteria.
To search for files or directories:
find path criteria
$ find .
OUTPUT
.
./writing
./writing/LittleWomen.txt
./writing/haiku.txt
./creatures
./creatures/basilisk.dat
./creatures/unicorn.dat
./creatures/minotaur.dat
./animal-counts
./animal-counts/animals.csv
./numbers.txt
./alkanes
./alkanes/ethane.pdb
./alkanes/propane.pdb
./alkanes/octane.pdb
./alkanes/pentane.pdb
./alkanes/methane.pdb
./alkanes/cubane.pdb
$ find . -type d
OUTPUT
.
./writing
./creatures
./animal-counts
./alkanes
$ find . -type f
OUTPUT
./writing/LittleWomen.txt
./writing/haiku.txt
./creatures/basilisk.dat
./creatures/unicorn.dat
./creatures/minotaur.dat
./animal-counts/animals.csv
./numbers.txt
./alkanes/ethane.pdb
./alkanes/propane.pdb
./alkanes/octane.pdb
./alkanes/pentane.pdb
./alkanes/methane.pdb
./alkanes/cubane.pdb
$ find . -name "*.txt"
OUTPUT
./writing/LittleWomen.txt
./writing/haiku.txt
./numbers.txt
$ wc -l $(find . -name "*.txt")
OUTPUT
21022 ./writing/LittleWomen.txt
11 ./writing/haiku.txt
5 ./numbers.txt
21038 total
grep and find:
$ grep "searching" $(find . -name "*.txt")
OUTPUT
./writing/LittleWomen.txt:sitting on the top step, affected to be searching for her book, but was
./writing/haiku.txt:With searching comes loss
Binary files contain data in formats other than text. grep is designed for text and may not work well with binary files. For binary files, it’s often necessary to convert or extract text-like elements.
strings can be used to extract printable text from binary files:
strings binaryfile | grep pattern
find: Finds files with specific properties matching patterns.grep: Finds lines in files that match patterns.--help: Displays usage information for many commands.man [command]: Shows the manual page for a given command.$([command]): Inserts the output of a command into another command.Feel free to ask if you need more details on any of these topics!