Author: Tracy Teal
Original contributors:
Paul Wilson, Milad Fatenejad, Sasha Wood and Radhika Khetani for Software Carpentry (http://http://software-carpentry.org/)
The shell is a program that presents a command line interface which allows you to control your computer using commands entered with a keyboard instead of controlling graphical user interfaces (GUIs) with a mouse/keyboard combination.
There are many reasons to learn about the shell.
- For most bioinformatics tools, you have to use the shell. There is no graphical interface. If you want to work in metagenomics or genomics you're going to need to use the shell.
- The shell gives you power. The command line gives you the power to do your work more efficiently and more quickly. When you need to do things tens to hundreds of times, knowing how to use the shell is transformative.
- To use remote computers or cloud computing, you need to use the shell.
Unix is user-friendly. It's just very selective about who its friends are.
- What is the shell?
- How do you access it?
- How do you use it?
- Getting around the Unix file system
- looking at files
- manipulating files
- automating tasks
- What is it good for?
- Where are resources where I can learn more? (because the shell is awesome)
The shell is already available on Mac and Linux. For Windows, you'll have to download a separate program.
On Mac the shell is available through Terminal
Applications -> Utilities -> Terminal
Go ahead and drag the Terminal application to your Dock for easy access.
For Windows, we're going to be using gitbash.
You've already downloaded and install gitbash
Open up the program.
We will spend most of our time learning about the basics of the shell by manipulating some experimental data.
Now we're going to download the data for the tutorial. For this you'll need internet access, because you're going to get it off the web.
Open the shell
Enter the command:
git clone https://github.com/tracykteal/shell-ecology.git -b gh-pages
This command will grab all of the data needed for this workshop. It's using something called git that's used for version control, but we won't talk about that here
Alternatively you can go to
https://github.com/tracykteal/shell-ecology/tree/gh-pages
And click on 'Download Zip' in the bottom right
Today we're going to go through using the command line.
These commands are in the README.md file and in Handout.md
Let's go in to that directory we just downloaded
cd shell-ecology
`cd` stands for 'change directory'
In this directory, there should be some things we just downloaded. Let's check. Type:
ls
`ls` stands for 'list' and it lists the contents of a directory.
There's a few directories there, but not too many.
Let's go look in the 'data' directory.
cd data
ls
In there, all mixed up together are files and directories/folders. If we want to know which is which, we can type:
ls -F
Anything with a "/" after it is a directory.
Things with a "*" after them are programs.
It there's nothing there it's a file.
You can also use the command
ls -l
to see whether items in a directory are files or directories. It gives a lot more information too, such as the size of the file
So, we can see that we have several files, directories and a program. Great!
Most programs take additional arguments that control their exact
behavior. For example, -F
and -l
are arguments to ls
. The ls
program, like many programs, take a lot of arguments. But how do we
know what the options are to particular commands?
Most commonly used shell programs have a manual. Let's open the manual page for ls
.
You can access the manual using the `man` program.
man ls
Space key goes forward
Or use the arrow keys to scroll up and down.
When you are done reading, just hit `q` to quit.
Programs that are run from the shell can get extremely complicated. To
see an example, open up the manual page for the find
program.
No one can possibly learn all of
these arguments, of course. So you will probably find yourself
referring back to the manual page frequently.
As you've already just seen, you can move around in different directories or folders at the command line. Why would you want to do this, rather than just navigating around the normal way?
When you're working with command line programs, which many that are are for bioinformatics or for working with large datasets you're working with your data and it's key to be able to have that data in the right place and make sure the program has access to the data. Many of the problems people run in to with command line programs is not having the data in the place the program expects it to be.
Let's practice moving around a bit.
We're going to work in that shell-ecology
directory we just downloaded.
First let's navigate there using the regular way by clicking on the different folders.
First we did something like go to the folder of our username. Then we opened 'shell-ecology'
Let's draw out how that went.
Now let's draw some of the other files and folders we could have clicked on.
This is called a hierarchical file system structure, like an upside down tree with root (/) at the base that looks like this.
That (/) at the base is often also called the 'top' level.
When you are working at your computer or log in to a remote computer, you are on one of the branches of that tree, your home directory (/home/username)
Now let's go do that same navigation at the command line.
Type
cd
This puts you in your home directory.
Mac: /Users/username Linux: /home/username PC: /home/username
##EXERCISE
- Using
cd
andls
, go in to the 'shell-ecology/data' directory and list its contents. - How many files, how many directories and how many programs are there?
Let's also check to see where we are. Sometimes when we're wandering around in the file system, it's easy to lose track of where we are and get lost.
If you want to know what directory you're currently in, type
pwd
This stands for 'print working directory'. The directory you're currently
working in.
What if we want to move back up and out of the 'data' directory? Can we just type 'shell-ecology'? Try it and see what happens.
To go 'back up a level' we need to use ..
Type
cd ..
Now do ls
and pwd
. See now that we went back up in to the 'shell-ecology'
directory. ..
means go back up a level.
##EXERCISE
Now, we're going on a file hunt.
- Move around in the 'hidden' directory and try to find the file 'youfoundit.txt'
By default, the ls
commands lists the contents of the working
directory (i.e. the directory you are in). You can always find the
directory you are in using the pwd
command. However, you can also
give ls
the names of other directories to view. Navigate to the
home directory if you are not already there.
Examining contents of directories
Type:
cd
Then enter the command:
ls shell-ecology
This will list the contents of the `shell-ecology` directory without
you having to navigate there.
The cd
command works in a similar way. Try entering:
cd
cd shell-ecology/data/hidden
and you will jump directly to `hidden` without having to go through
the intermediate directory.
##EXERCISE
- Try finding the 'anotherfile.txt' file without changing directories.
Navigate to the home directory. Typing out directory names can waste a lot of time. When you start typing out the name of a directory, then hit the tab key, the shell will try to fill in the rest of the directory name. For example, enter:
cd s<tab>
The shell will fill in the rest of the directory name for
shell-ecology
. Now go to shell-ecology/data/biology
ls su<tab><tab>
When you hit the first tab, nothing happens. The reason is that there
are multiple directories in the home directory which start with
su
. Thus, the shell does not know which one to fill in. When you hit
tab again, the shell will list the possible choices.
Tab completion is awesome.
BONUS
Tab completion can also fill in the names of programs. For example,
enter e<tab><tab>
. You will see the name of every program that
starts with an e
. One of those is echo
. If you enter ec<tab>
you
will see that tab completion works.
The cd
command takes an argument which is the directory
name. Directories can be specified using either a relative path or a
full path. The directories on the computer are arranged into a
hierarchy. The full path tells you where a directory is in that
hierarchy. Navigate to the home directory. Now, enter the pwd
command and you should see:
/home/username
which is the full name of your home directory. This tells you that you
are in a directory called username
, which sits inside a directory called
home
which sits inside the very top directory in the hierarchy. The
very top of the hierarchy is a directory called /
which is usually
referred to as the root directory. So, to summarize: username
is a
directory in home
which is a directory in /
.
Now enter the following command:
Full path
cd /home/username/shell-ecology/data/hidden
This jumps to hidden
. Now go back to the home directory (cd). We saw
earlier that the command:
Relative path
cd shell-ecology/data/hidden
had the same effect - it took us to the hidden
directory. But,
instead of specifying the full path
(/home/username/shell-ecology/data
), we specified a relative path. In
other words, we specified the path relative to our current
directory. A full path always starts with a /
. A relative path does
not.
A relative path is like getting directions from someone on the street. They tell you to "go right at the Stop sign, and then turn left on Main Street". That works great if you're standing there together, but not so well if you're trying to tell someone how to get there from another country. A full path is like GPS coordinates. It tells you exactly where something is no matter where you are right now.
You can usually use either a full path or a relative path depending on what is most convenient. If we are in the home directory, it is more convenient to just enter the relative path since it involves less typing.
Over time, it will become easier for you to keep a mental note of the structure of the directories that you are using and how to quickly navigate amongst them.
##EXERCISE
- List the contents of the /bin directory. Do you see anything familiar in there?
There are some shortcuts which you should know about. Dealing with the home directory is very common.
In the shell the tilde character, ~, is a shortcut
for your home directory.
ls ~
Try it even when you're in the shell-ecology directory:
cd
cd shell-ecology
Then enter the command:
ls ~
This prints the contents of your home directory, without you having to type the full path.
The shortcut `..` always refers to the directory
above your current directory.
ls ..
prints the contents of the directory one up from
where you are.
prints the contents of the /home/username/
. You can chain
these together, so:
ls ../../
prints the contents of /home/
.
Finally, the special directory .
always refers to your
current directory. So, ls
, ls .
, and ls ././././.
all do the
same thing, they print the contents of the current directory. This may
seem like a useless shortcut right now, but we'll see when it is
needed in a little while.
To summarize, while you are in the shell
directory, the commands
ls ~
, ls ~/.
, ls ../
, and ls /home/username
all do exactly the
same thing. These shortcuts are not necessary, they are provided for
your convenience.
This is data on a small mammal community in southern Arizona over the last 35 years. This is part of a larger project studying the effects of rodents and ants on the plant community. The rodents are sampled on a series of 24 plots, with different experimental manipulations of which rodents are allowed to access the plots.
This is a real dataset that has been used in over 100 publications. I've simplified it just a little bit for the workshop, but you can download the full dataset and work with it using exactly the same tools we'll learn about today.
We want to be able to look at these files and do some things with them.
Navigate to the ~/shell-ecology/data/biology
directory. This
directory contains our survey files and some other ones
we'll need for analyses. If we type ls
,
we will see that there are a bunch of files. A bunch of them end with .csv
The `*` character is a shortcut for "everything". Thus, if
you enter `ls *`, you will see all of the contents of a given
directory.
ls *csv
This lists every file that ends with a csv
.
We have survey data and plot and species data. If we want to just see the list of the files for the survey data we can use:
ls *survey*csv
lists every file in the current directory whose name contains the
text 'survey', and ends with csv
.
So how does this actually work? Well...when the shell (bash) sees a
word that contains the *
character, it automatically looks for filenames
that match the given pattern. In this case, it identified four such
files. Then, it replaced the *survey*csv
with the list of files, separated
by spaces.
##EXERCISE
- What happens if you do
ls survey*csv
? How is that different than 'ls surveycsv' - Do each of the following using a single
ls
command without navigating to a different directory.
- List all of the files in
/bin
that start with the letter 'c - List all of the files in
/bin
that contain the letter 'a' - List all of the files in
/bin
that end with the letter 'o'
BONUS: List all of the files in '/bin' that contain the letter 'a' or 'c'
You can easily access previous commands. Hit the up arrow. Hit it again. You can step backwards through your command history. The down arrow takes your forwards in the command history.
^-C will cancel the command you are writing, and give you a fresh prompt.
Generally ^-C is a very useful command. If you do something and you get stuck at a prompt or want to cancel what you just did, use this.
^-R will do a reverse-search through your command history. This is very useful.
You can also review your recent commands with the `history` command.
history
to see a numbered list of recent commands
You can reuse one of these commands directly by referring to the number of that command.
If your history looked like this:
259 ls *
260 ls /usr/bin/*.sh
261 ls *R1*fastq
then you could repeat command #260 by simply entering:
!260
(that's an exclamation mark).
##EXERCISE
- Find the line number in your history for the last exercise (listing files in /bin) and reissue that command.
We now know how to switch directories and look at the contents of directories, but how do we look at the contents of files?
The easiest way to examine a file is to print out all of the
contents using the program `cat`. Enter the following command:
cat surveys.csv
This prints out the contents of the `surveys.csv` file.
#EXERCISE
-
Print out the contents of the `~/shell-ecology/data/biology/plots.csv' file. What does this file contain?
-
Without changing directories, (you should still be in
shell-ecology
), use one short command to print the contents of all of the files in the/home/username/shell-ecology/data/biology
directory.
Make sure we're in the right place for the next set of the lessons. We
want to be in the shell-ecology
directory. Check if you're there with pwd
and if not navigate there. One way to do that would be
cd ~/shell-ecology/data/biology
cat
is a terrific program, but when the file is really big, it can
be annoying to use.
The program, `less`, is useful when files are big and
you want to be able to scroll through them.
less surveys.csv
`less` opens the file, and lets you navigate through it. The commands
are identical to the `man` program.
Some commands in less
key | action |
---|---|
"space" | to go forward |
"b" | to go backwarsd |
"g" | to go to the beginning |
"G" | to go to the end |
"q" | to quit |
less
also gives you a way of searching through files. Just hit the
"/" key to begin a search. Enter the name of the word you would like
to search for and hit enter. It will jump to the next location where
that word is found. Try searching the dictionary.txt
file for the
word "cat". If you hit "/" then "enter", less
will just repeat
the previous search. less
searches from the current location and
works its way forward. If you are at the end of the file and search
for the word "cat", less
will not find it. You need to go to the
beginning of the file and search.
For instance, let's search for the entry 5404
in our file.
You can see that we go right to that entry and can see
what it looks like.
Remember, the man
program actually uses less
internally and
therefore uses the same commands, so you can search documentation
using "/" as well!
There's another way that we can look at files, and in this case, just look at part of them. This can be particularly useful if we just want to see the beginning or end of the file, or see how it's formatted.
The commands `head` and `tail` let you look at
the beginning and end of a file respectively.
head surveys.csv
tail survyes.csv
The `-n` option to either of these commands can be used to print the
first or last `n` lines of a file. To print the first/last line of the
file use:
head -n 1 surveys.csv
tail -n 1 surveys.csv
We showed a little how to search within a file using less
.
We can search within files without even opening them,
using `grep`. Grep is a command-line utility for searching
plain-text data sets for lines matching a string or
regular expression.
Search for that sequence 5404 in the surveys.csv file
grep 5404 surveys.csv
We get back the whole line that had '5404' in it. What if we wanted not
just that line but the 3 entries after it as well.
grep -A 3 5404 surveys.csv
The `-A` flag stands for "after match" so it's returning the line that
matches plus the three after it. The `-B` flag returns that number of lines
before the match.
##EXERCISE
-
Search for the species "Reithrodontomy" in the species.csv file
-
Search for that species in all the csv files.
-
Search for all the records of "Reithrodontomys megalotis" in the surveys.csv file.
Hint: Use the 'species.csv' file to figure out the species code
We're excited we have all these records that are of just the Reithrodontomys megalotis species. We've identified it as a keystone species so we want to look just at those records to see when and where it was present. But all those sequences just went whizzing by with grep. How can we capture them?
We can do that with something called "redirection". The idea is that we're redirecting the output to the terminal (all the stuff that went whizzing by) to something else. In this case, we want to print it to a file, so that we can look at it later.
The redirection command for putting something in a file is `>`
Let's try it out and put all the entries that contain 'RM'
in the survys.csv in to another file called 'good-data.txt'
grep "RM" surveys.csv > good-data.txt
The prompt should sit there a little bit, and then it should look like nothing
happened. But type `ls`. You should have a new file called good-data.txt. Take
a look at it and see if it has what you think it should.
Note: The '>' command will write over any file that's already there without asking.
>> will append to the end of an existing file.
There's one more useful redirection command that we're going to show, and that's
called the pipe command, and it is |
. It's probably not a key on
your keyboard you use very much. What |
does is take the output that
scrolling by on the terminal and then can run it through another command.
When it was all whizzing by before, we wished we could just slow it down and
look at it, like we can with less
. Well it turns out that we can! We pipe
the grep
command through less
The pipe command '|' takes the output of the first
thing and then puts it in to the second part
grep "RM" surveys.csv | less
Now we can use the arrows to scroll up and down and use q
to get out.
We can also do something tricky and use the command wc
. wc
stands for
word count
. It counts the number of lines or characters. So, we can use
it to count the number of lines we're getting back from our grep
command.
And that will magically tell us how many sequences we're finding. We're
grep "RM" surveys.csv | wc
That tells us the number of lines, words and characters in the file. If we
just want the number of lines, we can use the -l
flag for lines
.
grep "RM" surveys.csv | wc -l
Redirecting is not super intuitive, but it's really powerful for stringing together these different commands, so you can do whatever you need to do.
The philosophy behind these command line programs is that none of them
really do anything all that impressive. BUT when you start chaining
them together, you can do some really powerful things really
efficiently. If you want to be proficient at using the shell, you must
learn to become proficient with the pipe and redirection operators:
|
, >
, >>
.
Now we can move around in the file structure, look at files, search files, redirect. But what if we want to do normal things like copy files or move them around or get rid of them. Sure we could do most of these things without the command line, but what fun would that be?! Besides it's often faster to do it at the command line, or you'll be on a remote server like Amazon where you won't have another option.
The stability.files file is one that tells us what sample name goes with what sequences. This is a really important file, so we want to make a copy so we don't lose it.
Lets copy the file using the cp
command. The cp
command backs up the file. Navigate to the data
directory and enter:
The copy command 'cp' let's you copy files
cp surveys.csv surveys_raw.csv
Now surveys_raw.csv
has been created as a copy of surveys.csv
.
Let's make a raw
directory where we can put this file.
The `mkdir` command is used to make a directory. Just enter `mkdir`
followed by a space, then the directory name.
mkdir raw
We can now move our backed up file in to this directory.
We can move files around using the command `mv`. Enter this command:
mv surveys_raw.csv raw/
This moves surveys_raw.csv
into the directory raw/
or
the full path would be `~/shell-ecology/data/biology/raw'
The mv
command is also how you rename files. Since this file is so
important, let's rename it:
mv surveys.csv surveys_IMPORTANT.csv
Now the file name has been changed to surveys_IMPORTANT.csv. Let's delete the raw file now:
rm raw/surveys_raw.csv
The `rm` file removes the file. Be careful with this command. It doesn't
just nicely put the files in the Trash. They're really gone.
##EXERCISE
Do the following:
- Rename the
surveys_IMPORTANT.csv
file tosurveys_new.csv
. - Create a directory in the
biology
directory callednew
- Then, copy the
surveys_new.csv
file intonew
By default, rm
, will NOT delete directories. You can tell rm
to
delete a directory using the -r
option. Let's delete that new
directory
we just made. Enter the following command:
rm -r new
We've been able to do a lot of work with files that already exist, but what if we want to write our own files. Obviously, we're not going to type in a whole survey file, but you'll see as we go through other tutorials, there are a lot of reasons we'll want to write a file, or edit an existing file.
To write in files, we're going to use the program nano
. We're going to create
a file that contains the favorite grep command so you can remember it for later. We'll name this file
'awesome.sh'.
The program 'nano' can be used to write files
nano awesome.sh
Now you have something that looks like
Type in the command
grep "RM" surveys.csv > good-data.txt
so it looks something like
To save the file and exit. At the bottom of nano, you see the "^X Exit". That
means that we use Ctrl-X to exit. Type `Ctrl-X`. It will ask if you want to save it. Type `y` for yes.
Then it asks if you want that file name. Hit 'Enter'.
Now you've written a file. You can take a look at it with less or cat, or open it up again and edit it.
##Exercise
Open 'awesome.sh' and add "echo AWESOME!" after the grep command and save the file.
We're going to come back and use this file in just a bit.
Commands like ls
, rm
, echo
, and cd
are just ordinary programs
on the computer. A program is just a file that you can execute. The
program which
tells you the location of a particular program. For
example:
which ls
Will return "/bin/ls". Thus, we can see that ls
is a program that
sits inside of the /bin
directory. Now enter:
which find
You will see that find
is a program that sits inside of the
/usr/bin
directory.
So ... when we enter a program name, like ls
, and hit enter, how
does the shell know where to look for that program? How does it know
to run /bin/ls
when we enter ls
. The answer is that when we enter
a program name and hit enter, there are a few standard places that the
shell automatically looks. If it can't find the program in any of
those places, it will print an error saying "command not found". Enter
the command:
echo $PATH
This will print out the value of the PATH
environment variable. More
on environment variables later. Notice that a list of directories,
separated by colon characters, is listed. These are the places the
shell looks for programs to run. If your program is not in this list,
then an error is printed. The shell ONLY checks in the places listed
in the PATH
environment variable.
Navigate to the shell-ecology/data
directory and list the contents. You will
notice that there is a program (executable file) called hello.sh
in
this directory. Now, try to run the program by entering:
Running programs
cd shell-ecology/data
hello.sh
This won't work because the shell isn't looking
in the right place for it.
You should get an error saying that hello.sh cannot be found. That is
because the directory /home/username/edamame-data/shell
is not in the
PATH
. You can run the hello.sh
program by entering:
./hello.sh
This will work, becuase we told it to look in this
directory '.' for the program.
Remember that .
is a shortcut for the current working
directory. This tells the shell to run the hello.sh
program which is
located right here. So, you can run any program by entering the path
to that program. You can run hello.sh
equally well by specifying:
The program can also be run by using the full path
/home/username/shell-ecology/data/hello.sh
Or by entering:
~/shell-ecology/data/hello.sh
When there are no /
characters, the shell assumes you want to look
in one of the default places for the program.
We know how to write files and run scripts, so I bet you can guess where this is headed. We're going to run our own script!
Go in to the 'MiSeq' directory where we created 'awesome.sh' before. Remember we wrote our favorite grep command in there. Since we like it so much, we might want to run it again, or even all the time. Instead of writing it out every time, we can just run it as a script.
It's a command, so we should just be able to run it. Give it try.
./awesome.sh
Alas, we get -bash: ./awesome.sh: Permission denied
. This is because we haven't told
the computer that it's a program. To do that we have to make it 'executable'. We do this
by changing its mode. The command for that is chmod
- change mode. We're going to change the mode
of this file, so that it's executable and the computer knows it's OK to run it as a program.
To run a program, you have to set the right permissions, make it
executable rather than just a text file.
chmod +x awesome.sh
Now we can run the program
./awesome.sh
Now you should have seen some output, and of course, it's AWESOME!
Congratulations, you just created your first shell script! You're set to rule the world.
Shell cheat sheets:
- http://fosswire.com/post/2007/08/unixlinux-command-cheat-sheet/
- https://github.com/swcarpentry/boot-camps/blob/master/shell-ecology/shell_cheatsheet.md
Web sites where you can see what the different components of a shell command are doing.
Tutorials:
- Software Carpentry tutorial - The Unix shell
- Bash HowTo
- man bash
- Google - if you don't know how to do something, try Googling it. Other people have probably had the same question.
- Learn by doing. There's no real other way to learn this than by trying it out. Write your next paper in nano, open pdfs from the command line, automate something you don't really need to automate or actually do.