0% found this document useful (0 votes)
77 views

Project 1: More Linux and The Shell

This document provides instructions on using Linux shell commands to work with files locally and remotely. It discusses using pipes, redirections, word counts, file transfers over SSH, and fetching web pages using CURL. The goal is for students to gain experience using commands like cat, ls, grep, less, ssh, sftp and curl to view files, transfer files between local and remote systems, search file contents, and retrieve web pages from a remote server.

Uploaded by

Roshan Shrestha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Project 1: More Linux and The Shell

This document provides instructions on using Linux shell commands to work with files locally and remotely. It discusses using pipes, redirections, word counts, file transfers over SSH, and fetching web pages using CURL. The goal is for students to gain experience using commands like cat, ls, grep, less, ssh, sftp and curl to view files, transfer files between local and remote systems, search file contents, and retrieve web pages from a remote server.

Uploaded by

Roshan Shrestha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Project 1: more Linux and the shell

Purpose of this practical

This practical extends yesterdays introduction to GNU/Linux shell commands. It


provides some instruction, but with the expectation you can begin to start to work on
your own more using available help (man command and/or internet) and the support
of the mentors.

Conventions used in this practical

As before, this practical guide uses the following typeface conventions:


Text in arial italics describes as action for you to do. ie. press return
Text in courier shows what you should see from the system on screen. ie. login:
Text in arial bold shows commands or text you should type. ie. USERNAME

Before you start:


Either find the file you created yesterday, containing a paragraph of text or create a
new one to use in this session in the same way as you did yesterday.

1. Pipes, Redirections and more Useful Commands

Now that you have a file with content that you have created, you can view its contents
using the cat command.

[prompt]$ cat <filename>

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

[prompt]$ cat <filename> | wc -w

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;

[prompt]$ ls /etc > test2.txt

now try;

[prompt]$ cat test2.txt | wc -w

You should see the same word count as before with ls /etc | wc -w. Now try;

[prompt]$ cat test2.txt

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.

[prompt]$ cat test2.txt | less

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;

[prompt]$ cat test2.txt | grep g

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.

2. Accessing remote systems

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:

[prompt]$ ssh -l bioexcel2017 training.nexastem.com

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:

[prompt_sftp>] put yourname.txt

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.

Congratulations (hopefully) logging in to remote machines and moving files across


is a key skill!

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:

[prompt]$ curl http://training.nexastem.com/index.html

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!

3. To finish - a treasure hunt challenge

Use curl to get the instructions from training.nexastem.com/treasureHunt.html

You might also like