Shell Basics¶
The shell is how a sysadmin actually drives Linux. This page gets you fluent in bash: the essential commands, how to chain them with pipes and redirection, glob with wildcards, work faster with tab completion and history, use environment variables, and edit files. It builds directly on the filesystem hierarchy you just learned.
Tested on
AlmaLinux 9.4 with bash 5.1 (the default login shell). Everything here is portable to other bash systems; Debian/Ubuntu differences are noted inline.
Terminal vs shell¶
These two words get mixed up constantly:
- A terminal (or terminal emulator) is the window/program that shows text and takes your keystrokes — e.g. GNOME Terminal, the SSH session window, the console.
- A shell is the program running inside the terminal that reads your commands, interprets them, and runs programs. The default on most Linux systems is bash (the "Bourne Again SHell").
So you type into a terminal; bash does the work. You can confirm which shell you're running:
The prompt¶
When the shell is ready for input it shows a prompt:
deepak— the current user.web01— the hostname.~— the current directory (~means your home directory).$— you're a regular user. A#instead means you're root (be careful!).
Essential commands¶
Moving around and looking¶
pwd # print working directory — where am I?
ls # list files in the current directory
ls -l # long listing: permissions, owner, size, date
ls -la # also show hidden files (those starting with .)
ls -lh /var/log # -h = human-readable sizes (KB/MB/GB)
cd /etc # change directory (absolute path)
cd .. # go up one level
cd # with no argument, go home (~)
cd - # jump back to the previous directory
Reading files¶
cat /etc/os-release # dump a whole (short) file to the screen
less /var/log/messages # page through a long file: arrows, /search, q to quit
head -n 20 /var/log/secure # first 20 lines (default 10)
tail -n 20 /var/log/secure # last 20 lines
tail -f /var/log/secure # follow: stream new lines live (Ctrl-C to stop)
echo "Hello, $USER" # print text/variables to the screen
Tip
Use less for anything that might be long — it doesn't load the whole file into memory and lets you search with /. cat is for short files or piping.
Creating, copying, moving, deleting¶
mkdir projects # make a directory
mkdir -p a/b/c # -p creates parent directories as needed
touch notes.txt # create an empty file (or update its timestamp)
cp notes.txt notes.bak # copy a file
cp -r projects projects-copy # -r copies a directory recursively
mv notes.bak archive/ # move (or rename) a file
mv old.txt new.txt # rename
rmdir emptydir # remove an EMPTY directory
rm notes.txt # remove a file
rm -r projects-copy # remove a directory and its contents
Danger
There is no recycle bin on the command line. rm -r (and especially rm -rf) deletes permanently and immediately. Double-check the path, and never run rm -rf / or rm -rf $VAR/ where $VAR could be empty.
Searching: find and grep¶
# find: locate files by name, type, size, age, etc.
find /etc -name "*.conf" # all .conf files under /etc
find /var/log -type f -mtime -1 # files modified in the last 24 hours
find /home -size +100M # files larger than 100 MB
# grep: search for text INSIDE files
grep "Failed password" /var/log/secure # lines containing the phrase
grep -i "error" app.log # -i = case-insensitive
grep -r "TODO" /home/deepak/projects # -r = recurse into directories
grep -n "listen" /etc/nginx/nginx.conf # -n = show line numbers
Getting help¶
man ls # the full manual page (q to quit, /word to search)
ls --help # quick usage summary for most commands
man -k password # search manuals by keyword (a.k.a. "apropos")
Tip
When you're stuck, man <command> and <command> --help answer 90% of questions without leaving the terminal. Get comfortable reading man pages early.
Pipes and redirection¶
This is where the shell becomes powerful. By default a command reads from standard input (stdin), writes results to standard output (stdout), and errors to standard error (stderr). You can rewire these.
| Operator | Meaning |
|---|---|
\| |
Pipe: send one command's stdout into the next command's stdin |
> |
Redirect stdout to a file (overwrites it) |
>> |
Append stdout to a file (creates it if missing) |
< |
Redirect stdin from a file |
2> |
Redirect stderr to a file |
2>&1 |
Send stderr to wherever stdout is going |
# Pipe: count how many users can log in with a shell
cat /etc/passwd | grep -c "/bin/bash"
# Chain several: top 5 largest directories under /var
du -h /var/* | sort -rh | head -n 5
# Redirect stdout, overwriting
echo "first line" > report.txt
# Append (don't clobber)
echo "second line" >> report.txt
# Feed a file in as stdin
sort < report.txt
# Capture errors separately from normal output
find / -name "*.log" > found.txt 2> errors.txt
# Send everything (output + errors) to one file
some_command > all.log 2>&1
Warning
> silently overwrites the target file. If you mean to add to a file, use >>. Many a logfile has been wiped by typing > instead of >>.
Wildcards (globbing)¶
The shell expands these patterns into matching filenames before the command runs:
| Pattern | Matches |
|---|---|
* |
Any number of characters (including none) |
? |
Exactly one character |
[...] |
Any one character from the set, e.g. [abc] or a range [0-9] |
ls *.conf # everything ending in .conf
ls log_?.txt # log_1.txt, log_a.txt — single char before .txt
ls report[0-9].pdf # report0.pdf ... report9.pdf
rm backup-2023-*.tar.gz # all 2023 backups — review with ls first!
Tip
Before any destructive glob with rm, run the same pattern with ls first to see exactly what will match.
Tab completion¶
Press Tab and bash completes the rest of a command, filename, or path for you. Press Tab twice to list all possibilities when there's more than one match.
This is not just a convenience — it prevents typos in long paths and is one of the biggest speed-ups you'll learn.
Command history¶
Bash remembers the commands you run.
history # numbered list of recent commands
history | grep ssh # find a command you ran earlier
!! # re-run the LAST command
sudo !! # re-run the last command WITH sudo (very common!)
!42 # re-run command number 42 from history
Press Up/Down arrows to scroll through previous commands. Press Ctrl-R and start typing to reverse-search your history interactively — keep pressing Ctrl-R to cycle through older matches, then Enter to run.
Environment variables¶
Environment variables are named values the shell and programs read for configuration.
echo "$HOME" # /home/deepak
echo "$USER" # deepak
echo "$PATH" # the dirs searched for commands, colon-separated
env # list ALL environment variables
$PATH is special: when you type ls, the shell searches each directory in $PATH (in order) for an executable named ls. That's why programs in /usr/bin "just work" by name.
# Set a variable for THIS shell only
GREETING="hello"
echo "$GREETING"
# export makes it available to programs you launch from this shell
export EDITOR=vim
# Add a directory to PATH (prepend so it's searched first)
export PATH="$HOME/bin:$PATH"
Note
Variables set this way last only for the current session. To make them permanent for your user, add the export lines to ~/.bashrc (per-interactive-shell) or ~/.bash_profile (per-login), then source ~/.bashrc to reload.
Editing files¶
You'll constantly edit config files in /etc. Two editors matter:
nano — the beginner-friendly choice¶
nano shows its shortcuts at the bottom of the screen (^ means Ctrl):
- Ctrl-O then Enter — write (save) the file.
- Ctrl-X — exit.
- Ctrl-W — search ("where is").
- Ctrl-K / Ctrl-U — cut / paste a line.
vim — survival guide¶
vim (or vi) is installed everywhere, including minimal servers and rescue environments, so you must know enough to escape it.
- vim starts in Normal mode (keys are commands, not text).
- Press
ito enter Insert mode and type normally. - Press
Escto return to Normal mode. - Type
:w+ Enter to save,:qto quit,:wqto save and quit. :q!+ Enter — quit without saving (your escape hatch when you've made a mess).
The one thing to remember about vim
If you're ever stuck in vim, press Esc then type :q! and Enter. That always gets you out without saving damage.
Verify your work¶
# 1. Confirm your shell, user, and location
echo "$SHELL"; whoami; pwd
# 2. Practice a pipe + redirection: save the 5 most-recent auth log lines
sudo tail -n 5 /var/log/secure > /tmp/recent-auth.txt && cat /tmp/recent-auth.txt
# 3. Practice globbing safely (list before you'd ever delete)
touch /tmp/t1.log /tmp/t2.log /tmp/keep.txt
ls /tmp/t?.log
# 4. Use history and re-run with sudo
echo "ran a command"; history | tail -n 3
# 5. Inspect your PATH and find where a command lives
echo "$PATH"; which grep
# 6. Clean up the practice files
rm /tmp/t1.log /tmp/t2.log /tmp/keep.txt /tmp/recent-auth.txt
Summary¶
- The terminal is the window; the shell (bash) is the program interpreting your commands. The prompt ends in
$(user) or#(root). - Core commands: navigate (
pwd,ls,cd), read (cat,less,head,tail,echo), manage files (cp,mv,rm,mkdir,rmdir,touch), search (find,grep), and get help (man,--help). - Pipes (
|) chain commands; redirection sends output to files:>overwrites,>>appends,<feeds input,2>captures errors. - Wildcards
*,?,[]let one command act on many files — preview withlsbefore destructive use. - Tab completion,
history/!!/Ctrl-R make you dramatically faster. - Environment variables like
$PATHconfigure the shell;exportpasses them to child programs; persist them in~/.bashrc. - Edit files with nano (beginner-friendly) or vim (everywhere — remember
Esc:q!to bail out). - You've finished Stage 1 — Foundations. You can now identify, install, navigate, and operate a Linux system from the shell.