Welcome to Part 1 of the Linux Bash course. This course is designed to take you from a absolute beginner to an advanced command-line power user. In this first part, we will explore the historical and architectural foundations of Linux and Bash, take our first steps running interactive terminal commands, and master navigating the hierarchical Linux filesystem.
If you are planning to deploy applications on Linux, you might find our guide on Installing Docker on Ubuntu very helpful once you have mastered the basics here. For more details on the shell standard, you can consult the official GNU Bash Manual.
Chapter 1: Introduction to Linux & Bash
1.1 What is Linux?
Linux is not a single operating system, but a family of open-source Unix-like operating systems based on the Linux kernel. Created by Linus Torvalds in 1991, the Linux kernel manages system hardware resources and provides a bridge between software applications and physical components.
- The Linux Kernel: The core engine of the operating system. It handles memory management, process scheduling, file system access, and hardware device communication.
- Linux Distributions (Distros): Since the kernel alone is not a complete OS, developers package the kernel with system utilities, desktop environments, package managers, and pre-installed software to create “distributions”. Notable examples include:
- Debian/Ubuntu: Famous for stability and the APT package manager.
- Red Hat Enterprise Linux (RHEL)/Fedora: Popular in enterprise environments, utilizing RPM.
- Arch Linux: A lightweight, rolling-release distribution for advanced users.
1.2 Understanding the Shell and Terminal
Many beginners confuse the terminal with the shell, but they are separate entities that work together:
- Terminal (or Terminal Emulator): A graphical application that provides a window where you can type commands and see text output. Examples include GNOME Terminal, Alacritty, iTerm2, and Kitty.
- Shell: The command-line interpreter that runs inside the terminal. It accepts the text commands you type, interprets them, executes them by interacting with the kernel, and sends the output back to the terminal.
- Historical Context: Originally, terminals were physical hardware units consisting of a keyboard and screen (often called teletypewriters or TTYs) connected to a main computer. Today, software terminal emulators reproduce this interaction.
1.3 What is Bash?
Bash, short for Bourne Again SHell, is an sh-compatible command language interpreter that executes commands read from standard input or files.
- Origin: It was written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell (
sh), which was the default shell on early Unix systems. - Features: Bash incorporates useful features from the Korn Shell (
ksh) and the C Shell (csh), including command-line editing, unlimited-size command history, directory manipulation, job control, and aliases. - Ubiquity: Bash is the default shell on most Linux distributions, making it the de facto standard for writing cross-platform shell scripts and automating system administration tasks.
1.4 How Linux Bootstraps the Terminal
When you start a terminal session, the system establishes a communication channel:
- Teletypes (TTYs): Virtual terminals managed by the Linux kernel. You can switch between raw TTYs on a desktop using keys like
Ctrl+Alt+F3toCtrl+Alt+F6. - Login vs. Non-Login Shells:
- Login Shell: Initiated when you authenticate into a system (e.g., via SSH or logging in at the text console). It loads configuration from
/etc/profileand~/.bash_profileor~/.bash_login. - Non-Login Shell: Started after login, such as when opening a new terminal window within a graphical user interface (GUI). It loads configuration from
~/.bashrc.
- Login Shell: Initiated when you authenticate into a system (e.g., via SSH or logging in at the text console). It loads configuration from
1.5 The Anatomy of a Linux Command
A Linux command entered into the shell typically follows a strict structural syntax:
command [options/flags] [arguments]
- Command: The name of the program or builtin you want to run (e.g.,
lsorgrep). - Options/Flags: Modifiers that alter the behavior of the command. They usually start with a single hyphen for short forms (e.g.,
-l) or double hyphens for long forms (e.g.,--long). - Arguments: The targets upon which the command operates, such as file paths, directory names, or search strings (e.g.,
/var/log).
1.6 The Bash Prompt Explained
When you open a terminal, you are greeted by a prompt. On a standard Ubuntu configuration, it looks like this:
suresh@techblog-server:~$
- suresh: The username of the currently logged-in user.
- @: Separator character.
- techblog-server: The hostname of the computer.
- ~: The current working directory. The tilde symbol represents the user’s home directory (e.g.,
/home/suresh). - $: Indicates that you are a standard (non-root) user. If you switch to the root administrator account, this character changes to a hash (
#).
1.7 Basic Shell Builtins vs. External Commands
Commands in Bash fall into two primary categories: shell builtins and external executables.
- Shell Builtins: Commands implemented directly inside the Bash source code. Because they run in the shell process itself, they do not require spinning up a new process, making them faster. Examples:
cd,echo,alias. - External Commands: Independent programs residing in directories on your filesystem (like
/bin,/usr/bin). When invoked, the shell forks a new process to run the program. Examples:ls,grep,systemctl. You can check how a command is defined using thetypecommand.
Syntax:
type [options] command_name
Example Command:
type cd ls
Expected Output:
cd is a shell builtin
ls is aliased to `ls --color=auto'
Flag & Command Breakdown:
type: The command used to describe how its arguments would be interpreted if used as command names.cd: The first argument. The output shows it is integrated directly inside the shell.ls: The second argument. The output shows it is defined as an alias wrapper around the external command.
1.8 Keyboard Shortcuts for Command-Line Efficiency
Using a terminal is much faster when you master core keyboard shortcuts to manipulate the input line:
Ctrl + C: Aborts the currently executing command and returns a clean prompt.Ctrl + D: Sends an End-Of-File (EOF) marker, logging you out of the shell session or closing the current terminal window.Ctrl + L: Clears the screen without discarding command history (similar to theclearcommand).Ctrl + A: Moves the cursor to the beginning of the current command line.Ctrl + E: Moves the cursor to the end of the current command line.Ctrl + U: Cuts (deletes) all text from the cursor position back to the beginning of the line.Ctrl + K: Cuts (deletes) all text from the cursor position to the end of the line.Ctrl + R: Initiates a reverse search through command history, allowing you to quickly find previously typed commands.
1.9 Shell Configuration Files
Bash uses configuration files to customize user environments:
/etc/profile: System-wide environment configurations applied to all users during login.~/.bash_profile: User-specific environment configuration for login shells. Usually sources~/.bashrc.~/.bashrc: User-specific configuration for non-login interactive shells. This is the standard file where custom aliases, shell functions, and environment exports are declared.~/.bash_logout: Script executed when a login shell exits.
1.10 Getting Help in the Shell
You do not need to memorize every flag. The shell provides built-in mechanisms to discover usage details:
- Help Flag: Almost all command-line tools support
--helpor-h. Runningls --helpprints a summary of available flags and syntax. - Shell Builtin Help: The
helpcommand displays documentation specifically for shell builtins (e.g.,help cd). - Manual Pages: Detailed documentation for commands, system calls, and configuration files, accessible using
man.
Chapter 2: First Steps in the Terminal
2.1 Printing Text to the Screen: The echo Command
The echo command writes its arguments to standard output, separated by spaces and terminated by a newline.
Syntax:
echo [options] [string...]
Example Command:
echo -e "Line one\nLine two with \t tab"
Expected Output:
Line one
Line two with tab
Flag & Command Breakdown:
echo: The command utility to print text.-e: Enables the interpretation of backslash escapes (e.g.,\nfor a newline,\tfor a tab stop)."Line one\nLine two with \t tab": The string argument containing backslash escape sequences.
2.2 Advanced Text Output: The printf Command
Unlike echo, printf offers strict formatting control modeled after the C library function of the same name. It does not automatically append a newline.
Syntax:
printf format [argument...]
Example Command:
printf "User: %s\tID: %04d\n" "suresh" 42
Expected Output:
User: suresh ID: 0042
Flag & Command Breakdown:
printf: The advanced formatting output utility."User: %s\tID: %04d\n": The format string containing directives:%s: Formats a string (substituted by"suresh").\t: Represents a tab character.%04d: Formats an integer padded with leading zeros to be at least 4 digits wide (substituted by42).\n: Explicit newline.
"suresh": First argument mapped to%s.42: Second argument mapped to%04d.
2.3 Finding Out Who You Are: The whoami Command
The whoami command displays the username associated with the current effective user ID.
Syntax:
whoami
Example Command:
whoami
Expected Output:
suresh
Flag & Command Breakdown:
whoami: Evaluates the current user context and prints the string name. Takes no arguments or options.
2.4 Checking System Hostname: The hostname Command
The hostname command is used to display or configure the system’s DNS host name.
Syntax:
hostname [options]
Example Command:
hostname -I
Expected Output:
192.168.1.100 172.17.0.1
Flag & Command Breakdown:
hostname: The utility to show the system hostname and network address mappings.-I: Displays all network addresses configured on all network interfaces (excluding loopback).
2.5 Finding Your Current Path: The pwd Command
The pwd (Print Working Directory) command displays the absolute path of the current directory, starting from the root /.
Syntax:
pwd [options]
Example Command:
pwd -P
Expected Output:
/home/suresh/Documents/TechBlog
Flag & Command Breakdown:
pwd: Print Working Directory command.-P: Resolves symbolic links and prints the physical directory structure (e.g. if the current directory is a symlink, it prints the actual physical path).
2.6 Listing Directory Contents: The ls Command
The ls command lists directory contents, showing files and subdirectories.
Syntax:
ls [options] [file...]
Example Command:
ls -lha
Expected Output:
total 24K
drwxr-xr-x 4 suresh suresh 4.0K Jun 12 13:30 .
drwxr-xr-x 8 suresh suresh 4.0K Jun 12 13:00 ..
-rw-r--r-- 1 suresh suresh 220 Jun 12 13:00 .bashrc
-rw-r--r-- 1 suresh suresh 807 Jun 12 13:00 .profile
drwxr-xr-x 2 suresh suresh 4.0K Jun 12 13:30 courses
Flag & Command Breakdown:
ls: List directory contents command.-l: Uses a long listing format, showing file permissions, owner, group, size, and modification timestamp.-h: Prints human-readable sizes (e.g., 4.0K instead of 4096 bytes), combined with-l.-a: Includes hidden files (those starting with a dot.), including the current directory.and parent directory...
2.7 Clear the Screen: The clear Command
The clear command clears the terminal screen if possible. It checks the environment variable TERM for the terminal type and queries the terminal database (terminfo) to find how to perform the action.
Syntax:
clear
Example Command:
clear
Expected Output: (The terminal screen is cleared, and the prompt returns to the top-left corner)
Flag & Command Breakdown:
clear: Resets the terminal interface view.
2.8 Displaying System Information: The uname Command
The uname command prints system and kernel details.
Syntax:
uname [options]
Example Command:
uname -a
Expected Output:
Linux techblog-server 6.8.0-35-generic #35-Ubuntu SMP PREEMPT_DYNAMIC Mon May 20 15:51:52 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
Flag & Command Breakdown:
uname: Print system information.-a: Prints all available system information (kernel name, network hostname, kernel release date, processor architecture, operating system).
2.9 Checking Disk Space: The df Command
The df (Disk Free) command reports the amount of disk space used and available on filesystems.
Syntax:
df [options] [file...]
Example Command:
df -hT /
Expected Output:
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 50G 12G 36G 25% /
Flag & Command Breakdown:
df: Reports filesystem storage capacities.-h: Human-readable format (displays output in Gigabytes/Megabytes).-T: Prints the filesystem type (e.g.ext4,tmpfs,xfs)./: Limits the output specifically to the root filesystem partition.
2.10 Checking Memory Usage: The free Command
The free command displays the total amount of free and used physical memory (RAM) and swap space in the system.
Syntax:
free [options]
Example Command:
free -h --si
Expected Output:
total used free shared buff/cache available
Mem: 7.9Gi 2.1Gi 3.5Gi 230Mi 2.3Gi 5.5Gi
Swap: 2.0Gi 0B 2.0Gi
Flag & Command Breakdown:
free: Utility to display RAM and Swap statistics.-h: Displays statistics in human-friendly formats (Gigabytes/Megabytes).--si: Uses power-of-1000 scaling metrics (SI standard) instead of power-of-1024 binary metrics.
Chapter 3: Navigating the Filesystem
3.1 The Linux Directory Structure
Unlike Windows, which uses drive letters (C:, D:), Linux represents all storage devices under a single unified hierarchical directory tree starting at the root directory /.
/bin&/sbin: Core binary programs required for system booting and single-user maintenance./etc: Host-specific configuration files for the operating system and installed services./home: User home directories. A non-privileged user namedsureshowns/home/suresh./var: Variable data, such as system logs (/var/log) and database storage./tmp: Temporary files which are often cleared automatically on system reboot./proc&/sys: Virtual directories created by the kernel to expose system and hardware status information.
3.2 Absolute vs. Relative Paths
Navigating the filesystem requires specifying file and directory locations:
- Absolute Paths: Always begin with the root forward slash
/and specify the exact location from the top-level directory down. Example:/home/suresh/Documents/TechBlog/src. - Relative Paths: Specify a location relative to the shell’s current working directory. They do not start with a
/..: Represents the current directory...: Represents the parent directory.- Example: If you are in
/home/suresh/Documents, the relative pathTechBlog/srcpoints to/home/suresh/Documents/TechBlog/src.
3.3 Changing Directories: The cd Command
The cd (Change Directory) command updates the shell’s working directory context.
Syntax:
cd [directory_path]
Example Command:
cd /var/log
Expected Output: (The working directory changes; the terminal prompt updates to show the new folder name)
suresh@techblog-server:/var/log$
Flag & Command Breakdown:
cd: Changes the working directory./var/log: The target absolute directory path to switch to.
3.4 Navigating Back and Forth
Bash provides special character combinations for rapid directory jumping:
cd ~or simplycd: Navigates directly to the current user’s home directory.cd -: Switches to the previous working directory before the lastcdcommand. Useful for jumping back and forth.cd ..: Moves up one level to the parent directory.
Syntax:
cd -
Example Command:
cd /etc && cd -
Expected Output:
/home/suresh/Documents/TechBlog
Flag & Command Breakdown:
cd -: The hyphen argument represents the environment variable$OLDPWDcontaining the previous path.
3.5 The Directory Stack: The pushd and popd Commands
For complex navigations, Bash maintains a directory stack. pushd adds a directory to the stack and changes to it, while popd removes the top directory and returns you there.
Syntax:
pushd directory_path
popd
Example Command:
pushd /etc && popd
Expected Output:
/etc /home/suresh/Documents/TechBlog
/home/suresh/Documents/TechBlog
Flag & Command Breakdown:
pushd: Adds/etcto the directory stack, showing the stack status.popd: Removes/etcfrom the stack and returns the current shell to/home/suresh/Documents/TechBlog.
3.6 Creating Directories: The mkdir Command
The mkdir command creates one or more directories if they do not already exist.
Syntax:
mkdir [options] directory_name...
Example Command:
mkdir -v new_project
Expected Output:
mkdir: created directory 'new_project'
Flag & Command Breakdown:
mkdir: Make directory utility.-v: Verbose output. Displays a message confirming each directory created.new_project: The name of the directory to create.
3.7 Creating Parent Directories: mkdir -p
Creating a deeply nested path like assets/images/icons fails if the parent directories do not exist. Use the -p option to create parent directories as needed.
Syntax:
mkdir -p parent/child/grandchild
Example Command:
mkdir -vp assets/images/icons
Expected Output:
mkdir: created directory 'assets'
mkdir: created directory 'assets/images'
mkdir: created directory 'assets/images/icons'
Flag & Command Breakdown:
mkdir: Create directory utility.-v: Print confirmation messages.-p: Create intermediate parent directories if missing. If the directory already exists, it does not output an error.
3.8 Removing Empty Directories: The rmdir Command
The rmdir command removes empty directories. It safe-guards against accidental deletion of folders containing files.
Syntax:
rmdir [options] directory_name...
Example Command:
rmdir -v new_project
Expected Output:
rmdir: removing directory, 'new_project'
Flag & Command Breakdown:
rmdir: Removes the specified empty directories.-v: Verbose confirmation output.
3.9 Creating Empty Files: The touch Command
The touch command updates the access and modification timestamps of a file. If the file does not exist, it creates a new empty file.
Syntax:
touch [options] file_name...
Example Command:
touch new_script.sh
Expected Output:
(No console output, but running ls confirms that new_script.sh exists with 0 bytes)
Flag & Command Breakdown:
touch: Updates file times or creates files.new_script.sh: The target file.
3.10 Viewing Directory Tree: The tree Command
The tree command recursively lists directory contents in a tree-like graphical format.
Syntax:
tree [options] [directory_path]
Example Command:
tree -L 2 -d assets
Expected Output:
assets
└── images
└── icons
2 directories
Flag & Command Breakdown:
tree: Lists directory contents recursively as a text-diagram tree.-L 2: Max display depth. Limits the output to 2 subdirectory levels.-d: Lists directories only (omits individual file names from the output).assets: The target directory path.