Session 01
Basic Linux CLI (command-line interface) commands
A prompt is something a CLI will display to show the user that it's ready to receive input. Prompts come in all sizes, colours, shapes and forms depending on the shell an distribution you are using.
For example, your prompt might look like this:
user@mymachine:~$
Or it might look minimalistic, like this:
~>
There are many ways a prompt can look, so for this reason the
$
sign is used as a generic symbol in documentation and teaching to simply denote a prompt. And the commands after it are the ones you'll type (so don't type the$
at the beginning).Do not copy-paste commands. You will notice that if you hover over the example command with your mouse, you'll get a symbol show up that allows you to copy the command into your clipboard, which you could paste into your terminal instead of typing it yourself. There are many reasons why this is a bad idea, generally. But specifically for these exercises, you'd be copy-pasting the
$
symbol with the command, and when you try to execute the command you'll receive an error. More importantly though, from experience, I can promise you that typing in the commands by hand will help you learn them faster. Remember, using the CLI allows you to express yourself, or "talk" to the system.
Example:
$ man date
What do the following commands do?
Use the man
command, followed by the command you're interested in (see above example) to find out what it does. Note that not all commands have an accompanying manual page.
Command | Command | Command | Command |
---|---|---|---|
date | who | w | whoami |
echo | uname | passwd | ls |
ln | more | wc | cp |
mv | rm | find | chmod |
diff | head | tail | lpr |
which | gzip | gunzip | tar |
cd | pwd | mkdir | rmdir |
Alternatively, most commands can also be called with the optional argument --help
, which will give you a brief (and sometimes not so brief) summary of how the command syntax works.
Example:
date --help
Directory Structure Exercise 1
Use the appropriate Linux command(s) to create the following directory tree in your home directory
graph TD; FileTypes --> TextFiles --> ShellScripts FileTypes --> BinaryFiles --> Byte-Code --> J-Code Byte-Code --> P-Code FileTypes --> PostScriptFiles --> PDF
Directory Structure Exercise 2
Create the following list of files in a directory called LinuxEx2
:
The first column of this table (and sometimes others) with the
#
heading is simply there for orientation purposes; to help you keep track of which line you are on, and help you get back in case you have to interrupt or get otherwise distracted.
# | Filename |
---|---|
01 | Feb-99 |
02 | jan12.99 |
03 | jan19.99 |
04 | jan26.02 |
05 | jan5.99 |
06 | Jan-01 |
07 | Jan-98 |
08 | Jan-99 |
09 | Jan-00 |
10 | Mar-98 |
11 | memo1 |
12 | memo10 |
13 | memo2 |
14 | memo2.txt |
Explain what the following commands do
Experiment by executing these commands. Document what the output is and, in your own words, try to explain what the commands do. Write your observations down, either on paper or wherever you keep your lecture notes.
# | Command to execute |
---|---|
01 | echo * |
02 | echo jan* |
03 | echo M[a-df-z]* |
04 | echo ???? |
05 | echo ????? |
06 | echo *[0-9] |
07 | echo [[:upper:]] |
08 | echo *.* |
09 | echo *99 |
10 | echo [FJM][ae][bnr]* |
11 | echo ls | wc -l |
12 | who | wc -l |
13 | ls *.txt | wc -l |
14 | who | sort |
15 | cp memo1 .. |
16 | rm ??? |
17 | mv memo2 memo2002 |
18 | rm *.txt |
19 | cd; pwd |
20 | sillycommand 2> errors |
A small script with nano
Change your working directory to your home directory. Start the nano
text editor and type the text that follows. Then save this to a file called FirstTypedScript.sh
#!/bin/env bash
#
# my first typed script
#
echo "Knowledge is Power"
#
# Two more commands.
#
# This one has an interesting optional argument
ls -R1
# This one is new
cat FirstTypedScript.sh
# Look this one up. Useful in shell scripts, like this one. Not much used in everyday CLI usage though.
exit 0
Checking and move FirstTypedScript.sh
Check that the newly created file exists in your home directory. Now move the file with the appropriate command(s) to the directory you created earlier, named ShellScripts
.
What was that in the script?
Find out what the commands written in the FirstTypedScript.sh
file do by typing them in manually on the prompt. Use the manual pages to help you discover more about each command (and optional arguments).
Wrapping it all up
Finally, run your first typed shell script by calling it:
$ ./FirstTypedScript.sh
What happens when you do this? Try finding a way of running the shell script.