Contents

Readings: Practical Computing for Biologists: Chapter 6.

Review basic commands and server access from UConn_Unix_basics

1 Searching for file or directories using find

find .

find shows us files and directories. The power of find is in specifying “tests” or “filters”.

find . -type f
find . -type d

The above filters search for files and directories, respectively.

Search depth:
One can specify how far down the file hierarchy to go by controlling depth (first you should navigate one directory closer to root than the data-shell directory):

find ./data-shell -maxdepth 2 -mindepth 2 -type f

The above command searches for all files two directory levels within the data-shell folder.
Quick try: Try other combinations of levels and types and verify by counting number of items in output.

Let’s try matching by name:

find ./data-shell -name "*.txt"

Quick try: Combine find and grep to find the number of text files within 2 and 3 levels inside of the data-shell folder.

1.1 Exercise 1: Finding Files With Different Properties

The find command can be given several other criteria known as “tests” to locate files with specific attributes, such as creation time, size, permissions, or ownership. Use man find to explore these, and then write a single command to find all .txt files in or below the current directory that were modified in the last 168 hours.

Hint 1: you will need to use three tests: -type, -name, and -mtime

Hint 2: The value for -mtime will need to be negative - why?

2 Setting and viewing variables

variables are strings that can be assigned values. To create a variable use the following format:

var=variable # when setting variable do not use spaces

To see what the variable is you can print it to the screen with echo:

echo $var  # the '$' designates that this is a variable

*Try using echo without the dollar sign.

Whole sentences or lists can be designated as variables:

fileList=*.txt
echo $fileList
## SC_example.txt TS_example.txt Thoreau_quotes.txt Wonderful_world.txt animals.txt color-table.txt haiku.txt mm9_genes.txt newTable.txt the_raven.txt

If your variable is going to be combined with another string, make sure you surround the variable with a curly bracket. For instance:

School=MEDS
echo listing: $School
echo this class is $School_5420 # something's wrong
echo this class is ${School}_5420  # properly inserts variable
## listing: MEDS
## this class is
## this class is MEDS_5420

2.1 Setting variables using single vs. double quotes vs. back ticks

In short, double quotes allows variables within strings to be interpreted, whereas single quotes makes them literal.
Try out this example:

instructor="Michael Guertin"
echo "My instructor for MEDS5420 is $instructor"
## My instructor for MEDS5420 is Michael Guertin

or

instructor="Michael Guertin"
echo 'My instructor for MEDS5420 is $instructor'
## My instructor for MEDS5420 is $instructor

2.2 Executing commands ‘in place’ within variables

Uses for backticks - the key usually just under the escape key.
Backticks allow one to insert the result of a command in place within a command line.
One nice use for this is to set variables as outputs of commands.
Here’s an example with the command date that prints the date and time to the screen:
Compare the two following examples:

info=date
echo The date and time is: $info
## The date and time is: date

vs.

info=`date`
echo The date and time is: $info
## The date and time is: Thu Feb 9 15:54:42 EST 2023

Backticks can cause problems when using other quotations, so there is another way to run a command in place:

echo The date and time is: $(date)
## The date and time is: Thu Feb 9 15:54:42 EST 2023

3 Scripting in the shell.

Scripts are a logially ordered set of commands used to process files. They can be simple routines or complex programs. There are three main things one needs when writing scripts in general:
1. The commands themselves

2. Information about what language should be used to interpret the commands

3. Permission to execute commands in script file.

3.0.1 Text editors:

Certain text editors are designed for scripting and can recognize code. Importantly, they do not embed the document or fonts with hidden characteristics that can cause problems when running programs on you computer. There are three features that you should look for in an editor:

1. language specific highlighting

2. line number display

3. version control

MAC USERS: Download BBedit here: http://www.barebones.com/products/bbedit/download.html?ref=tw_alert: http://www.barebones.com/products/bbedit/download.html?ref=tw_alert and then install it:

Open your text editor: gedit on Linux or BBEdit or textEdit on Mac.

PC USERS: download notepad here: https://notepad-plus-plus.org/download/v7.5.8.html

Note: You can also use emacs or other command line editors such as nano or vim. These command line editors are the functional equivalent of opening a file in BBEdit, TextEdit, or NotePad. THe interface is a bit clunky and requires keyboard prompts to save, write, and exit. We will be using emacs or nano when we work on the server next time.