


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























This self-paced tutorial will take you through some of the key concepts of the BASH command line shell. There are also many tutorials available on the Web or in books. I use this style for commands you type, and this style for file- names. In the command summary, I use this style for things that should be substi- tuted with an appropriate filename or word.
1 Why do I care about this? 3
2 Introduction — what is the Linux/BASH command line? 3 2.1 What is Linux?.................................. 3 2.2 What is the BASH command line?...................... 4
3 Accessing the BASH command line 5 3.1 If you are using Linux............................... 5 3.2 If you are using a Mac............................... 5 3.3 If you are using Windows............................. 5
4 Directories and Folders 6 4.1 Listing files.................................... 6 4.2 Changing the current working directory................... 7
5 Finding out more about files 8
6 Using the shell 8 6.1 Recalling previous commands......................... 8 6.2 Typing less.................................... 9 6.3 Cut and Paste.................................. 9
7 Organizing files into subdirectories 9
8 Grabbing files from the Internet and moving files around 10 8.1 Grabbing files.................................. 10 8.2 Moving files................................... 11
9 File archives 11
Linux is a ‘flavour’ of a group of operating systems known as ‘unix’. Unix is a true multi-tasking, multi-user operating system. ‘Multi-tasking’ means that it is able to run multiple programs at the same time — if the computer has multiple CPU cores, then it can actually do multiple things at once (otherwise it switches rapidly between different tasks); ‘multi-user’ means that many users can log onto the same computer at the same time from separate ‘terminals’. Each user is given an area of disk space in which they can create their own files (a ‘home directory’). These days, if you are a Mac user, then you are using a flavour of unix (perhaps without knowing it) as OS/X is built on a version of unix called Darwin which, in turn, is based on BSD unix. So unix-based operating systems can have beautiful graphical user interfaces (GUIs). The term ‘unix’ or ‘Linux’ actually refers to what is known as the ‘kernel’ of the operating system — in other words all the deep and complicated things that go on to allow a computer to run software, access files on disk or on a network, etc. Everything else (including the graphical interface, or ‘window manager’ and even the command line itself), is actually a program that runs on top of the operating system. This means that people can package the Linux kernel with different sets of tools to produce a different Linux ‘distribution’. There are several of these, some of the most popular being Ubuntu, Fedora, RedHat and CentOS. The beauty of this arrangement is that people have a huge amount of flexibility to make a Linux-based system look and feel as they want it to. For example, you might want a flashy Mac or Windows style GUI with windows that slurp down into an icon when you shrink them or have 3D effects on your really powerful desktop computer, but you might want something much simpler on the laptop that you bought 10 years ago for £250.00. If you are running a server, then you probably don’t need a GUI at all so you don’t install one! You don’t get that flexibility with Windows or a Mac.
You are almost certainly familiar with using ‘Explorer’ on Windows or ‘Finder’ on a Mac. This is a graphical user interface that allows you to browse through the hierarchy of folders that you have on your computer, create new folders, drag files from one folder to another and view the contents of files by starting an appropriate program (such as Microsoft Word when you click on a Word document). Linux has a whole host of similar directory manager programs. However in all three environments (Windows, Mac, Linux) you can carry out all the same functions from the command line. The command line is a prompt at which you type commands (or the names of programs) that you want to run, together with required and optional parameters to tell the program what to process and to tweak the details of how it should be processed. Under Linux, even the default command line can be slightly different between dis- tributions (but you can choose the one you want). The ‘shell’ is the program that provides a prompt and lets you type commands. It then interprets those commands and runs other programs as required. When these programs have finished, the shell shows the prompt again waiting for you to do something. Here we will introduce the most common command line shell which is known as BASH. The BASH shell is also the default command line on a Mac and can be used under Windows (as described below).
3 Accessing the BASH command line
Search for an application called ‘Terminal’. If you can’t find it then look for ‘xterm’, ‘Command Line’ or ‘Command Prompt’. The terminal is actually just a window that runs the ‘shell’. It is most likely that your terminal will run the BASH shell and the prompt may look like:
bash-4.2$
in which case you know you are using the BASH shell. If you don’t have a prompt containing the word ‘bash’, then:
1: Type the following:
echo $SHELL
You should find that it says something like:
/bin/bash
If this doesn’t contain the word ‘bash’ then ask for some help in making BASH your default shell^3.
You can access the command line using the ‘Terminal’ application which lives in the ‘/Applications/Utilities/’ folder. To find it, go to your ‘Applications’ folder. Near the bottom, there is a folder called ‘Utilities’. The terminal is actually just a window that runs the ‘shell’, but the default shell is BASH which is what we want.
Things are a bit more difficult^4. The easiest way to access a BASH shell is to in- stall a package called ‘git-bash’. First check to see if ‘git-bash’ is already installed: type git-bash into the search box. If not, you can download this from https:// git-for-windows.github.io/
Task: Install any necessary software and open a command line prompt.
(^3) The command chsh bash may work for you; if not then you really do need to ask for help! (^4) If you are running Windows 10 with the ‘Anniversary Update’ then you can run the ‘Windows Subsystem for Linux’ which provides you with a Linux BASH environment running under Windows (see https://www.howtogeek.com/265900/ everything-you-can-do-with-windows-10s-new-bash-shell/). However, we have found this to be unreliable and not to allow access to the internet or to files downloaded under the normal Windows environment. At the time of writing, this is not recommended!
4: Type the following:
ls /
In this example we are looking at the root (top) directory.
5: Type the following:
ls /etc
Now we are looking in the etc sub-directory of the root directory (this happens to be where system configuration files live). Specifying a directory or file starting with the root directory (‘/’) is known as specifying the ‘full path’. Each element of the full path of directories is separated with a ‘/’, so we could refer to a file such as /etc/fstab (see Figure 1).
The cd (‘Change Directory’) command is used to change the current working direc- tory.
6: Type the following:
cd / ls
This has the same effect as ls / but this time you have changed the current direc- tory to the root directory and then obtained a listing. To return to your home directory, type cd by itself.
7: Type the following:
cd ls
Note that the ∼ is an abbreviation for your home directory so you can also:
8: Type the following:
cd ~ ls
You will see why this is useful later. You can find out which directory you are currently in by using the pwd command. This stands for ‘Print Working Directory’ and can be thought of as a ‘where am I?’ command:
9: Type the following:
pwd
The name of the home directory will depend on the system you are using. It might be something like /home/andrew or /c/Users/andrew or /n/ if you are on a network drive.
Task: Remembering that the directory above the current one is always available as ‘..’, how do you think you can move up one level in the tree of directories? Give it a go and use pwd to check where you are. Return to your home directory using cd.
5 Finding out more about files
We have already seen the ls command to list the files in a directory and the -a option (also referred to as a ‘flag’ or ‘switch’) that allows us to see hidden files. There are also other options to give more information.
-l Long format — give information about file sizes, permissions, etc. For the moment, don’t worry about the details other than the file size (in the 5th column), the date and time (in the 6th-8th columns) and the filename (at the end).
-t Sort by time — newest files first
-r Reverse the sort, so -t -r will give the newest file last which is often more useful
-h Give file sizes in a more human readable (kilobytes, gigabytes etc.)
These one-letter options can be combined into one as shown in this example:
10: Type the following:
ls -ltrh
Task: Work out what the previous command has done. Play with cd and ls and its options to explore the files on your computer.
6 Using the shell
You have probably found yourself typing the same, or very similar commands several times. Wouldn’t it be nice if you could recall those previous commands and modify them if required? Well you can.
Task: Use ls to see your new sub-directory within your home directory. Navigate to your new directory using cd but try specifying the sub-directory name by just typing c and using the Tab key. Then look to see what is in there using ls. Are there any hidden files? Finally move back up to your home directory. What are the different ways of getting back to your home directory?
You may remember that ∼ is an abbreviation for your home directory, so to get to the cli directory, rather than typing cd followed by cd cli , you can simply:
12: Type the following:
cd ~/cli
8 Grabbing files from the Internet and moving files
around
Let’s grab a file to look at from the Internet. Note that this file (pdbs.tgz) is only an example file. There are three ways of grabbing a file:
Depending on the system you are using you will have one (or both) of curl and wget installed — they are equivalent command line tools that allow you to grab files from the internet.
To use curl :
13: Type the following:
cd curl -O http://www.bioinf.org.uk/teaching/cli/pdbs.tgz
Note! That is a capital letter ‘O’, not the digit zero (0). To use wget :
14: Type the following:
cd wget http://www.bioinf.org.uk/teaching/cli/pdbs.tgz
Task: Having downloaded the file, do a directory listing to check that it is there and how big it is.
When you looked at the directory listing you should have realized we made a (de- liberate) mistake. We should have put the file in our cli directory not in our home directory. We can use the mv (short for ‘MoVe’) command to move the file into that directory:
15: Type the following:
mv pdbs.tgz cli
The mv command can also be used to rename files — if the second parameter is a name that isn’t a directory, the file (given in the first parameter) will be renamed to the name you specified. Be careful! If this second parameter is the name of another file that exists already, then that file will be over-written!
9 File archives
The file you have downloaded is in a format known as a ‘gzipped tar file’. This is very similar to a ZIP file which you are probably familiar with. It is a compressed archive containing several other files all bundled together. tar stands for ‘Tape ARchiver’ — it was originaly used for performing backups to tape, but can also create (or unpack) archive files on disk. Navigate to your cli directory and unpack the archive file.
16: Type the following:
cd ~/cli tar xvzf pdbs.tgz
The four letters after the tar command tell it what to do:
x Extract files
v Be verbose (the program tells you what it’s doing)
z The tar file has been compressed with a program called gzip
f The archive is a file (the name of which follows) rather than coming from a tape
These options can appear in any order except that the ‘ f ’ must come last.
Task: Find out what files or directories you have now. You should find a new directory called pdbs has been created, so change to that directory and see what is in there.
Be very careful with the rm command — unlike a graphical file browser it doesn’t put the file into a ‘trashcan’ from which it can be recovered. Once it’s gone, it’s gone! If we wished to concatenate all the files that end in .ent we can use a ‘*’ which matches any characters to save typing all the filenames^7.
22: Type the following:
cat (^) *.ent > all.pdb
Task: Use ls to verify that the all.pdb file is of an appropriate size that it includes all the files and then remove the all.pdb file.
We won’t look at it now, but you can also append to an existing file using redirec- tion. In this case we simply use >> rather than >.
Once you have a file, you often need to copy it — perhaps so you can keep a version before you make some changes. You may not realise it, but you have already seen a way you can do this using cat and redirection.
Task: Think about how you could use cat to copy the pdb9aat.ent file to another name and give it a try. Delete the copied file once you have finished.
However there is an easier way of copying a file using the cp (‘CoPy’) command:
23: Type the following:
cp pdb9aat.ent 9aat.pdb ls
Note that file extensions are not as important in Linux as they are in Windows. More precisely, file extensions are not so important when you are using the command line rather than a GUI to access files. When you click on a Word document in Windows or on a Mac, the GUI looks at the extension of the file (.doc or .docx) and knows to use the Word software to open the document. The file extension doesn’t really say anything about the content of the file; rather it tells the GUI which program to use to open the file. In contrast, when you use the command line you always have to specify which program to use to access a file.
(^7) We could also do something like pdba.ent if we wanted only those files that have an ‘a’ in the
PDB code and more complex patterns are also possible.
Now we have two copies of the same file: one called pdb9aat.ent and one called 9aat.pdb. If we have two files and want to know if they are the same, or perhaps we have modified one of them and want to know what the differences between the two versions are, we can use the diff (‘DIFFerences’) command to compare them.
24: Type the following:
diff pdb9aat.ent 9aat.pdb
This command should return nothing. This indicates that there are no differences between the files. If there had been differences, these would have been displayed; we will see and example in a moment.
cat is all very well, but it’s not very useful for big files as the content zooms past on the screen too quickly to see.
Often it is useful just to be able to see the top of a file where there might be some comments or identifying information. Suppose we are interested in the first 20 lines of the file pdb9aat.ent or the ‘head’ of the file:
25: Type the following:
head -20 pdb9aat.ent
The -20 part says that we want the first 20 lines. Similarly to see the end of the file, or its ‘tail’, we use the tail command:
26: Type the following:
tail -20 pdb9aat.ent
Task: Remembering what you learned about using > for redirection, create a file called pdb9aat.top containing just the top 25 lines of pdb9aat.ent. Use cat to verify that this file contains what you expect.
Let’s verify that there really are 25 lines in the file you just created. There is a command for doing a ‘Word Count’ with the rather unfortunate name wc.
27: Type the following:
wc pdb9aat.top
11 Input and output: Redirection and pipes
When you did the diff between the two files that you created each containing the top 20 lines of one of the PDB files, you should again have found that the results rushed past and you couldn’t see the top of the file. You should already know one solution to that problem.
Task: Run the diff command again, but use the > symbol to redirect the output to a file called differences.txt. Now use less to look at the file.
However this is not altogether convenient as you end up creating files that you will then delete almost immedately. Unix-like operating systems provide a concept known as ‘pipes’ that allow the output of one program (such as diff ) to be fed directly into another program (such as less ). We have already seen the idea of ‘redirection’ of the output from a program. Many programs default to sending their output to what is known as ‘standard output’ (or stdout) and, by default this is the screen. In examples you have seen previously, you used > to redirect standard output to a file instead. Many commands such as less will take input from a file specified on the com- mand line, but if no file is specified, input will be taken from ‘standard input’ (stdin). By default, standard input is the keyboard, but we can use the < operator to redirect standard input to be a file. For example, we previously saw the command cat to display a whole file on the screen:
31: Type the following:
cat pdb9aat.ent
The cat program has seen that there is a filename specified on the command line so knows that it should take its input from there. If you try typing cat by itself, it will expect input from standard input (the keyboard) instead.
32: Type the following: cat
The cat program is now sitting waiting for input from the keyboard. So let’s type a couple of lines and finish with Ctrl-d (press and hold the Ctrl key, press and release ‘ d ’, then release Ctrl ) which is used to indicate the end of a file (i.e. no more text is coming from standard input).
33: Type the following: hello world Ctrl-d
(Remember Ctrl-d does not mean that you type the letters C , t , r , l , - , d !) Note what happened: cat reads from standard input (the keyboard) and writes to standard output (the screen). A little variation of that is a very quick way of creating a file. Let’s create a file called helloworld.txt containing the two lines: Hello and World:
34: Type the following: cat > helloworld.txt Hello World Ctrl-d
Task: Check the file has been created and check its content with cat. Once you are done, remove the helloworld.txt file.
This is actually a very useful way of creating very small files quickly. To create larger files or modify the content of files, you use what is known as a ‘text editor’ which is like a word processor, but just creates simple plain text files without being able to change fonts, sizes, bold, italics, etc. We will not look at text editors in this session although Section 24.1 gives you a brief introduction to emacs , one of the most powerful text editors. We can use the < operator to redirect standard input so that it comes from a file instead of from the keyboard.
35: Type the following:
cat < pdb9aat.ent
In practice this is exactly the same as typing cat pdb9aat.ent (without the <), but underneath something slightly different is happening.
You can probably see how redirection to a file is very useful, but are probably won- dering why redirection from a file is useful. In general it isn’t used very much, but the concept of standard input is very important. Wouldn’t it be wonderful if we could take the output of our diff program that created too much to see on the screen in one go and send it to the less program directly to be able to page through it rather than hav- ing to go via an intermediate file. In other words, we want to send the standard output of one program (which would normally go the screen) directly to the standard input (which would normally be the keyboard) of another program. Pipes let us do that. A pipe is created using the symbol | (which is probably bottom left on your keyboard, next to the left shift button). Let’s take a simple example first:
13 Getting more clever
In the associated lecture we talked about a basic algorithm for counting the number of amino acids in a PDB file. If we understand the power of BASH we don’t actually need a program to do this; we can do it all using standard BASH commands. This is the real power of unix-like command-line shells rather than GUIs.
The slightly oddly named grep command (‘Globally search a Regular Expression and Print’) allows us to search a file for lines that match a specified pattern and print them. Let’s look at one of our PDB files and see what it contains:
42: Type the following:
cd ~/cli/pdbs less pdb9aat.ent
Page through the file using the space-bar and press ‘ q ’ to quit when you are done. You will see that each record (line) starts with a field (e.g. HEADER. REMARK, SEQRES) that says what the record contains. As you move through the file, you will find that the records that start with ATOM are the atomic coordinates. These are the records we need if we want to count the amino acids. Using grep we can find all the records that contain the word ATOM:
43: Type the following:
grep ATOM pdb9aat.ent
Task: Use your knowledge of ‘pipes’ to send the output of this grep command into less or into head to view the first 20 lines.
Looking at the beginning of the output from grep , you should notice that the first few lines do indeed contain ATOM, but not at the start of the line. We are only interested in lines that start with ATOM, so we tell grep to anchor the search to the start of a line using the ‘caret’ or ‘hat’ character (^):
44: Type the following:
grep ^ATOM pdb9aat.ent
Task: Again, use your knowledge of pipes to send the output of this grep command into less or head
You should find that the command is now doing the right thing and only returning lines that start with ATOM. Sending this output into less is all well and good, but what we eventually want to know is the number of amino acids. As a step towards that we can use the word count program that we used before ( wc ) to obtain the number of ATOM records which will be the total number of atoms in the file.
Task: Use a pipe to send the output of this grep command into wc to count the lines.
You should find there are 6322 atoms. Remembering from the lecture that we can count the amino acids by looking for the Cα atom (which is called CA in the PDB files), we can send the output of our first grep command (that found the word ATOM) via a pipe into another grep command that looks for CA and then send that via another pipe into wc to count the number of amino acids. grep is another of these programs that will take input from standard input if a filename isn’t specified, so your second grep command will simply be grep CA (with a pipe symbol either side of it).
Task: Give it a try:
You should find you have 802 amino acids. If you wanted to find out the total number of amino acids in all the PDB files, you could simply replace the pdb9aat.ent with *.ent
Task: Give it a try — you should find there are 2117 amino acids in total.