Command Line Basics
The CLI, or Command Line Interface is a text-based way to do stuff on your Linux computer. (Back in the old days, this was actually the only way to interact with the computer).
Terminology
- directory = folder
Filesystem hierarchy
- All files in Linux are arranged in a hierarchical directory structure
- a directory can hold 2 things:
- Files
- Other directories
- the topmost directory (or outermost folder) is ‘/’ and is called the root directory
- inside the root directory, there are many directories (most of them, you don't need to worry about). One of them is called ‘home’
- inside ‘home’, each user has their own ‘home directory’ named after their username. All your personal files are in your home directory
- If there's a directory or file inside another directory, then you can append its name to the parent directory to get its full path name
- for ex. bob's home directory would be called /home/bob
- another example: /home/bob/Documents/school/math/homework.pdf
Note: if you append a slash (/) to a directory name, it doesn't change the meaning (it doesn't matter). This is not the case for files, however.
Terminal
- the ‘Terminal’ is a program that allows you to use the CLI
- open with Ctrl-Alt-T or search for ‘Terminal’ in the applications menu
- When using the terminal, you can think of the terminal as being ‘in’ a directory at all times
- You interact with the computer by typing ‘commands’ and then pressing Enter
Commands
1. pwd - print working directory
- outputs the ‘current’ directory, or in other words, the directory that the terminal is ‘in’ currently
- you can see the same information written in blue text on the terminal
- a tilde (~) is short for your home directory (which is /home/bob, but replacing bob with your username)
2. ls - list
- lists all files and directories in the current directory
- shows you what you would normally see in a graphical file browser
- type
ls -a
to show all files, including hidden files. Hidden files are those that start with a dot (.). - In general, you can replace the ‘a’ with other letters to modify the effect of a command. These are known as ‘options’.
- For example, you can use the ‘l’ option with the
ls
command (typels -l
) to see some more info about the files and directories (don't worry about what it means) - Other commands have options too
- For example, you can use the ‘l’ option with the
3. cd <name> - change directory
- change the current directory. Replace <name> with the name of the directory that you wish to enter. Equivalent to clicking on a folder in a graphical file browser
- type
cd ..
to go to the parent directory - you can chain multiple directories like so:
cd Documents/notes/
You can even chain ‘..’, like this:
cd ../../../
- you can use ‘absolute’ and ‘relative’ path names of the directories
- absolute names start with ‘/’. for ex. /home/bob/Documents/english
- relative names don't start with ‘/’ and are assumed to begin from the current directory. for ex. Documents/english
4. man <command> - manual
- shows you information about the usage of a command
- replace <command> with a command, like ‘ls’ or ‘cd’
- press q to quit
- although the ‘man pages’ (which is the common name for these manual entries) may seem hard to understand at first, I encourage you to at least try to read the Description section. Over time, you will learn the jargon and the man pages will become more and more helpful.
- man pages also list all the commands’ options, and what each option does