Fundamentals of the Command Line Interface (CLI)
From the quiz curriculum
Fundamentals of the Command Line Interface (CLI)
TL;DR
The command line interface (CLI) is a text-based way to tell your computer what to do directly. You type commands, and the computer executes them, making it a powerful tool for managing files, running programs, and automating tasks. Learning a few core commands lets you navigate and interact with your system efficiently.
1. The Mental Model
Think of your computer's operating system like a house. The graphical interface (what you usually see) is like walking through the house, opening doors, and pointing at things. The CLI is like calling out instructions to a very fast, obedient assistant who knows exactly where everything is.
2. The Core Material
The command line lets you interact with your computer by typing text commands. You'll typically use a program called a "terminal," "console," or "shell" (like Bash, Zsh, or PowerShell) to do this. Each command generally follows a pattern: command [options] [arguments].
command: The action you want to perform (e.g.,lsto list files,cdto change directories).options(or flags): Modify the command's behavior, usually starting with a hyphen (e.g.,-lfor a "long" list,-afor "all" files).arguments: What the command acts upon (e.g., a file name, a directory path).
Navigating Your File System

Photo by Zulfugar Karimov on Pexels
Your computer organizes files into a hierarchical structure of directories (folders). The CLI works by you being "inside" a specific directory, called your current working directory.
-
pwd(Print Working Directory): Tells you where you currently are.
bash pwd # Example Output: /Users/yourusername/Documents -
ls(List): Shows the contents of your current directory.ls -l: Shows a "long" list with more details (permissions, owner, size, date).ls -a: Shows all files, including hidden ones (which start with a dot, like.bashrc).ls -la: Combines both options.
```bash
ls
Example Output: my_report.txt photos/ project_ideas.md
ls -l
Example Output:
-rw-r--r-- 1 youruser staff 1234 May 15 10:30 my_report.txt
drwxr-xr-x 3 youruser staff 96 Apr 20 14:00 photos
-rw-r--r-- 1 youruser staff 567 May 10 11:15 project_ideas.md
```
-
cd(Change Directory): Moves you to a different directory.cd [directory_name]: Moves into a subdirectory.cd ..: Moves up one level to the parent directory.cd ~: Moves to your home directory (a common starting point).cd /: Moves to the root directory (the very top of your file system).
```bash
pwd
Output: /Users/yourusername/Documents
cd photos
pwdOutput: /Users/yourusername/Documents/photos
cd ..
pwdOutput: /Users/yourusername/Documents
cd ~
pwdOutput: /Users/yourusername
```
Managing Files and Directories

Photo by Pixabay on Pexels
Once you can navigate, you'll want to manipulate files and folders.
-
mkdir(Make Directory): Creates a new directory.
bash mkdir new_project ls # Output: my_report.txt new_project/ photos/ project_ideas.md -
touch(Create Empty File): Creates an empty file or updates a file's timestamp.
bash touch notes.txt ls # Output: my_report.txt new_project/ notes.txt photos/ project_ideas.md -
cp(Copy): Copies files or directories.cp [source] [destination]cp -r [source_directory] [destination_directory](for copying directories,-rmeans "recursive")
```bash
cp notes.txt new_project/daily_notes.txt
Now, new_project/daily_notes.txt is a copy of notes.txt
```
-
mv(Move/Rename): Moves files or directories, or renames them.mv [source] [destination]
bash mv project_ideas.md archived_ideas.md # Renames the file mv my_report.txt new_project/ # Moves my_report.txt into new_project
-
rm(Remove): Deletes files or directories. Use with caution, asrmdoesn't usually send files to a trash bin!rm [file_name]rm -r [directory_name](for deleting directories,-ris recursive)rm -rf [directory_name](force removal, recursive, ignores non-existent files – EXTREMELY DANGEROUS!)
```bash
touch temp_file.txt
rm temp_file.txt
temp_file.txt is now gone
```
Understanding the Flow of Commands

Photo by Godfrey Atima on Pexels
graph TD
A["You type a command"] --> B["Shell (e.g., Bash) interprets it"]
B --> C{Is it a built-in command?};
C -- Yes --> D["Shell executes the command directly"];
C -- No --> E{Is it an executable program on the system PATH?};
E -- Yes --> F["Shell finds and runs the program"];
E -- No --> G["'command not found' error"];
D --> H["Command completes (produces output/changes system)"];
F --> H;
3. Worked Example
Let's say you've just downloaded a zip file called my_data.zip to your Downloads directory, and you want to extract it, move the extracted folder to a new data_projects folder in your Documents, and then clean up by deleting the original zip file.
- Open your terminal.
- Go to your home directory:
bash cd ~ pwd # Output: /Users/yourusername - Navigate to your Downloads folder:
bash cd Downloads ls # You should see 'my_data.zip' here - Unzip the file. (Most systems have
unzippre-installed. If not, you might needsudo apt install unziporbrew install unzip). This creates a new folder, let's saymy_data_folder.
bash unzip my_data.zip ls # Output: my_data.zip my_data_folder/ - Move back up to your home directory, then into Documents:
bash cd ~ cd Documents - Create a new directory for your data projects:
bash mkdir data_projects ls # Output: data_projects/ (and other existing files/folders) - Move the extracted folder (
my_data_folder) fromDownloadsintodata_projects. Remember, we're currently inDocuments. The full path to the source is important.
bash mv ~/Downloads/my_data_folder ./data_projects/ # The `.` means "the current directory" - Verify it's there:
bash ls data_projects/ # Output: my_data_folder/ - Go back to Downloads and clean up the original zip file:
bash cd ~/Downloads rm my_data.zip ls # Output: (my_data.zip should be gone)
You've now successfully downloaded, extracted, moved, and cleaned up using only the command line!
4. Key Takeaways
- The CLI provides a direct, text-based way to control your computer, often faster than clicking.
- Your "current working directory" is important;
pwdshows you where you are, andcdmoves you around. lsis your go-to command for seeing what's in a directory, withls -landls -aproviding more detail.mkdir,touch,cp,mv, andrmare essential for creating, copying, moving, and deleting files and directories.- Always be careful with
rm, especially with the-rand-foptions, as deleted files are often unrecoverable.
Common Mistakes to Avoid

Photo by KATRIN BOLOVTSOVA on Pexels
- Forgetting where you are: Always use
pwdif you're unsure of your current directory. - Incorrect paths: Make sure you're specifying the correct full or relative path for files and directories.
- Typos in commands or file names: The CLI is very precise; even a single mistyped character will cause an error.
- Using
rm -rfindiscriminately: This can quickly delete important files or your entire system without warning.
5. Now Try It
Open your terminal. Navigate to your home directory, then create a new folder called my_cli_practice. Inside my_cli_practice, create two empty files named shopping_list.txt and todo.md. Copy shopping_list.txt to a new file named groceries.txt within the same folder. Then, create a new subfolder called archive, and move todo.md into archive. Finally, list the contents of both my_cli_practice and my_cli_practice/archive to confirm your changes.
Success looks like:
When you run ls -R my_cli_practice, you should see:
my_cli_practice:
archive/
groceries.txt
shopping_list.txt
my_cli_practice/archive:
todo.md
Frequently asked about Fundamentals of the Command Line Interface (CLI)
More from quiz
Get the full quiz curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account