A Guide to Basic Bash Commands and How to Use Them (cd, ls, echo, etc.)
In the previous articles, you got your Bash environment ready to go (either on macOS or with WSL on Windows). Now, it's finally time to step into the world of Bash for real! Have you ever dreamed of looking like a "hacker," controlling your computer at will by typing commands from a keyboard? The basic commands you'll learn today are the first step toward that dream.
In this article, we'll focus on the truly important basic commands used daily in web development, explaining the meaning and usage of each one carefully. Just as a chef learns how to use their knives, mastering these basic tools first will open the door to all sorts of advanced techniques. Each command comes with a sample that you can copy and paste directly to run. We encourage you to get your hands dirty in your terminal and get a feel for command-line operations!
1. Navigation Commands - Become a File System Explorer
The foundation of terminal operations is first knowing "where you are" and then "moving to your destination." These commands are your "feet" in the CUI world.
pwd - Check Your Current Location
pwd (Print Working Directory) displays the absolute path (the full location from the root) of the directory you are currently in. Just like looking at a compass when you're lost in a forest, if you get lost in the terminal, type pwd first.
$ pwd
/Users/yourname/Documents/Projects
cd - Change Directory
cd (Change Directory) is, as its name suggests, the command for moving to a different directory (folder).
Entering a specific folder:
$ cd website-v1
Returning to the parent directory:
$ cd ..
Instantly returning to your home directory:
$ cd
This has the same meaning as typing cd ~. No matter how deep you are in the directory structure, this one command will take you back home.
2. Listing Commands - Looking Around Your Environment
Once you've arrived at your destination, the next thing you'll want to know is what's there. The ls command acts as your "eyes," telling you about your surroundings.
ls - List files and folders
Running ls (List) by itself will display a list of the names of files and folders in the current directory.
$ ls
css images index.html js
Customizing the display with options
ls can be switched to various display methods by adding "options."
Displaying detailed information (-l):
$ ls -l
total 8
drwxr-xr-x 2 user staff 64 Jul 6 10:00 css
drwxr-xr-x 3 user staff 96 Jul 6 10:00 images
-rw-r--r-- 1 user staff 512 Jul 6 10:01 index.html
drwxr-xr-x 2 user staff 64 Jul 6 10:00 js
You can see detailed information like permissions, owner, file size, and modification date.
Displaying all files, including hidden ones (-a):
This also displays "hidden files" that start with a dot, like .htaccess.
$ ls -a
. .. .htaccess css images index.html js
. is a special symbol that refers to the current directory, and .. refers to the parent directory.
Options can be combined:
$ ls -al
3. Manipulation Commands - Create and Change Your World
Now that you can move around and check your surroundings, let's try actually creating, copying, renaming, and deleting files and folders. These are your "hands" in the CUI world.
mkdir - Create a folder
mkdir (Make Directory) creates a new folder.
$ mkdir assets
touch - Create an empty file
The touch command can update a timestamp or, if the file doesn't exist, create an empty one.
$ touch contact.html
cp - Copy files or folders
cp (Copy) duplicates files or folders.
Copying a file:
$ cp index.html index_backup.html
Copying a folder (requires the -r option):
$ cp -r css/ css_backup/
mv - Move or rename files and folders
mv (Move) moves files and folders. It's also used for renaming.
Moving a file:
$ mv contact.html pages/
Renaming a file (think of it as moving it to the same location with a different name):
$ mv index_backup.html index_old.html
rm - Remove files or folders
rm (Remove) is the delete command. [SUPER, SUPER IMPORTANT NOTE!] Unlike the GUI's trash bin, anything deleted with rm is gone forever and cannot be recovered. Before running it, take a deep breath and be as cautious as pointing to the file to confirm it's the right one.
Deleting a file:
$ rm index_old.html
Deleting a folder (requires the -r option):
$ rm -r css_backup/
4. Commands for Viewing File Content
Finally, let's introduce a few commands for checking the contents of files. This is an essential skill for server management, such as checking logs.
cat - Display an entire file at once
cat is useful when you want to quickly check the contents of a short file.
$ cat index.html
less - View long files by scrolling
less is extremely useful for viewing long files like log files. You can scroll with the up and down arrow keys and press the q key to quit.
$ less /var/log/apache2/access_log
head / tail - View only the beginning or end of a file
head displays the beginning part of a file, and tail displays the end part. They are frequently used when you want to check the latest logs, for example.
$ tail -n 20 access_log
-n 20 means "display the last 20 lines."
Conclusion
Great work! This time, we've taken a whirlwind tour of the basic command set that is unavoidable when using Bash. While each command is simple, combining them allows for work that is faster and more efficient than you could imagine with a GUI.
The quickest way to let what you've learned today sink in is to intentionally use the terminal for simple tasks you usually do with the GUI. Even for simple operations like "create a new folder and copy a file into it," making a habit of executing them with commands will help your fingers remember them naturally.
With this, you have learned the basic "vocabulary" of the Bash language. The next step is to combine these words to form "sentences"βin other words, the world of Bash scripting. In the next article, we'll learn how to write simple automation scripts and explore ways to make your work even easier!
Introduction to Bash Scripting: Simple Automation Script Examples to Write First