Linux 101
Linux 101: Essential Prerequisites for the Bash Scripting Series
Before jumping into Bash scripting, it is important to understand the environment in which your scripts will run. Bash is a command-line shell used on Linux systems, and learning to work with Linux will make writing, testing, and troubleshooting scripts much easier.
This guide covers the essential Linux fundamentals you should understand before starting the Bash scripting series. We will explore the Linux command line, file-system navigation, file management, text editors, package management, users, and file permissions.
You do not need to be a Linux expert before getting started. The goal is to become comfortable using the terminal and understanding the commands you will encounter throughout the series.
What You Will Learn
By the end of this guide, you should be familiar with:
Linux distributions and how to set up a Linux environment.
The Bash shell and basic command structure.
Identifying system information and CPU architecture.
Navigating the Linux file system.
Creating, viewing, searching, copying, moving, and deleting files.
Using command-line text editors.
Managing software packages and archives.
Understanding Linux users, groups, ownership, and permissions.
Estimated time: 2 hours
1. Getting Started with Linux
What Is a Linux Distribution?
A Linux distribution, commonly called a distro, is an operating system built around the Linux kernel and bundled with system utilities and software.
Some popular Linux distributions include:
Ubuntu: A beginner-friendly distribution widely used for general computing, development, and server environments.
Linux Mint: A distribution based on Ubuntu that emphasizes ease of use.
Fedora: A distribution known for providing recent software and development tools.
Arch Linux: A lightweight and flexible distribution suited to users who prefer a hands-on approach.
Red Hat Enterprise Linux (RHEL): A commercial distribution used in enterprise environments.
Kali Linux: A distribution focused on cybersecurity, penetration testing, and security research.
For this guide, Ubuntu is a suitable starting point.
Setting Up Your Linux Environment
If you are using Windows, you can run Linux inside a virtual machine using software such as Oracle VirtualBox or VMware.
A virtual machine allows you to use Linux as a guest operating system without replacing your existing Windows installation.
For this lab, install Ubuntu in a virtual machine by following your Linux installation guide. Once the installation is complete, open the terminal and prepare to practice the commands in the following sections.
Practical tip: Create a dedicated folder for your weekly exercises and save your scripts, commands, notes, and solutions there. This will help you revisit your work as you progress through the series.
2. Understanding the Bash Shell
The Linux command line is accessed through a program called a shell. The shell accepts commands and provides an interface for interacting with the operating system.
One commonly used shell is Bash, which stands for Bourne Again Shell.
To check your current shell, open the terminal and run:
echo $SHELLThis prints the path of your configured login shell.
Understanding Command Structure
Linux commands generally follow this structure:
command [options] [arguments]Each part has a purpose:
Command: The program you want to execute.
Options: Flags that modify how the command behaves.
Arguments: The files, directories, or other inputs on which the command operates.
For example:
ls -l /homeHere, ls is the command, -l is the option, and /home is the argument.
Understanding this structure is essential for Bash scripting because scripts are built around executing commands, supplying arguments, and controlling their behavior.
Discovering Your System Information
The uname command displays information about your system.
uname -aThe -a option requests all available system information from the command.
To learn more about your CPU architecture, run:
lscpuThis command displays details such as CPU architecture, supported operating modes, byte order, and the number of CPUs visible to the operating system.
These commands are useful when inspecting your environment or troubleshooting system-related issues.
3. Understanding the Linux File System
Linux organizes files and directories in a hierarchical structure that resembles an inverted tree.
At the top of this structure is the root directory, represented by /. Other directories and files exist beneath it.
A path such as:
/home/user/Documentsidentifies a directory within the file system.
Absolute Paths vs. Relative Paths
An absolute path specifies the complete path from the root directory. It starts with /.
Example:
/home/user/DocumentsA relative path specifies a location in relation to your current working directory.
Example:
DocumentsThe meaning of a relative path depends on where you are currently located.
Finding Your Current Directory
Use pwd to display your current working directory:
pwdChanging Directories
The cd command allows you to move between directories.
cd DocumentsTo return to your home directory:
cd ~To move up one directory level:
cd ..To navigate directly to an absolute path:
cd /home/user/DocumentsListing Files and Directories
The ls command displays directory contents.
lsTo display hidden files as well:
ls -aOther useful variations include:
ls -l
ls -lt
ls -lS
ls -lrS
ls -lrtThese options let you explore detailed file information and different sorting orders. Practice them and observe how the output changes.
Practice Exercise
Open your terminal and:
Display your current directory.
Navigate to your home directory.
List its contents, including hidden files.
Create a new directory called
bash-practice.Enter that directory and confirm your location.
Useful commands:
pwd
cd ~
ls -a
mkdir bash-practice
cd bash-practice
pwd4. Managing Files and Directories
File management is one of the most important Linux fundamentals. You will frequently use these commands when creating scripts, organizing output, and working with configuration files.
Creating Directories
Use mkdir to create a directory:
mkdir NewFolderTo create nested directories in one command, use the -p option:
mkdir -p tools/index/helper-scriptsCreating Empty Files
The touch command creates an empty file if it does not already exist.
touch script.shYou can create multiple files in one command:
touch file1.txt file2.txt file3.txtRemoving Files and Directories
Use rm to remove a file:
rm file1.txtTo remove an empty directory, use:
rmdir NewFolderThe rm -r command removes a directory and its contents recursively:
rm -r OldFolderImportant: These commands can permanently remove files. Check the target path carefully before executing a removal command.
Copying Files
The cp command copies files.
cp file1.txt file2.txtThis creates a copy named file2.txt in the current directory.
You can also copy a file to another directory:
cp file1.txt /home/user/Documents/Moving and Renaming Files
Use mv to move a file:
mv file1.txt backup/You can also use it to rename a file:
mv old_name.txt new_name.txtThe same command can move or rename directories.
Finding Files
The find command searches for files and directories.
Find a file named bash.sh beneath the current directory:
find . -type f -name "bash.sh"Search for shell scripts:
find . -type f -name "*.sh"Search for hidden files:
find . -type f -name ".*"Find files larger than 250 MB:
find /path/to/search -type f -size +250MFind text files modified within the last ten days:
find /path/to/search -type f -name "*.txt" -mtime -10In these examples, . represents the current directory, while -type f limits the search to regular files.
5. Viewing and Comparing File Contents
Linux provides several commands for inspecting text files. These will become especially useful when reading scripts, reviewing logs, and troubleshooting errors.
Displaying File Contents
Use cat to display a file:
cat file.txtFor larger files, less provides an interactive way to navigate the contents:
less file.txtYou can use head to display the first ten lines:
head file.txtTo display the first five lines:
head -n 5 file.txtUse tail to display the last ten lines by default:
tail file.txtTo display the last 50 lines:
tail -n 50 file.txtThese commands are particularly useful when examining log files and the output generated by scripts.
Counting Lines, Words, and Characters
The wc command counts lines, words, and bytes by default:
wc file.txtYou can also request specific counts:
wc -l file.txt
wc -w file.txt
wc -c file.txtComparing Files
The diff command compares two files and shows their differences.
diff file1.txt file2.txtTo report whether the files differ without displaying the full differences:
diff -q file1.txt file2.txtComparing files is useful when checking changes between script versions or reviewing differences in configuration files.
Reading Command Documentation
Linux commands often provide built-in documentation through manual pages.
For example:
man lsThe manual explains the command, its options, and its usage. Press q to exit the manual viewer.
Practice exercise: Create two text files with different contents, display each file, compare them using diff, and use wc to count their lines.
6. Using Text Editors in Linux
You need a way to create and edit files before writing Bash scripts. Three editors covered in this lab are gedit, nano, and vim.
gedit: A graphical text editor with programming-related features.
nano: A lightweight command-line editor that is relatively easy for beginners to use.
vim: A powerful terminal-based editor with different operating modes.
Getting Started with Nano
Create or open a file using:
nano script.shYou can begin writing text immediately.
Common keyboard shortcuts include:
Ctrl + O— save the file.Enter— confirm the filename when prompted.Ctrl + X— exit Nano.
If you have unsaved changes, Nano will ask whether you want to save them before exiting.
Getting Started with Vim
Open a file using:
vim script.shVim uses different modes, and understanding them is important.
Command mode: The default mode when Vim opens. It allows you to navigate and execute commands.
Insert mode: Used to enter or edit text. Press i while in command mode.
Visual mode: Used to select text. Press v to select characters, Shift + V to select lines, or Ctrl + V to select a block.
Press Esc to return to command mode.
To save your work and exit:
:wqTo exit without saving:
:q!If you are new to Linux, start with Nano and become comfortable creating, editing, and saving files before exploring Vim further.
7. Installing Software and Managing Packages
Linux distributions provide package managers that help you install, update, upgrade, and remove software.
Ubuntu uses APT, the Advanced Package Tool.
Updating and Upgrading Packages
Refresh the package lists:
sudo apt updateUpgrade installed packages:
sudo apt upgradeThe sudo command allows an authorized user to execute commands with elevated privileges.
Installing and Removing Packages
Install htop, an interactive process viewer:
sudo apt install htopRemove it when you no longer need it:
sudo apt remove htopInstalling Local Packages
The dpkg command can install local Debian package files.
sudo dpkg -i package_name.debReplace package_name.deb with the actual package filename.
Downloading Files and Projects
You may also encounter tools used to download files or retrieve projects from online repositories.
Git:
git clone REPOSITORY_URLThis downloads a repository, including its version history.
Wget:
wget FILE_URLThis downloads a file from a URL.
Curl:
curl -O FILE_URLThe -O option saves the downloaded file using its remote filename.
These tools serve different purposes, but all are useful when setting up development environments or retrieving resources.
Creating and Extracting Archives
The tar command combines files and directories into an archive.
Create a .tar archive:
tar -cvf archive.tar /path/to/directoryCreate a gzip-compressed archive:
tar -czvf archive.tar.gz /path/to/directoryExtract a .tar archive:
tar -xvf archive.tarExtract a .tar.gz archive:
tar -xzvf archive.tar.gzThe key options are:
-c— create an archive.-x— extract an archive.-v— display the files being processed.-f— specify the archive filename.-z— use gzip compression or decompression.
8. Linux Users, Groups, and Permissions
Linux uses user accounts and groups to manage access to the system and its resources.
Understanding User Types
There are three main account types covered in this lab:
Superuser: The root account has the highest level of system access and uses UID
0.System user: An account used to run system services.
Regular user: An account intended for ordinary interactive use.
Identifying Users and Groups
The id command displays your user ID and group information.
idTo inspect another user's information:
id usernameA group is a collection of accounts that can share access to files and other resources. Groups are identified by group IDs, or GIDs.
Understanding File Permissions
Use ls -l to display file permissions and ownership:
ls -lLinux permissions determine whether a user can read, write, or execute a file.
The main permission symbols are:
r— read.w— write.x— execute.
Permissions are commonly considered for three categories: the owner, the group, and others.
Changing Permissions with chmod
The chmod command modifies file permissions.
Using symbolic notation, you can add execute permission for the owner:
chmod u+x script.shHere, u represents the owner and +x adds execute permission.
Remove read and write permissions for the group and others:
chmod go-rw file.txtUsing numeric notation, permissions are represented by three octal digits. Each digit corresponds to the owner, group, or others.
For example:
chmod 451 file.txtThis sets the permissions to read for the owner, read and execute for the group, and execute for others.
Changing File Ownership
Use chown to change ownership:
sudo chown username file.txtTo change ownership recursively for a directory and its contents:
sudo chown -R username /path/to/directoryThese operations may require elevated privileges.
Managing User Accounts
Create a user account:
sudo adduser user2Change a user's password:
sudo passwd usernameSwitch to another user account:
su usernameDelete a user account while retaining the home directory:
sudo userdel usernameTo delete the account and its home directory:
sudo userdel -r usernameImportant: Be especially careful with commands that change ownership, modify permissions, or delete accounts and directories. Practice these operations in your own virtual machine rather than on a system you do not administer.
9. Final Hands-On Exercise
Before moving on to the Bash scripting series, complete this exercise in your Ubuntu environment.
Create a directory named
bash-practice.Inside it, create three text files and one file named
hello.sh.Use
ls -lto inspect the files.Write a few lines of text into one of the text files using Nano.
Display its contents with
catandless.Create a copy of the text file and compare the two files using
diff.Search for all
.shfiles inside your practice directory usingfind.Change the permissions of
hello.shusingchmod.Read the manual page for one unfamiliar command using
man.Record what each command does in your study notes.
Keep your work organized. These exercises will serve as a reference as you begin building scripts of your own.
Conclusion
Linux command-line proficiency is an important foundation for Bash scripting. Understanding directories, file manipulation, text editors, package management, and permissions will help you focus on the logic of your scripts rather than struggling with the environment.
You do not need to memorize every command immediately. Practice them, read their manual pages, experiment in your virtual machine, and keep notes on what you learn.
Once you are comfortable navigating Linux and working with the terminal, you will be better prepared to move on to variables, user input, conditions, loops, functions, and other Bash scripting concepts.
Next in the series: We will begin exploring Bash scripting itself, starting with the structure of a Bash script and how to execute it on Linux.
Keep practicing, keep experimenting, and remember: the best way to learn the command line is to use it.
Comments