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 $SHELL

This 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 /home

Here, 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 -a

The -a option requests all available system information from the command.

To learn more about your CPU architecture, run:

lscpu

This 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/Documents

identifies 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/Documents

A relative path specifies a location in relation to your current working directory.

Example:

Documents

The meaning of a relative path depends on where you are currently located.

Finding Your Current Directory

Use pwd to display your current working directory:

pwd

Changing Directories

The cd command allows you to move between directories.

cd Documents

To return to your home directory:

cd ~

To move up one directory level:

cd ..

To navigate directly to an absolute path:

cd /home/user/Documents

Listing Files and Directories

The ls command displays directory contents.

ls

To display hidden files as well:

ls -a

Other useful variations include:

ls -l
ls -lt
ls -lS
ls -lrS
ls -lrt

These 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:

  1. Display your current directory.

  2. Navigate to your home directory.

  3. List its contents, including hidden files.

  4. Create a new directory called bash-practice.

  5. Enter that directory and confirm your location.

Useful commands:

pwd
cd ~
ls -a
mkdir bash-practice
cd bash-practice
pwd

4. 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 NewFolder

To create nested directories in one command, use the -p option:

mkdir -p tools/index/helper-scripts

Creating Empty Files

The touch command creates an empty file if it does not already exist.

touch script.sh

You can create multiple files in one command:

touch file1.txt file2.txt file3.txt

Removing Files and Directories

Use rm to remove a file:

rm file1.txt

To remove an empty directory, use:

rmdir NewFolder

The rm -r command removes a directory and its contents recursively:

rm -r OldFolder

Important: 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.txt

This 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.txt

The 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 +250M

Find text files modified within the last ten days:

find /path/to/search -type f -name "*.txt" -mtime -10

In 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.txt

For larger files, less provides an interactive way to navigate the contents:

less file.txt

You can use head to display the first ten lines:

head file.txt

To display the first five lines:

head -n 5 file.txt

Use tail to display the last ten lines by default:

tail file.txt

To display the last 50 lines:

tail -n 50 file.txt

These 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.txt

You can also request specific counts:

wc -l file.txt
wc -w file.txt
wc -c file.txt

Comparing Files

The diff command compares two files and shows their differences.

diff file1.txt file2.txt

To report whether the files differ without displaying the full differences:

diff -q file1.txt file2.txt

Comparing 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 ls

The 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.sh

You 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.sh

Vim 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:

:wq

To 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 update

Upgrade installed packages:

sudo apt upgrade

The 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 htop

Remove it when you no longer need it:

sudo apt remove htop

Installing Local Packages

The dpkg command can install local Debian package files.

sudo dpkg -i package_name.deb

Replace 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_URL

This downloads a repository, including its version history.

Wget:

wget FILE_URL

This downloads a file from a URL.

Curl:

curl -O FILE_URL

The -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/directory

Create a gzip-compressed archive:

tar -czvf archive.tar.gz /path/to/directory

Extract a .tar archive:

tar -xvf archive.tar

Extract a .tar.gz archive:

tar -xzvf archive.tar.gz

The 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:

  1. Superuser: The root account has the highest level of system access and uses UID 0.

  2. System user: An account used to run system services.

  3. Regular user: An account intended for ordinary interactive use.

Identifying Users and Groups

The id command displays your user ID and group information.

id

To inspect another user's information:

id username

A 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 -l

Linux 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.sh

Here, u represents the owner and +x adds execute permission.

Remove read and write permissions for the group and others:

chmod go-rw file.txt

Using numeric notation, permissions are represented by three octal digits. Each digit corresponds to the owner, group, or others.

For example:

chmod 451 file.txt

This 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.txt

To change ownership recursively for a directory and its contents:

sudo chown -R username /path/to/directory

These operations may require elevated privileges.

Managing User Accounts

Create a user account:

sudo adduser user2

Change a user's password:

sudo passwd username

Switch to another user account:

su username

Delete a user account while retaining the home directory:

sudo userdel username

To delete the account and its home directory:

sudo userdel -r username

Important: 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.

  1. Create a directory named bash-practice.

  2. Inside it, create three text files and one file named hello.sh.

  3. Use ls -l to inspect the files.

  4. Write a few lines of text into one of the text files using Nano.

  5. Display its contents with cat and less.

  6. Create a copy of the text file and compare the two files using diff.

  7. Search for all .sh files inside your practice directory using find.

  8. Change the permissions of hello.sh using chmod.

  9. Read the manual page for one unfamiliar command using man.

  10. 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

Popular posts from this blog

Information disclosure in version control history

Linux AAA

Hashcat LAB