1 Background

This tutorial provides an introduction to Bash and the Linux Command Line. It will focus on skills to navigate and manage files (the R workshops will cover more complex topics like scripting). Our objective is (1) to furnish you with reference material in case one day you have to interact with the Linux system and (2) to provide you the essential skills for using bash in case you want to dwell further into it.

The methodology of this workshop section consists mainly in walking you through some of the essential terminal commands. We will also solve some basic exercises that combine and put these skills into context.

If you are working in a windows system please download and install git bash. You can download it from: https://git-scm.com/downloads. Install with the default configuration.

The directories names presented in these examples are names from my computer. You can adapt these commands with the directories names in your computer.

2 Declaring variables and printing text to the screen

Printing text to the screen (the standard output, often shortened as stdout, is the exact technical term) and creating variables are two most basic operation in bash.

The echo command prints text to the screen.

Variable declaration is done using the = sign. The variable name is placed to the left of =, while the assigned value goes to the right. In bash, variables names must be unique (using the same variable name twice will override it) and cannot start with a number or contain spaces. Variables can store numeric, strings, or booleans (true/false) values. For the moment, we will focus numbers and strings.

  1. Print text to the screen
echo 'Hello World'
  1. Declare a numeric variable and print it to the screen
var1=1
echo $var1
  1. Declare a string variable and print it to the screen
var2='Hello world'
echo $var2
  1. Variables and strings can be combined using the ${var}. This is called “disambiguation”. It tells bash that whatever is contained inside the {} is a variable and should be read a such.
name='Jorge'
echo "${var2} my name is ${name}"
  1. Attempt the previous piece of code with ' instead of ". What happens?
% echo '${var2} my name is ${name}'

Double quotes ” are neccesary to “disambuguate” the variable.

5 Creating directories and files

The command mkdir can be used to create new directories. Directories are essential for keeping data organized.

  1. Create a new directory called TestDirectory, and use cd to move into it as explained before.
mkdir TestDirectory
cd TestDirectory
pwd

Information can be printed into a file using >, while cat is used to observe the contents of a file. After moving into TestDirectory, put some simple information into a file using >, then retrive that information to the stdout with cat.

  1. echo some information into a file called simple.txt, then use cat to observe the contents of the file.
echo "This is a text file" > simple.txt
cat simple.txt
  1. Exercise Attempt the using echo on simple.txt. What happens?
echo simple.txt

Sometimes the file is too large to be visualized with cat, instead you can use less or zless if the file is zipped.

  1. explore the file Dd2_pos1_S85_L001_R1_001.fastq.gz
cd ../
zless Dd2_pos1_S85_L001_R1_001.fastq.gz

6 Copying and Moving files

We are going to create a BackupDirectory for TestDirectory.

  1. Move back one level with cd and use mkdir to create BackupDirectory
mkdir BackupDirectory
pwd
ls
  1. Use mv to move the simple.txt file into BackupDirectory
mv TestDirectory/simple.txt BackupDierctory/
ls BackupDirectory/
  1. Use cp to copy the simple.txt file into TestDirectory

6.1 Deleting files and directories

The command rm can be used to delete files. It is recommend to always append the -i flag when deleting files. -i will prompt you to confirm whether or not you want to delete the file (in bash there is no going back once a file is deleted!). rm also deletes directories. To delete directories, append the -r flag, which stands for recursively.

22 Remove the simple.txt file

cd TestDirectory
rm simple.txt
ls
  1. Move one level up and remove TestDirectory
cd ..
rm -r TestDirectory