Before this course, install theUni-VPNand confirm that it works [estimated time: 5-10 minutes]
In this class, we will introduce the idea of the command line, executing commands without the normal graphical user interface (GUI) and accessing remote computers via ssh (Secure Shell).
8.1start terminal
On Windows or Mac, search for "terminal" in the launcher. You should see a small black box (called a "Command Prompt" in Windows) with a cursor. On Windows, you can also typecmd.exe
in the search field in the lower left corner.
Note that the cursor appears after some information indicating the file path and/or the identity of the computer. Just as RStudio has a working directory, a place to load or save files, so does the command line. This is very important, as many command line statements only work if they are executed.in the right place.
8.2Change folder ("browse directory tree")
Get the content of the current directory withdirectory
(without Windows) ols
(sin Mac).
You will get a list of contents, some of them are folders, some are files. Can you identify what is what?
You can change to a subfolder withCD folder name
. With you can move up/down the roadCD ..
You can get the current pathCD
(Windows) tuPeople with disabilities
(Mac)
This allows you to browse all the files stored on your computer. It's cumbersome and annoying compared to the normal GUI method, but it's more fundamental to the "under the hood" operation of computers, and this way of interacting with computers is necessary in some situations (including frequent access to remote computers). .
Remember that, like in RStudio, you can use TAB to complete commands.
8.3red
Computers connected to a network have an address, known asIP adress, which are always four numbers separated by periods. Are you used to dealing with domain names likesheffield.ac.uk, but these names exist for human convenience. Like the command line for individual computers, IP addresses are closer to how computer networks work.
Copy this IP into your browser and see which website it takes you to:143.167.2.102.
Your computer is also connected to the Internet, so it has an IP address. You can find out by typingipconfig
(Windows) tuifconfig
(Mac) from the command line. (You can also google "what's my IP", but that won't work for what we're going to do.) This command should provide a lot of information. Look up the IP addresses. If you are at home you can see one with the first two numbers192.168.xxx.xxx
. If you are on campus, you should see an IP in the format "172.xxx.xxx.xxx". All computers on the university network have IP addresses that start with the same number.
Now start the college VPN and find your IP again. If you're off campus, this should change, so you now have an IP starting with 172. Remember this number, we'll use it later.
8.4Ring
Computers communicate with each other in many different ways, but you are probably most familiar with web pages: the "World Wide Web" (www) that is served throughprotocolo http.
One of the easiest ways for two computers to communicate isRing
This is just a tool to check if two computers can send and receive messages. The content of the message is not important, just check that both are connected to the network.
You can ping an IP or a website. Try it on the command line
ping shef.ac.uk
You should receive a series of lines telling you how long it took your computer, in milliseconds, to receive a message to and from this site. Try somewhere further away.
ping wapo.com
(US East Coast), orping globaltimes.cn
(China continental)
But we can also ping individual computers. "sharc" is one ofhigh performance computing(HPC) in Sheffield. You can only talk to him if you are within the university network (ie you have to be on campus or activate your VPN). To try
ping sharc.shef.ac.uk
If you have your VPN enabled correctly it will work.
8.5Remote access via SSH
If two computers are connected, you can access one remotely with a command called ssh. take the form
ssh user@ip_machine
Tom will set up a machine for you to try this out in class. If it's successful, you should see something like this.
The terminal is similar, but the commands you run will now be executed on that remote computer. It is also possible that you have changed your operating system so that instead of using Mac or Windows commands, you need to use commands that work on the remote computer. Many HPC machines and web servers are Linux machines (including the one we'll connect to in class).
8.6Find files, run files, copy files
This section assumes that you are working on a remote Linux machine.
Let's use the commandThink
to search for R scripts on this remote machine. The use of * here is called a "wildcard".
find -name '*.R'
We can run R on the remote machine, but it can't run RStudio (because the terminal doesn't display graphics, just text). Running software without the GUI is called headless running software.
R
Starts an interactive R console. stop meq()
But we can also run the script without starting R (so the script runs and exits, leaving us back in the terminal. You can navigate to the containing folder and run it.
Rscripttoyscript.R
You can also pass the script to R by providing the full path
Rscript home/neo/Escritorio/toyscript.R
Find out how long the script takes to run by typing putTempo
Before
tempo Rscript toyscript.R
Now there is no point in running a simple R script remotely, but if you have a large amount of data, it might be useful to run your analysis on a more powerful remote machine (or many remote machines that allow parallel processing).
Let's copy this script from the remote machine to our home machine. For that you need thescp
command and know your username and IP address. The path where the file will be saved comes after the colon:
scp toyscript.R user@your_ip:/path/to/Desktop/toyscript.R
scp works on Linux or Mac, but not on Windows.
8.6.1trocar
We often feed additional switches (sometimes called "switches") to command line tools. then the commandspeak
speak words out loud
say hi"
we can read themTalk Documentationto see what options are available. Let's adjust the pitch and speed.
espeak -p 8 "Hello, but with extra seriousness"
espeak -s 230 "Hello my name is fast"
8.7Exercises
These exercises must be completed during the week following the class, since the remote device is not permanently on. See in Slack the IP, username and password for remote access
- Enter the remote IP in a browser; this will display a website served by the remote computer
- remote access via ssh
- Locate the index.html file
- Note: It is located in /var/www/html
- Copy to your local computer using scp
- edit it with a text editor and save it to your local computer
- copy back to server
- Look again at the IP address in a browser to see if it has changed (remember to press refresh/F5/clear cache).
8.8Check list
- Identify and open a terminal window
- Navigate the directory tree using command line instructions
- find your ip address
- Ping an IP or website
- Find files, run programs, on remote computers
- Pass options to command line tools using options
- Copy files to/from remote machines with scp
8.9Resources
- Five Reasons Researchers Should Learn to Love the Command Line
- University of Sheffield VPN
- For historical interest:At first... it was the command line(1999)Neal Stephenson
- If you want to try Linux, you can start withLinux libre