Bash Scripting Series — Level 1
Bash Scripting for Beginners — Level 1: Linux Command-Line Foundations
Series: Bash Scripting for Beginners
Level: 1 — Beginner
Environment: Ubuntu Terminal
Focus: File listing, navigation, copying, permissions, searching, and user management
Introduction
Welcome to Level 1 of this Bash scripting series!
Before writing scripts that automate tasks, you need to understand how to interact with Linux through the command line. The terminal allows you to navigate directories, inspect files, manage permissions, search the filesystem, and administer user accounts.
These skills are especially useful in cybersecurity, where you may need to investigate files, inspect permissions, manage Linux systems, or automate repetitive tasks.
In this lesson, we'll work through seven practical exercises using Ubuntu.
What you'll learn
By the end of this lesson, you should be able to:
List files and inspect their permissions.
Navigate a directory structure using relative paths.
Copy files using commands and wildcards.
Create directories and modify permissions and ownership.
Remove files and directories safely.
Search for files and directories using
find.Create, switch between, and remove user accounts.
1. Listing Files
The ls command displays the contents of a directory. It is one of the first commands you should learn because it helps you understand what's available in your current location.
1.1 List files in your home directory
ls ~The ~ symbol represents your home directory. This command lists its contents without requiring you to navigate there first.
You can also use:
cd ~
lsHere, cd changes your current directory, and ls lists its contents.
1.2 Display a long listing
To display additional information about files and directories, use:
ls -l ~The -l option displays a long listing, including permissions, ownership, file size, modification time, and filename.
For example:
-rw-r--r-- 1 user user 120 Oct 10 10:30 notes.txt
drwxr-xr-x 2 user user 4096 Oct 10 10:35 DocumentsThe first character indicates the type of entry: - represents a regular file, while d represents a directory. The following characters show the permission bits.
Useful ls options
Sort by modification time:
ls -lt ~The -t option sorts entries by modification time, with the newest first.
Sort by file size:
ls -lS ~The uppercase -S option sorts by file size, with the largest first.
Reverse the sorting order:
ls -ltr ~
ls -lSr ~The -r option reverses the selected sort order.
Display hidden files:
ls -a ~Linux filenames beginning with a dot, such as .bashrc, are hidden by default. The -a option includes them.
Display usage instructions:
ls --helpThis displays information about the command's available options.
Exercise 1 — Quick review
| Task | Command or option |
|---|---|
| List the home directory | ls ~ |
| Display detailed information | ls -l ~ |
| Sort by modification time | -t |
| Sort by size | -S |
| Reverse the sorting order | -r |
| Include hidden files | -a |
| Display usage instructions | ls --help |
2. Navigating the Filesystem
Linux organizes files and directories in a hierarchical structure. The cd command lets you move between directories.
For this exercise, we'll use a repository containing 1,000 text files organized into nested directories.
2.1 Download and extract the exercise files
Open your Ubuntu Terminal and run:
git clone https://github.com/iangmitchell/thousandFiles
cd thousandFiles
tar -xvf 1000Files.tarThe commands:
git clonedownloads the repository.cd thousandFilesenters the downloaded directory.tar -xvfextracts the archive.
Now enter the extracted directory:
cd 1000FilesCheck your current location:
pwdThe pwd command displays the full path of your current working directory.
2.2 Navigate to 871.txt
The exercise files use nested directories. For example, 000.txt is located at 0/0/000.txt.
Using the same directory structure, navigate to the directory containing 871.txt:
cd 8/7
lsTo return to the 1000Files directory, move up two levels:
cd ../..Now navigate to the directory containing 230.txt:
cd 2/3
lsRemember that you navigate into the directory containing a file; you cannot use cd to enter a regular text file itself.
2.3 List files across nested directories
If you want to see the directory contents recursively, use:
ls -RThe -R option lists directories and their contents throughout the directory tree.
To include hidden entries as well, use:
ls -RAExercise 2 — Quick review
Navigate to the directory containing
871.txt:cd 8/7Return to
1000Files:cd ../..Navigate to the directory containing
230.txt:cd 2/3Recursively list directory contents:
ls -R
3. Copying Files
The cp command copies files and directories. You can copy individual files, select files using wildcards, or recursively copy directory contents.
First, return to the main exercise directory:
cd ~/thousandFiles/1000Files
pwdYour exact path may differ depending on where you cloned the repository.
3.1 Copy an individual file
Copy 000.txt into the current directory:
cp 0/0/000.txt .The dot (.) means the current working directory.
Next, copy 871.txt:
cp 8/7/871.txt .Verify that the files exist in the current directory:
ls -l 000.txt 871.txt3.2 Copy files beginning with 67
A wildcard lets you match filenames using a pattern. The * character matches any sequence of characters.
To copy the files beginning with 67 into the current directory, use:
cp 6/7/67*.txt .The pattern 67*.txt matches text filenames beginning with 67 and ending with .txt.
3.3 Copy files beginning with 5
To copy the text files beginning with 5 from their nested directories into the current directory, use:
cp -R 5/*/*.txt .The -R option enables recursive copying. The pattern selects text files beneath the first-level directories inside 5.
3.4 Copy files beginning with 4 into another directory
Copy the files beginning with 4 into 2/4:
cp -R 4/*/*.txt 2/4/Now inspect the destination:
ls -l 2/4/This lets you verify whether the copy operation succeeded.
Important: cp can overwrite an existing file if the destination contains a file with the same name. Check your destination before copying important data.
Exercise 3 — Quick review
| Task | Command |
|---|---|
Copy 000.txt | cp 0/0/000.txt . |
Copy 871.txt | cp 8/7/871.txt . |
Copy files beginning with 67 | cp 6/7/67*.txt . |
Copy files beginning with 5 | cp -R 5/*/*.txt . |
Copy files beginning with 4 to 2/4 | cp -R 4/*/*.txt 2/4/ |
4. Creating Directories and Managing Permissions
Linux permissions control who can read, modify, or access files and directories.
4.1 Create a directory
From the 1000Files directory, run:
mkdir thisDirectoryThe mkdir command creates a new directory.
Inspect its permissions:
ls -ld thisDirectoryThe -d option displays information about the directory itself rather than listing its contents.
4.2 Change the permissions to 540
Run:
chmod 540 thisDirectoryThe chmod command changes permissions.
Linux represents permissions numerically:
Read (
r) = 4Write (
w) = 2Execute (
x) = 1
Each digit represents permissions for a different group:
| Digit | Applies to | Permissions |
|---|---|---|
5 | Owner | Read and execute |
4 | Group | Read only |
0 | Others | No permissions |
Therefore, 540 means the owner can read and access the directory, the group has read permission without execute permission, and other users have no permissions.
For directories, execute permission is required to traverse the directory and access entries inside it. Group members with only read permission generally cannot access those entries.
Verify the result:
ls -ld thisDirectory4.3 Change ownership to root
To change the directory's owner to root, run:
sudo chown root:root thisDirectoryThe sudo command runs the command with elevated privileges. The chown command changes ownership, and root:root specifies both the owner and the group.
Verify the new ownership:
ls -ld thisDirectoryYou should now see root as the owner and group.
5. Removing Files and Directories
The rm command removes files. Removing directories requires additional care because a directory can contain many files.
Be careful with deletion commands. Confirm your current location before executing them.
5.1 Remove a file
rm 000.txtThis removes 000.txt from the current directory.
5.2 Remove all .txt files in the current directory
rm -- *.txtThis deletes matching text files directly inside 1000Files. It does not recursively delete text files inside its subdirectories.
If you need to delete text files recursively, a different command is required, such as find . -type f -name '*.txt' -delete. Use that only when you deliberately intend to delete matching files throughout the directory tree.
5.3 Remove thisDirectory
If the directory is empty, use:
rmdir thisDirectoryrmdir removes empty directories. If your permissions or ownership settings prevent the operation, inspect them and use elevated privileges only if necessary.
5.4 Remove the directory named 9
First, verify that you are in the intended exercise directory:
pwd
ls 9If you intend to delete the directory and everything inside it, use:
rm -r 9The -r option recursively removes directory contents.
This is a destructive operation. Do not run it outside the exercise environment or against a path you have not checked.
6. Finding Files and Directories
The find command searches for files and directories based on criteria such as names, types, sizes, and directory depth.
For these exercises, work from the 1000Files directory.
6.1 Find all subdirectories named 3
find . -type d -name '3'Here:
.means search from the current directory.-type dselects directories.-name '3'matches directories named exactly3.
6.2 Find files larger than 1 MiB
find . -type f -size +1MThe -type f option selects regular files. The -size +1M expression matches files larger than the specified size threshold in GNU find's binary-megabyte units.
6.3 Find files that are exactly 3 bytes
find . -type f -size 3cThe suffix c means bytes. This command finds regular files with a size of exactly three bytes.
6.4 Find files directly inside the current directory
find . -maxdepth 1 -type fThe -maxdepth 1 option prevents find from descending into subdirectories. This lists regular files directly inside the current directory.
To see both files and directories at that level, use:
find . -maxdepth 1Exercise 6 — Quick review
| Task | Command |
|---|---|
Find directories named 3 | find . -type d -name '3' |
| Find files larger than 1 MiB | find . -type f -size +1M |
| Find files exactly 3 bytes long | find . -type f -size 3c |
| Find files directly in the current directory | find . -maxdepth 1 -type f |
7. Linux User Management
Linux supports multiple users, each with their own account and permissions. Ubuntu provides commands to create accounts, change passwords, switch users, and remove accounts.
These tasks require appropriate administrative privileges.
7.1 Create a new user
Create a test account named labuser:
sudo adduser labuserUbuntu will guide you through creating the account and setting its password.
7.2 Change your current password
passwdThis changes the password of the currently logged-in user. You will normally be prompted to provide your current password before setting a new one.
7.3 Switch to the new user
su - labuserEnter the password for labuser when prompted.
The - option starts a login shell for that user, loading their login environment.
7.4 Switch back to your previous user
exitThis exits the labuser shell and returns you to your previous shell session.
7.5 Remove the test account and its home directory
First, ensure that you have exited the labuser session. Then run:
sudo deluser --remove-home labuserThis removes the account and its home directory.
Security note: Do not manually edit /etc/passwd to remove a user. Use the account-management tools supplied by Ubuntu. They update the relevant system account records properly and can remove the associated home directory when requested.
Level 1 Challenge
Before moving to Level 2, try completing these tasks without looking at the examples above:
List the hidden files in your home directory in long format.
Find every directory named
4beneath your exercise directory.Copy a file into a new directory you create.
Change a directory's permissions to
750, then explain what each digit means.Find every text file exactly 10 bytes in size.
Create a temporary Linux user, switch to it, return to your original user, and remove the temporary account.
Try each task in the Ubuntu Terminal and verify the result rather than simply assuming the command succeeded.
Conclusion
In Level 1, you learned the fundamentals of interacting with Linux through the terminal: listing files, navigating directories, copying and deleting data, changing permissions and ownership, searching the filesystem, and managing users.
These commands are also building blocks for future Bash scripts. In the next levels, we can start combining commands with variables, conditions, loops, functions, and user input to automate real tasks.
The goal isn't just to memorize commands. It's to understand what each command does, when to use it, and how to verify its result.
Comments