Linux Essentials: A Comprehensive Guide - Part 1 - Introduction to Shell

Master Linux fundamentals with this comprehensive guide covering shell basics, essential commands, filesystem hierarchy, and core Linux concepts.

Linux Essentials: A Comprehensive Guide - Part 1 - Introduction to Shell

Table of Contents

Introduction to Shell

What is a shell?

A shell in Linux is a command-line interpreter that provides a user interface for the Linux operating system. It acts as an intermediary between you and the system kernel, allowing you to execute commands, run programs, and manage system resources. Common shells include Bash (Bourne Again SHell), Zsh, and Fish.

Linux Terminal

The Home Directory

The home directory in Linux serves as your personal workspace. It contains your personal files, configuration settings, and directories. Each user has their own home directory, typically located at /home/username, where username is your login name.

The home directory is usually represented by the tilde symbol (~). When you see ~ in a path, it refers to your home directory.

Commands and Arguments

In Linux, interactions with the shell involve using commands and arguments:

  • Commands: Instructions given to the shell to perform specific operations
  • Arguments: Additional information provided to commands to specify how they should operate

For example, in the command ls -l /home/username:

  • ls is the command (list directory contents)
  • -l is an option argument (use long listing format)
  • /home/username is an argument specifying which directory to list

Types of Commands

Linux commands fall into several categories:

  1. Built-in Commands: These are part of the shell itself and don’t exist as separate files. Examples include cd, echo, and exit.

  2. External Commands: These are executable files stored in the filesystem. Examples include ls, grep, and find.

  3. Shell Scripts: These are files containing a series of commands that the shell can execute, used to automate tasks.

  4. Alias Commands: Custom shortcuts created by users to represent longer commands or command sequences.

  5. Functions: Similar to shell scripts but defined within the shell session for reuse.

To check whether a command is built-in or external, use the type command:

$ type cd
cd is a shell builtin

$ type uptime
uptime is /usr/bin/uptime

Essential Linux Commands

Here are some fundamental commands you’ll use regularly in Linux:

  1. pwd - Print Working Directory
    Shows the current directory path.

  2. ls - List Storage
    Lists files and directories.

    • ls -l: Long listing format with details
    • ls -a: Show all files (including hidden)
    • ls -lt: Sort by modification time
    • ls -ltr: Sort by modification time in reverse order
  3. mkdir - Make Directory
    Creates new directories.

    • mkdir -p dir1/dir2/dir3: Create nested directories
  4. cd - Change Directory
    Changes your current location.

    • cd ~: Go to home directory
    • cd ..: Go to parent directory
    • cd -: Go to previous directory
  5. mv - Move
    Moves or renames files and directories.

    • mv source destination
  6. cp - Copy
    Copies files or directories.

    • cp source destination
    • cp -r source destination: Copy directories recursively
  7. rm - Remove
    Deletes files or directories.

    • rm filename: Remove a file
    • rm -r directory: Remove a directory and its contents
    • rm -f filename: Force removal without confirmation
  8. rmdir - Remove Directory
    Removes empty directories.

Viewing File Contents

  1. cat - Concatenate
    Displays file contents.

    • cat filename
    • cat > filename: Create a new file and add content
  2. touch
    Creates empty files or updates timestamps.

    • touch filename
  3. more / less
    Display file contents one screen at a time.

    • more filename: Basic pager
    • less filename: Advanced pager with backward navigation
  4. head / tail
    Show beginning or end of files.

    • head -n 5 filename: First 5 lines
    • tail -n 5 filename: Last 5 lines
    • tail -f logfile: Follow log updates in real-time
  5. grep - Global Regular Expression Print
    Searches for patterns in files.

    • grep "pattern" filename
    • grep -i "pattern" filename: Case-insensitive search
    • grep -r "pattern" directory: Recursive search
    • grep -v "pattern" filename: Show non-matching lines
    • grep -n "pattern" filename: Show line numbers

File Permissions

  1. chmod - Change Mode
    Changes file permissions.

    • chmod 755 filename: Set permissions using octal notation
    • chmod +x filename: Make file executable
    • chmod -w filename: Remove write permission
  2. chown - Change Owner
    Changes file owner.

    • chown username:groupname filename
  3. chgrp - Change Group
    Changes file group.

    • chgrp groupname filename

Getting Help

When you need assistance with commands, these tools are invaluable:

  1. man - Manual
    Displays the manual page for a command.

    • man ls
  2. --help option
    Most commands provide basic help with this flag.

    • ls --help
  3. whatis
    Shows a one-line description of a command.

    • whatis grep
  4. apropos
    Searches for commands related to a keyword.

    • apropos file

Understanding Paths

There are two ways to specify file and directory locations in Linux:

Absolute Path

An absolute path specifies the complete location from the root directory (/).

  • Always starts with /
  • Example: /home/username/documents/file.txt

Relative Path

A relative path specifies a location relative to your current directory.

  • Does not start with /
  • Example: If you’re in /home/username, the relative path to file.txt would be documents/file.txt

Path Navigation Tools

For more efficient navigation, you can use:

  • pushd and popd: Manage a directory stack
    • pushd /var/log: Save current directory and move to /var/log
    • popd: Return to the previously saved directory

Environment Variables

Environment variables store information about your system environment. Here are some important ones:

  • $HOME: Your home directory
  • $PATH: List of directories for executable programs
  • $PWD: Current working directory
  • $SHELL: Your shell type
  • $USER: Your username
  • $HOSTNAME: Your computer’s name
  • $OSTYPE: Your operating system type

View all environment variables:

printenv

View a specific variable:

echo $HOME

Set a temporary environment variable:

export VARIABLE_NAME=value

Make environment changes permanent by adding them to configuration files:

echo "export VARIABLE_NAME=value" >> ~/.bashrc
source ~/.bashrc

The PATH Variable

The PATH variable is particularly important as it tells the shell where to look for executable files when you type a command. Directories in the PATH are separated by colons (:).

View your PATH:

echo $PATH

Add a new directory to your PATH:

export PATH=$PATH:/new/directory/path

To find the location of a command’s executable, use:

which command_name

Customizing Your Shell Prompt

The shell prompt is the text displayed before you type commands. You can customize it by modifying the PS1 environment variable:

export PS1="\u@\h:\w\$ "

Common prompt elements:

  • \u: Username
  • \h: Hostname
  • \w: Current working directory
  • \d: Date
  • \t: Time
  • \$: Shows # for root, $ for regular users

Make prompt changes permanent:

echo 'PS1="\u@\h:\w\$ "' >> ~/.bashrc

Linux Core Concepts

The Linux Kernel

The Linux kernel is the core component of the Linux operating system. It acts as an intermediary between hardware and software, managing:

  • Process Management: Scheduling and executing processes
  • Memory Management: Allocating and managing system memory
  • Device Management: Interfacing with hardware through drivers
  • File System Management: Handling data storage and retrieval
  • Networking: Managing network connections and protocols

The Linux kernel is both monolithic and modular:

  • Monolithic: Runs as a single large process in a single address space
  • Modular: Supports dynamically loadable kernel modules for extending functionality

Check your kernel version:

uname -r
uname -a

User Space vs. Kernel Space

Linux memory is divided into two distinct areas:

User Space:

  • Where user applications and processes run
  • Limited access to system resources
  • Process isolation prevents crashes from affecting the system

Kernel Space:

  • Where the kernel and core system services run
  • Full access to hardware and system resources
  • Operations require privileged access

Linux Memory Architecture

When a user application needs a system resource, it makes a system call that transitions from user space to kernel space, performs the operation, and returns to user space.

Hardware Management

Linux provides several commands for hardware discovery and management:

  • dmesg: Displays kernel ring buffer messages, useful for hardware details and debugging
  • lspci: Lists all PCI devices (network cards, graphics, etc.)
  • lsblk: Lists block devices (hard drives, SSDs, etc.)
  • lscpu: Displays CPU architecture information
  • lsmem: Lists memory blocks and information
  • free: Shows memory usage statistics
  • lshw: Provides comprehensive hardware configuration details

Linux Boot Sequence

The Linux boot process follows these steps:

  1. BIOS/UEFI Initialization:

    • Performs Power-On Self Test (POST)
    • Locates boot device
  2. Bootloader (GRUB):

    • Loads the Linux kernel and initial RAM disk
    • Presents a boot menu if configured
  3. Kernel Initialization:

    • Initializes hardware
    • Mounts the root filesystem
    • Starts the init process or systemd
  4. Init/Systemd:

    • Initializes the user-space environment
    • Starts system services according to runlevel/target
  5. Runlevel/Target Initialization:

    • Defines the system state (multi-user, graphical, etc.)
    • Starts appropriate services
  6. Login Prompt:

    • Presents a text or graphical login interface

Init and Systemd

The init process is the first process started during boot (PID 1). Modern Linux distributions often use systemd as the init system.

Runlevels define the state of the system:

  • 0: Shutdown
  • 1: Single-user mode (maintenance)
  • 2: Multi-user mode without networking
  • 3: Multi-user mode with networking (text)
  • 5: Multi-user mode with networking and GUI
  • 6: Reboot

In systemd systems, runlevels are replaced by targets:

  • poweroff.target (runlevel 0)
  • rescue.target (runlevel 1)
  • multi-user.target (runlevel 3)
  • graphical.target (runlevel 5)
  • reboot.target (runlevel 6)

Check your current target:

systemctl get-default

Linux File Types

Linux supports several types of files, each serving a specific purpose:

Linux File Types

  1. Regular Files (-):

    • Text files or binary files
  2. Directories (d):

    • Containers for other files and directories
  3. Symbolic Links (l):

    • Pointers to other files or directories
  4. Special Files:

    • Character Device Files (c): Handle data as character streams
    • Block Device Files (b): Handle data in fixed-size blocks
  5. Sockets (s):

    • Facilitate inter-process communication
  6. Named Pipes/FIFOs (p):

    • Allow unidirectional data flow between processes

Use the file command to identify file types:

file filename

To see file types in a directory listing, use:

ls -l

Filesystem Hierarchy Standard

The Linux filesystem is organized according to the Filesystem Hierarchy Standard (FHS), which defines the purpose and contents of each directory:

Filesystem Hierarchy

  • / (Root): The top-level directory
  • /bin: Essential user command binaries
  • /boot: Boot loader files and kernel
  • /dev: Device files
  • /etc: System configuration files
  • /home: User home directories
  • /lib, /lib64: Essential shared libraries
  • /media: Mount points for removable media
  • /mnt: Temporary mount points
  • /opt: Optional application software
  • /proc: Virtual filesystem for process information
  • /root: Home directory for the root user
  • /run: Runtime variable data
  • /sbin: Essential system binaries
  • /srv: Data for services provided by the system
  • /sys: Virtual filesystem for system information
  • /tmp: Temporary files
  • /usr: Secondary hierarchy for user data
  • /var: Variable data (logs, databases, etc.)

This organization ensures consistency across Linux distributions and helps users and administrators locate specific files and directories.

Table of Contents