Project 1: More Linux and The Shell
Project 1: More Linux and The Shell
Now that you have a file with content that you have created, you can view its contents
using the cat command.
How about counting the number of words in your file? This can be done with the word
count command (wc), and a simple pipe. A pipe is a mechanism for passing the output
of one command to the input for another and is implemented with a | (shift left slash key).
For example
You should be provided with the number of words in the document. The wc command
has other options just use man.
Now with this knowledge we can count the number of files in a directory. This is done
with;
[prompt]$ ls /etc | wc -w
The /etc directory was chosen here simply because it has quite a few files in it. Similar
to a pipe, is a redirection using the forward arrow (>). This can be used to create a
file, and here we will create a file called test2.txt from the output of the ls command;
now try;
You should see the same word count as before with ls /etc | wc -w. Now try;
You will notice that the output scrolls off the screen. This is a common problem with
long files, and large directory listings. Of course there is also a solution the less
command. less formats the display so you can view it one page at a time. eg.
Three further commands which add very powerful pattern searching facilities to the
command line are sed, (g)awk and grep. The intricacies of these three commands are
way beyond the scope of this session but even basic use can be very powerful. More
information can be gained however, via man as usual. A quick example of grep would
be useful, so;
grep g will search the output from cat test2.txt and display any lines which contain the
letter g. Try some different greps by piping different directory listings in to get a feel for
the usefulness of the command.
In this section, and the one that follows, you will be accessing a remote system. The
address of this system is:
training.nexastem.com
All of you will be using the same log in account. The username is bioexcel2017 and
the password is guest
The first way to access remote systems that we will use is called ssh. ssh allows us to
log in to a remote system and access its shell. Using your account details, try logging
into the remote server now:
You should see the command prompt of the remote system presented to you. This is
a Linux system similar to the one you have already been working on but in a
datacenter in London. You can explore using the commands you already know but
there are no files in your home directory (yet!)
Now exit the remote shell and return to your local system:
[remote_prompt]$ exit
On your local system, create a new file with your name as the filename (ie
yourname.txt) and put some content in it (doesnt matter what it contains). Now you
will move this file to the remote system using file transfer over ssh sftp.
[prompt]$ sftp bioexcel2017@training,nexastem.com
Will present you with an sftp shell on the remote system. You can copy your local file
to the remote system with:
Your file will now also be on the remote system. Exit sftp back to your local shell with
either bye or exit
Now: login to the remote system using ssh and open your file to edit it. Add some text
of your choice, save and exit to your local machine. Now use sftp and the get
command to fetch this file back to your local machine. Open it locally and check you
have the version you edited on the remote machine.
Finally, you need to try another program for fetching remote content which we will use
in another session curl.
First, visit the webserver that is running on the remote training machine. Open a
browser and use the url http://training.nexastem.com/index.html you will see a basic
welcome page.
Now we are going to fetch that page/data to your local machine at the command line.
In your shell:
You should see the HTML code of the page appear in your terminal. You can, of
course, pipe this into a local file to keep it if you want.
Try piping this to a file, also try fetching the source code of some other web pages
elsewhere (you can gather a lot of information with curl!). Check out the command line
switches for these commands (ssh, sftp and curl) and read the man pages!