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.
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.
echo 'Hello World'
var1=1
echo $var1
var2='Hello world'
echo $var2
${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}"
' instead of
". What happens?% echo '${var2} my name is ${name}'
Double quotes ” are neccesary to “disambuguate” the variable.
The command mkdir can be used to create new directories.
Directories are essential for keeping data organized.
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.
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
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.
Dd2_pos1_S85_L001_R1_001.fastq.gzcd ../
zless Dd2_pos1_S85_L001_R1_001.fastq.gz
We are going to create a BackupDirectory for
TestDirectory.
cd and use mkdir
to create BackupDirectorymkdir BackupDirectory
pwd
ls
mv to move the simple.txt file into
BackupDirectorymv TestDirectory/simple.txt BackupDierctory/
ls BackupDirectory/
cp to copy the simple.txt file into
TestDirectoryThe 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
TestDirectorycd ..
rm -r TestDirectory