Shell
Imagine you want to tell the computer's operating system (the "kernel") to do something. You don't talk to it directly; you need an interpreter.
A shell in Linux is a command interpreter that acts as an interface between the user and the operating system.
It allows users to enter commands on a command line, which are then interpreted by the shell and sent to the operating system for execution
There are different types of shells, just like there are different brands of cars. The most common ones are:
BASH (Bourne Again Shell)
- It's the standard shell on most Linux distributions and macOS.
- It's incredibly widespread. Scripts written for Bash are highly portable. You can be sure it's always installed.
- Best for: Beginners and scripting because of its universal availability.
Z-shell (Zsh)
- The Z-shell is an enhanced version of the BASH
- It supports superior auto-completion, themed prompts, automatic directory changing, and better plugin assistance.
Basic Commands
Basic Navigation Commands
Linux provides a set of commands for basic file management and navigation from the command line.
-
pwd (Print Working Directory)
- The
pwdcommand displays the absolute pathname of your current working directory. - When a user logs in or starts a terminal session, their current working directory is typically set to their home directory.
- The
-
ls (List Directory Contents)
- The
lscommand lists the names of files and directories. It can list the contents of the current working directory or any specified directory. - Common options include:
-l(long format): Displays detailed information about files, including permissions, number of links, owner, group, file size, last modification date and time, and the filename.-a(all): Shows all entries, including hidden files (those whose names begin with a period).-F(classify): Appends an indicator character (e.g.,/for directories,*for executables) to the end of each listed name.-h(human-readable): Displays file sizes in human-readable format ("8K" or "53M") in long format listings.-i(inode): Shows each file's inode number.-d(directory): When used with-l, it shows details about the directory itself, rather than its contents.
- The
-
cd (Change Directory)
- The
cdcommand changes the current working directory. - It takes a pathname as an argument, which can be:
- Absolute pathname: Starts from the root directory (/) and follows the full path to the desired directory (e.g.,
/usr/bin). - Relative pathname: Starts from the current working directory.
.(dot): Refers to the current working directory...(double dot): Refers to the parent directory of the current working directory.~(tilde): Represents the pathname of the user's home directory.cdused by itself also changes back to the home directory.~user_namechanges the directory to the home directory of the specified user.cd -: Changes the working directory to the previous working directory.
- The
-
mkdir (Make Directory)
- The
mkdircommand creates new directories. - It can create multiple directories with a single command by listing them as arguments.
- The
-
touch
- The
touchcommand is a utility that can be used to change file times. It is also mentioned as a command for assisting in file system explorations.
- The
-
rmdir (Remove Directory)
- The
rmdircommand is used to remove (erase) empty directories. - To remove a directory and all its contents (subdirectories and files), you must use the
rmcommand with the -r (recursive) option.
- The
-
cp (Copy)
- The
cpcommand copies files or entire directories. - To copy a file, specify the source file and the destination (new file name or directory).
- When copying files to a directory, the directory name is the last argument.
-ror-R: Used to copy directories recursively, including their subdirectories and files.-i(interactive): Prompts the user for confirmation before overwriting an existing file.-v(verbose): Displays verbose output, showing the action being performed.
- The
-
mv (Move/Rename)
- The
mvcommand moves or renames files and directories. - To rename a file or directory, specify its current name and the new name.
- To move one or more items to a different directory, list the items followed by the destination directory.
-i(interactive): Prompts the user for confirmation before overwriting an existing file.-u(update): Moves files only if they either do not exist in the destination or are newer than the existing corresponding files.
- The
-
rm (Remove)
- The
rmcommand removes (erases) files and directories. - To remove an empty directory, the
rmdircommand can be used. - To remove a non-empty directory,
rmmust be used with the -r (recursive) option. -i(interactive): Prompts the user for confirmation before deleting each file. This is often recommended for safety.-f(force): Forces removal without prompting, even if files are write-protected.
- The
-
touch
- The
touchcommand is used to create empty files. - If you run
touchon an existing file, it updates the file's timestamps without altering its content.
- The
Interactive Challenge: Find the Treasure
Objective: Use your terminal to navigate and create a specific folder structure.
Your Mission: From your home directory, create a folder called my_adventure. Inside it, create a folder named forest. Inside forest, create an empty file called treasure_map.txt.
Steps:
-
Open your terminal. You should start in your home directory (
~). Verify withpwd. -
Create the main folder:
mkdir my_adventure -
Move into it:
cd my_adventure -
Create the sub-folder:
mkdir forest -
Move into the sub-folder:
cd forest -
Create the treasure file:
touch treasure_map.txt -
Verify your work: Use
lsandpwdto confirm you are in the right place and the file exists.pwdshould show:/home/yourusername/my_adventure/forestlsshould show:treasure_map.txt
Bonus Challenge:
Can you create the treasure_map.txt file using an absolute path from your home directory without using cd?
Hint (Click to reveal)
touch /home/yourusername/my_adventure/forest/treasure_map.txt
Text Editors
Linux provides several text editors for creating and editing files.
Here we’ll cover three most powerful.
1. Mousepad (The Graphical Safe Haven)
If you are on a desktop Linux distribution like Xubuntu and have a graphical interface running, Mousepad is a simple, pre-installed text editor. It's the Linux equivalent of Windows' Notepad.
-
How to open a file:
$ mousepad myfile.txt &
(The
&runs it in the background, freeing up your terminal.) -
How to use it:
It works exactly like any graphical text editor: click, type, use the menu to Save and Quit.
- Best for: Beginners who are most comfortable in a point-and-click environment. Note: This won't be available on servers or remote connections.
2. Nano (The Simple Terminal Editor)
Nano is the perfect starting point for terminal-based editing. It's simple, intuitive, and shows all its commands on the screen.
Opening, Editing, Saving, and Quitting:
-
Open a file:
$ nano myfile.txt
If
myfile.txtexists, Nano opens it. If it doesn't, Nano creates it. -
The Interface:
You'll see a blank screen or your file's content. The bottom two lines show the most common commands, where
^means the Ctrl key.^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos M-U Undo ^X Exit ^R Read File ^\ Replace ^U Uncut Text ^T To Spell ^_ Go To Line M-E Redo
- Edit text: Just start typing! Use the arrow keys to move the cursor.
-
Save ("Write Out"):
- Press Ctrl + O (for "Write Out").
- Nano will ask for the filename. Press Enter to confirm the current name.
-
Quit:
- Press Ctrl + X to exit.
- If you have unsaved changes, Nano will ask if you want to save them. Press Y for "Yes" or N for "No".
Nano Cheat Sheet:
- Ctrl + X: Exit Nano.
- Ctrl + O: Save the file ("Write Out").
- Ctrl + K: Cut the current line.
- Ctrl + U: Paste the cut line.
- Ctrl + W: Search for text.
- Ctrl + _ (Ctrl+Shift+-): Go to a specific line number.
Best for: Beginners in the terminal and quick, simple edits to configuration files.
3. Vim (The Powerful Editor)
Vim is a very powerful and efficient editor, but it has a steep learning curve. It's mode-based, which is its biggest difference.
- Insert Mode: For typing and editing text. (You are inserting text.)
- Normal Mode: For navigating, deleting, copying, and pasting text. (This is the default mode when you start.)
Opening, Editing, Saving, and Quitting (The Absolute Basics):
-
Open a file:
$ vim myfile.txt
- You are in Normal Mode. You cannot type text yet. This mode is for giving commands.
-
To start editing (Enter Insert Mode):
Press the letter i. You will see
-- INSERT --appear at the bottom of the screen. Now you can type and edit text normally. -
To stop editing (Return to Normal Mode):
Press the Esc key. The
-- INSERT --indicator will disappear. -
To save the file (Write):
Make sure you are in Normal Mode (press Esc). Then type
:wand press Enter. -
To quit:
Make sure you are in Normal Mode (press Esc). Then type
:qand press Enter. -
To save AND quit in one command:
(Normal Mode) →
:wq+ Enter -
To quit WITHOUT saving (force quit):
(Normal Mode) →
:q!+ Enter
How to remember the basics: The Vim lifecycle for beginners is i → Esc → :wq.
ito get in (Insert mode)- Esc to get out (back to Normal mode)
:wqto write and quit
Best for: System administrators and developers who do a lot of work in the terminal and are willing to learn a more efficient, but complex, tool.
Challenge 2
Interactive Challenge: The Log File Investigator
Objective: Use the tools to find specific information in a system log.
Scenario: You are troubleshooting a web server issue. You need to look at the Apache access log.
-
First, see how big the log file is. Is it small or large?
-
ls -lh /var/log/apache2/access.log(This will show the file size.)
-
-
Get a preview. Look at just the last 5 entries to see recent activity.
-
sudo tail -5 /var/log/apache2/access.log
-
-
Now, open the entire log file with the correct viewer.
-
sudo less /var/log/apache2/access.log
-
-
Inside
less, find all requests that resulted in a404error.-
Press
/then type404and hit Enter. -
Press
nto jump to the next occurrence.
-
Press
-
The developer says the issue started recently. Watch the log live.
-
sudo tail -f /var/log/apache2/access.log - Now, open a web browser and try to access your server. Watch the commands appear in real-time!
-
Press
Ctrl+Cto stop following the log.
-