Lecture 03: Command Line Interface
Speed. Typing beats hunting through menus with a mouse
Power. Some things simply cannot be done from a graphical interface
Reproducibility. A script can be run again. Clicking cannot
Portability. The same commands work on your laptop, a Raspberry Pi, or a supercomputer
Automation. Shells are programmable, so repetitive work becomes a script
Employability. Servers, containers, and cloud tools all run on Linux, which is why the shell is having a renaissance
Do One Thing And Do It Well
Let’s open up our shell!
A convenient way to do this is through VS Code’s built-in Terminal.
Click on the View menu, then Terminal. You can also use the shortcut Ctrl + ` (backtick).
Your system default shell is loaded. To find out what that is, type echo $SHELL in the terminal.
It is Z shell in my case
… what about you? It is your turn to find out! 🤓
Of course, it’s always possible to open up the shell directly if you prefer. It’s your turn!
Windows: install WSL first, using our tutorial. It gives you the same shell as everyone else
Feel free to check our class tutorial on how to set up your shell in VS Code
You should see something like:
This is shell-speak for: “Who am I and where am I?”
username denotes a specific user (one of potentially many on this computer).
@hostname denotes the name of the computer or server.
:~ denotes the directory path (where ~ signifies the user’s home directory).
$ (or maybe %) denotes the start of the command prompt.
#).All bash commands have the same basic syntax:
command option(s) argument(s)
Examples:
Commands
You don’t always need options or arguments
For example:
ls ~/Documents/ and ls -lh ~/Documents are both valid commands that will yield (different) outputHowever, you always need a command!
All Bash commands have the same basic syntax:
command option(s) argument(s)
Examples:
Options (also called flags)
-l long listing, -a all files including hidden ones, -h readable sizes-u on sort keeps only unique linesAll Bash commands have the same basic syntax:
command option(s) argument(s)
Examples:
man ls opens the manual page for ls. Press q to quit
Manual pages come in numbered sections. Run man man to see them
cht.sh gives short, practical answers straight in the terminal (you may need to install curl first):
tldr tool (GitHub repository) which provides simplified help pages for common commands. Run it like this: tldr command$ tldr ls
Successfully updated local database
ls
List directory contents.
More information: <https://www.gnu.org/software/coreutils/manual/html_node/ls-invocation.html>.
- List files one per line:
ls -1
- List all files, including hidden files:
ls [-a|--all]
- List files with a trailing symbol to indicate file type (directory/, symbolic_link@, executable*, ...):
ls [-F|--classify]
... (the manual continues; press q to quit)manThe man command (“manual pages”) is your friend if you need help
$ man ls
LS(1) General Commands Manual LS(1)
NAME
ls – list directory contents
SYNOPSIS
ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when]
[-D format] [file ...]
DESCRIPTION
For each operand that names a file of a type other than directory, ls
displays its name as well as any requested, associated information. For
each operand that names a file of type directory, ls displays the names
of files contained within that directory, as well as any requested,
associated information.
If no operands are given, the contents of the current directory are
displayed. If more than one operand is given, non-directory operands are
... (the manual continues; press q to quit)manman shows the page in a pager, a small viewer for long text. The keys you need:
j and k, or the arrow keys, move one lined and u move half a page, space and b a whole oneg and G jump to the very start and the very end/pattern searches forwards, ?pattern backwards. Then n and N for the next and previous hitq quitsh lists every other key RTFM! 😂
Always check the documentation!
man page explorer challengePartner up and choose a command from the list below. Use man to complete these tasks:
You have about 5-10 minutes. Be ready to share your findings!
Reflection: How was using man compared to online searches? How might you use it in future projects?
Key navigation commands are:
pwd to print (the current) working directorycd to change directoryPaths can be absolute (from the top) or relative (from where you are). Prefer relative ones, and use these shortcuts:
~ your home folder. the current directory.. the directory aboveLet’s try it out!
Accessing the figures/ directory:
Now, let’s go back to the previous directory:
Back two directories:
Beware of spaces in names. Say you have a folder called “My Documents” (I’m looking at you, Windows)
cd My Documents fails. Bash reads My and Documents as two separate argumentscd "My Documents"cd My\ Documents. Tab completion does this for youassignment-05 or assignment_05Getting around
| Command | What it does |
|---|---|
pwd |
Print the working directory |
cd dir |
Change directory |
cd - |
Back to where you just were |
ls |
List files here |
ls -l |
Long format, with details |
ls -a |
Include hidden files |
ls -lh |
Long format, readable sizes |
ls -R |
Include subdirectories |
~ . .. |
Home, here, one level up |
Working with files
| Command | What it does |
|---|---|
mkdir |
Create a directory |
touch |
Create an empty file |
cp |
Copy |
mv |
Move, or rename |
rm |
Delete a file. No undo! |
rmdir |
Delete an empty directory |
cat |
Show what is in a file |
find |
Search for files |
A fuller list is here
Type each command. Look at the result before you move on
cd ~mkdir practice && cd practicetouch file1.txt file2.txtlsfile2.txt to file3.txt: mv file2.txt file3.txtls && cd ~rm -rf deletes at once, with no Trash and no undo. Read the path, then delete the directory: rm -rf practicelscd ~mkdir shell-practicecd shell-practicetouch file1.txt file2.txt file3.txtlsmkdir subdirmv file2.txt subdir/cp file1.txt file4.txtls -Rcd ..rm -r shell-practicelsBonus: use man to read about any command you used.
command [options] arguments-l) or spelled out (--human-readable), and they combine: ls -lahpwd says where you are, ls shows what is there, cd moves you~ is home, . is here, .. is one level upman, tldr, and cht.sh answer your questions. q gets you out of the pagerfind, so you can act on many files at oncecat, head, grep, and sed