Command Line Basics¶
The command line gives you more control than the GUI, is the only way to do some things on Linux, and lets you script repetitive work. The cost is that you have to remember commands, or know how to look them up. This lesson gets you moving around, reading files, and finding help.
Open a terminal¶
Press Ctrl+Alt+T on Ubuntu. Or click Show Applications (the grid icon) and type terminal.
You'll see a prompt:
student@lab:~$
student is your username, lab is the machine name, ~ is your current folder (your home directory). Type after the $ and press Enter to run.
Capitalization and spaces matter. ls -la works; LS -la and ls-la do not. Ctrl+C stops a running command. Ctrl+D closes the terminal.
How a command is put together¶
cat -n /etc/hostname
| Part | Here | Meaning |
|---|---|---|
| Command | cat |
What to do |
| Option (flag) | -n |
Change how it behaves. Not always needed. |
| Argument | /etc/hostname |
What to do it to |
Options usually start with - (short) or -- (long). Some commands also take an operator like > to redirect output; that's below.
Where am I, what's here, move¶
pwd # print working directory: the full path of where you are
ls # list files here
ls -l # long listing: permissions, owner, size, date
ls -a # include hidden files (names starting with a dot)
ls -la # both
cd /etc # change directory
cd # go home
cd .. # go up one level
cd - # go back to where you were
Absolute vs. relative paths¶
A path starting with / is absolute and works from anywhere.
cd /home/student/Music
Anything else is relative to where you are. . means the current folder, .. means the parent.
cd Music # same as cd ./Music, if you're in /home/student
cd ../Documents # up one, then into Documents
Getting help¶
man ls # the manual page for ls. Arrow keys or PgUp/PgDn scroll, q quits.
ls --help # short usage summary
Manual pages have numbered sections. man 5 passwd opens the section 5 (file formats) page for /etc/passwd, not the passwd command.
Reading and creating files¶
cat /etc/hostname # print a file
cat -n file.txt # print with line numbers
file report.pdf # tell you what kind of file something is
echo "hello" # print text to the screen
> sends a command's output into a file instead of the screen. It creates the file, or overwrites it if it exists.
echo "hello" > note.txt
cat note.txt
Try the sequence from the CyberPatriot deck:
cat -n > ~/Documents/hello2.txt
Nothing happens yet, because cat is waiting for input. Type This is a test. Hello again!, press Enter, then Ctrl+D. Open ~/Documents/hello2.txt in the file manager. Your line is there, with a 1 in front of it from -n.
Moving, copying, and deleting¶
touch a # create an empty file (or update its timestamp)
cp a b # copy a to b
mv b c # rename or move b to c
rm c # delete c. There is no recycle bin.
mkdir notes # make a folder
rm -r notes # delete a folder and everything in it
Danger
rm doesn't ask and doesn't undo. rm -r on the wrong folder is how people lose everything. Check pwd first.
Editing files¶
For config files you'll use a text editor. On a desktop image the easiest is the graphical one:
gedit /etc/hosts # Ubuntu (GNOME)
xed /etc/hosts # Linux Mint
Files under /etc are owned by root, so you'll need sudo in front to save changes. The next lesson covers that. nano is the standard terminal editor if there's no desktop: Ctrl+O saves, Ctrl+X exits.
Try it¶
- Run
pwd. Move to/var/log, list it, and go back home with a single command. - Run
man man. Find what section 8 is for. Quit withq. - Run these in order and explain the output of each
ls:cd touch Documents/a cp Documents/a b mv b c ls . Documents rm c Documents/a ls . Documents