160 lines
5.3 KiB
Markdown
160 lines
5.3 KiB
Markdown
# Basic Linux Commands (Bash)
|
|
|
|
Almost every software engineer, sysadmin, and DevOps person uses the terminal daily.
|
|
This lesson takes you from never having used Linux to being comfortable navigating
|
|
and managing files on a machine. You can practice on a Raspberry Pi, a cloud VM,
|
|
or any Linux distro — the commands are the same.
|
|
|
|
## 1. What is a terminal?
|
|
|
|
A terminal is a text window where you type a command and press Enter. The program
|
|
that reads your commands is called a **shell**. On most Linux systems (including
|
|
Raspberry Pi OS) it's **Bash**.
|
|
|
|
To open one on the Pi, press `Ctrl+Alt+T` or find "Terminal" in the menu.
|
|
|
|
Commands in this lesson go in code boxes. Type exactly what's in the box and press
|
|
Enter. Anything in a box by itself is a command; anything after `#` is a comment
|
|
explaining it — don't type that part.
|
|
|
|
## 2. Your first commands
|
|
|
|
Run these one by one and read the output:
|
|
|
|
```
|
|
whoami # prints your username
|
|
pwd # prints your current folder (your "address")
|
|
ls # lists the files in your current folder
|
|
date # prints the date and time
|
|
```
|
|
|
|
Linux organizes everything as files and folders in one big tree. The very top
|
|
folder is `/` (the root), and your personal folder (like `/home/pi`) is your home.
|
|
|
|
## 3. Moving around
|
|
|
|
```
|
|
ls # what's in this folder
|
|
ls -la # everything, including hidden files, with details
|
|
cd downloads # go into a folder (no space between cd and the name)
|
|
cd .. # go up one level
|
|
cd ~ # go to your home folder (cd with no argument does the same)
|
|
```
|
|
|
|
Tip: type the first few letters of a folder name and press **Tab** — Linux
|
|
autocompletes it for you. Use Tab constantly.
|
|
|
|
## 4. Files and folders
|
|
|
|
```
|
|
touch notes.txt # create an empty file
|
|
mkdir projects # create a folder
|
|
mkdir -p projects/web/app # create nested folders in one go
|
|
cp notes.txt backup.txt # copy a file
|
|
mv notes.txt projects/ # move a file (or rename it)
|
|
rm backup.txt # delete a file
|
|
rm -r projects # delete a folder and everything inside it
|
|
```
|
|
|
|
Warning: `rm` does not ask "are you sure?" and there is no trash bin. Deleted is
|
|
deleted. Read your command carefully before pressing Enter — especially `rm -rf`,
|
|
which deletes recursively without mercy.
|
|
|
|
## 5. Looking at files
|
|
|
|
```
|
|
cat notes.txt # print the whole file
|
|
head notes.txt # first 10 lines
|
|
tail notes.txt # last 10 lines
|
|
less notes.txt # scroll through a big file (press q to quit)
|
|
```
|
|
|
|
## 6. Writing to files
|
|
|
|
```
|
|
echo "hello" > notes.txt # write (overwrites the file if it exists)
|
|
echo "second line" >> notes.txt # append (>> adds to the end)
|
|
```
|
|
|
|
## 7. Editing files: nano
|
|
|
|
`nano` is the standard beginner editor. Open a file with:
|
|
|
|
```
|
|
nano notes.txt
|
|
```
|
|
|
|
Now you can edit. The keys you need are shown at the bottom of the screen:
|
|
|
|
- `Ctrl+O` then `Enter` — save (it calls saving "Write Out")
|
|
- `Ctrl+X` — quit
|
|
|
|
If it asks "Save modified buffer?" when you quit, press `y`, then `Enter`.
|
|
|
|
## 8. Searching and pipes
|
|
|
|
```
|
|
grep "hello" notes.txt # print only the lines containing "hello"
|
|
ls | grep notes # list only files whose name contains "notes"
|
|
```
|
|
|
|
That `|` is called a **pipe**. It takes the output of the command on the left and
|
|
feeds it as input to the command on the right. Piping small tools together is one
|
|
of the core ideas of Linux — small commands that each do one thing, chained up.
|
|
|
|
## 9. Getting help
|
|
|
|
```
|
|
ls --help # short summary of how to use a command
|
|
man ls # full manual page (press q to quit)
|
|
```
|
|
|
|
When you don't know a command, `man` is the first place to look. It's on the
|
|
machine itself, so it works even without internet.
|
|
|
|
## 10. Installing software: apt
|
|
|
|
On Raspberry Pi OS (and Ubuntu/Debian), software comes in packages managed by
|
|
`apt`. `sudo` means "run this as the administrator" and it will ask for your
|
|
password:
|
|
|
|
```
|
|
sudo apt update # refresh the list of available packages
|
|
sudo apt install -y curl # install a package (the -y means "yes to prompts")
|
|
sudo apt remove curl # remove a package
|
|
```
|
|
|
|
## 11. About your system
|
|
|
|
```
|
|
uname -a # what machine and kernel this is
|
|
df -h # how much disk space is left
|
|
free -h # how much memory is in use
|
|
history # everything you've typed so far
|
|
clear # clear the screen
|
|
```
|
|
|
|
## 12. Shortcuts that feel like magic
|
|
|
|
- **Tab** — autocomplete a command or filename
|
|
- **Up/Down arrows** — cycle through your previous commands
|
|
- **Ctrl+C** — stop whatever is currently running
|
|
- **Ctrl+L** — clear the screen (same as `clear`)
|
|
- **Ctrl+R** — search your command history: start typing the old command, keep
|
|
pressing Ctrl+R to cycle through matches, Enter to run it
|
|
|
|
## Practice
|
|
|
|
Try these from memory (scroll up only when you're stuck):
|
|
|
|
1. Create a folder called `lesson1` and go into it.
|
|
2. Create a file `hello.txt` containing the line `hello world`.
|
|
3. Append a second line: `second line`.
|
|
4. Print only the lines containing `hello`.
|
|
5. Copy `hello.txt` to `hello-backup.txt`, then delete the original.
|
|
6. Print the first line of `hello-backup.txt`.
|
|
|
|
If you can do all six without looking, you know the essentials: `pwd`, `ls`, `cd`,
|
|
`touch`, `nano`, `cat`, `rm`, `cp`, `mv`, `grep`, and pipes. That's the toolkit
|
|
you'll reach for on any Linux machine.
|