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.

CommandCommandCommandCommand
datewhowwhoami
echounamepasswdls
lnmorewccp
mvrmfindchmod
diffheadtaillpr
whichgzipgunziptar
cdpwdmkdirrmdir

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
01Feb-99
02jan12.99
03jan19.99
04jan26.02
05jan5.99
06Jan-01
07Jan-98
08Jan-99
09Jan-00
10Mar-98
11memo1
12memo10
13memo2
14memo2.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
01echo *
02echo jan*
03echo M[a-df-z]*
04echo ????
05echo ?????
06echo *[0-9]
07echo [[:upper:]]
08echo *.*
09echo *99
10echo [FJM][ae][bnr]*
11echo ls | wc -l
12who | wc -l
13ls *.txt | wc -l
14who | sort
15cp memo1 ..
16rm ???
17mv memo2 memo2002
18rm *.txt
19cd; pwd
20sillycommand 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.