Mastering Shell Basics

Simangaliso Vilakazi
3 min readMay 7, 2024

Before we start, it’s important to have a basic understanding of the shell and shell scripting. If you’re new to this, I recommend going through an introductory tutorial or reading the official Bash documentation, just like I did.

The shell is a program that lets you control your computer by typing commands. It’s a powerful tool that can make your work faster and easier. I recently learned how to use the shell, and I want to share what I’ve learned with you. The aim for this guide is to teach you how to use shell scripts to navigate folders, manage files and perform other basic tasks.

The Shell Basic Project (which you can find on my github) is a collection of scripts that cover many essential operations. I will use this project as reference to this whole article. From printing the current folder you’re in to creating links and moving files, this project gave me hands-on experience with shell scripting. I believe it can help you learn too. BUT again, I will ask you to please to go through some other introductory tutorial.

Project Structure

The project is divided into individual scripts, each one doing a specific task. Here are some of the scripts and what they do, along with the examples I used:

Navigating Folders:

  • current_working_directory — Prints the path of the folder you’re currently in.

Example:

#!/bin/bash
pwd
  • bring_me_home — Takes you to your home folder.

Example:

#!/bin/bash
cd ~
  • back — Takes you to the previous folder you were in.

Example:

#!/bin/bash
cd -

Listing Files and Folders:

  • listit — Shows the contents of the current folder.

Example:

#!/bin/bash
ls
  • listfiles — Shows the contents of the current folder in a long format.

Example:

#!/bin/bash
ls -l
  • listmorefiles — Shows all the files and folders in the current folder, including hidden ones.

Example:

#!/bin/bash
ls -a
  • listfilesdigitonly — Displays the current directory contents with user and group IDs shown numerically.

Example:

#!/bin/bash
ls -n
  • lists — Lists all files, including hidden ones, in the current directory, parent directory, and the /boot directory, in long format.

Example:

#!/bin/bash
ls -la . .. /boot
  • commas — Lists all files and directories in the current directory, separated by commas.

Example:

#!/bin/bash
ls -m

Managing Files and Folders:

  • firstdirectory — Creates a new folder <foldername> inside the /tmp/folder.

Example:

#!/bin/bash
mkdir /tmp/<foldername>
  • movethatfile — Moves the file <filename> from /tmp/ to /tmp/<foldername>.

Example:

#!/bin/bash
mv /tmp/<filename> /tmp/<foldername>
  • firstdelete — Deletes the <filename> in /tmp/<foldername>.

Example:

#!/bin/bash
rm /tmp/<foldername>/<filename>

Other Tasks:

  • file_type — Tells you what type of file “<filename>” is.

Example:

#!/bin/bash
file <filename>
  • symbolic_link — Creates a link called “ls” that points to the /bin/ls program.

Example:

#!/bin/bash
ln -s /bin/ls __ls__
  • copy_html — Copies all HTML files from current folder to the parent folder.

Example:

#!/bin/bash
cp *.html ..
  • lets_move — Moves all files beginning with an uppercase letter to the /tmp/u directory.

Example:

#!/bin/bash
mv [A-Z]* /tmp/u

Usage:

To run a script, type “./script_name” in the shell. For example, to print the current folder, you would type “./current_working_directory”. But before doing that you need to make sure that the file is executable first. And to make the file executable, you need to run the command “chmod +x”. I will go in dept about why you need to do this but for now, we goin to focus on running the script.

Conclusion

The project helped me learn how to use the shell to do basic tasks. By working through these scripts, I gained hands-on experience with navigating folders, managing files and folders, and automating various operations. Whether you’re a beginner or an experienced user, I hope this helps you improve your shell scripting skills.

--

--

Simangaliso Vilakazi
Simangaliso Vilakazi

No responses yet