-
Notifications
You must be signed in to change notification settings - Fork 84
Introduction to Linux: Labs
Welcome to the Introduction to Linux lab!
This time, we're going to discover how to use terminal commands to get work done. Let's get started!
Hit the keys ctrl
+alt
+t
to open a terminal. You could hit the icon at the top of the screen, but using the keyboard is quicker and easier.
Firstly, we're going to discover the man
command. man
shows a manual page for a certain topic. Type man pwd
<enter>
to see what the pwd
command does. Use the ⬆️ and ⬇️ keys to scroll around the page, or hit the spacebar to skip a whole page.
Once you've found out what pwd
does, hit q
to exit the man system. Now, type pwd
again: what does the output tell you?
Type cd /
<enter>
, then try pwd
again.
- What happened? What does
cd
do? (hint: there's no 'man page' becausecd
is so simple!) - Use
cd ~
to go back to the directory you were in. This is your home directory - it's where all your stuff goes. You can always get back here by typingcd ~
. - type
cd ..
, then check where you are withpwd
. What does..
mean? (hint: it's in every folder!)
Type ls
.
- What does
ls
do? - Type
ls -a
. Read the manpage to find out what the a switch does. What's the difference betweenls
andls -a
? -
Without using
cd
, show all the files and directories in the root directory (remember "/"?) -
Without using
cd
, show all the files and directories in the parent directory (remember ".."?) - What do the switches
l
,r
, andt
do? What does the commandls -lart
do? Why is it useful?
What does ps
do? Try it!
Type cd ~
to get back to your home directory.
Open the pico
editor.
Type some text! Say hello, write your name, where you come from, what you like to do in your spare time. Write two paragraphs about yourself.
- At the bottom of the screen, you'll see all the commands you can use. Sometimes you'll see
^
- this just meansctrl
. So for example,^O
just meansctrl
+o
. - Use the WriteOut command to save your work (name the file with your first name), then the Exit command to quit pico. You'll be right back at the command prompt.
- My file is called
sam
. What happens when I typecat sam
in the command prompt? Try that with the name of your file. Use the manpage for cat to figure out what's happening. - What happens when I type
less sam
? What'sless
? (Typeq
to escape!). Do you recogniseless
? It's how manpages are shown on the screen! - What happens when I type
head -1 sam
? What abouttail -1 sam
?
-
whoami
prints out your user ID! -
ps aux
prints details of every process running on your machine. -
grep
searches the input for lines matching a certain pattern. For example,grep cheese sam
prints out all the lines in the file called sam that contain the exact word "cheese". - Grep is very powerful.
grep ^I sam
prints out all the lines in the file sam that start with the character "I". - What do you think
grep UK$ sam
does? - You can use the backticks (`) to execute a command and pass its output to the outer command. This sounds complex but it's really simple. If I type
grep `whoami` sam
thewhoami
command is executed first. That returns "sam". That output is then substituted into the command, sogrep sam sam
is executed. See? Easy!
Some commands can be combined together. For example, wc
is a simple command that shows a wordcount for a file - the number of characters, lines, and words in the file.
- Type
wc
<enter>
. Then, type a bunch of text. You're actually writing a file into wc right now! Hit enter and enter some more text. Do it some more. When you're done, hitctrl
+d
to end the "file". What do the three numbers mean? - What does
wc -l
do? Enter it as a command and then do the same - what does the one number mean? - Remember my file called
sam
? What happens when I entercat sam | wc -l
? What is the "pipe" doing? - Try it with your file. How many words are in your file? How many lines of text?
- Use
ls
andwc
to find out how many files and directories are in yourroot
directory. - How many files and directories are in your
home
directory? - Enter
ls > files
. Then entercat files
. What happened? What isfiles
? What does>
do? - Enter
wc -l < files
. What does the number mean? -
tee
is very useful. Before passing its input through to its output, it also writes the input to a file.
You can save information in the shell using environment variables. Type the following:
message="hello, my name is sam"
echo $message
Make sure to leave no spaces around the =
, and remember the $
before "message" - this is the name of the variable. They can be used by processes that you write, and contain useful information that the shell uses. We'll see more variables when we come to use Heroku next week.
As well as having an input and an output, each process returns either zero or a non-zero number, notifying the shell that it either succeeded (0) or it failed (1-255). The different numbers can be used as error codes - for example 1 might mean that a file could not be read, etc.
The special variable $?
contains the status code of the last command that ran. How can you read the value of $?
?
The simple commands true
and false
set status codes of 0
and 1
respectively. Run them and then print out the value of $?
.
Now, the &&
and ||
operators can be used to combine multiple commands based on their success or failure. You've already seen this command:
sudo apt-get update && sudo apt-get install arduino
Can you now guess what it means?
Linux commands are based on the philosophy of doing one job, and combining simple tasks into complex operations using |
, <
, and >
.
The good thing about building pipelines is that you can build all the bits separately, and then combine them at the end. Let's work through one together: save a list of all the running process that are owned by me.
- Firstly, what command do we use to print out all the running processes? What does the output mean?
- Next, how can I find out the ID of the current user?
- Next, how can I filter the output of the processes command?
- Great! Now let's combine all those.
- Firstly, list the processes.
- Pass the output of that to our filter command. This should also get the current userid.
- Finally, output the results of the filter to a file.
Work in groups to build pipelines to do the following:
-
Write a file called everything which contains all the files and directories in your home directory, and a file called directories which contains just the directories.
- HINT: You'll need to understand the output of
ls -la
. - HINT: You'll need to use a
grep
pattern - HINT: You'll need to use
tee
once.
- HINT: You'll need to understand the output of
-
Find out what
watch -n1 "ls -lart /"
does. Try it out - then itctrl
+c
to exit. Write a similar pipeline to show the one most recently edited file in your home directory every second. Open another terminal window and use theecho
and>
command to create a new file, and check that your pipeline's output changed.