Technology & Software
How to Use the Command Line

# How to Use the Command Line: A Beginner's Guide to Mastering the Basics The command line interface, or CLI, is often depicted in movies as a crypti...
How to Use the Command Line: A Beginner's Guide to Mastering the Basics
The command line interface, or CLI, is often depicted in movies as a cryptic, black screen with glowing green text, a tool reserved for elite hackers and programming prodigies. This image, however, couldn't be further from the truth. The command line is a powerful, text-based interface that allows you to communicate directly with your computer's operating system. It's an incredibly efficient tool for automating tasks, managing files, and running programs, offering a level of control and speed that a graphical user interface (GUI) often cannot match. Whether you're an aspiring developer, a systems administrator, or simply a curious user looking to enhance your tech skills, learning the command line is a fundamental step toward deeper computer literacy. This guide is designed to demystify the CLI, providing you with the foundational knowledge you need to start navigating your computer like a pro.
This comprehensive tutorial will guide you through the essential first steps to learn the command line. We will start by explaining what the command line is, why it's still incredibly relevant in today's visually-driven world, and how to access it on the three major operating systems: Windows, macOS, and Linux. You will learn how to read the command prompt and understand the basic syntax of commands. The core of this article will be a step-by-step exploration of the most fundamental and frequently used commands for file navigation and manipulation. You will learn how to move between directories, list the contents of a folder, create and delete files and folders, and copy and move items from one location to another. Each command will be explained in detail, with clear examples and notes on the subtle but important differences between the commands used on Windows versus those on macOS and Linux. By the end of this guide, you will have the confidence to open the terminal and perform basic file management tasks, paving the way for more advanced command line wizardry.
Getting Started: Accessing and Understanding the Command Line
Before you can start typing commands, you first need to know what you're working with and how to open the application. The command line interface is a program that takes your text-based commands and passes them to the computer's operating system to execute. While the concept is the same across platforms, the specific application and some of the commands will differ slightly. This section will help you locate and open the correct tool for your system and understand the basic structure of the command prompt itself.
Opening the Command Line Interface
Your gateway to the command line is an application called a terminal or terminal emulator. Finding and opening it is a straightforward process on any operating system.
Windows
On Windows, you have two primary command line interfaces built-in: Command Prompt and PowerShell.
- Command Prompt: This is the traditional CLI for Windows. To open it, you can press the Windows key, type
cmd
orCommand Prompt
, and press Enter. - PowerShell: This is a more modern and powerful command line shell. It uses many of the same commands as Command Prompt for basic navigation but also has a more advanced scripting language. To open it, press the Windows key, type
PowerShell
, and press Enter. - Windows Terminal: Modern versions of Windows 11 often come with Windows Terminal, which can house multiple command line tabs, including Command Prompt, PowerShell, and even Linux shells if you have the Windows Subsystem for Linux (WSL) installed.
For the purpose of this beginner's guide, the commands for file navigation and manipulation are largely identical between Command Prompt and PowerShell.
macOS
On macOS, the command line interface is accessed through an application called Terminal.
- You can find it in your
Applications
folder, inside theUtilities
sub-folder. - The quickest way to open it is by using Spotlight search. Press Command + Spacebar, type
Terminal
, and press Enter. The macOS Terminal uses a shell called Zsh by default (previously Bash), which is very similar to what is found on Linux.
Linux
Linux is renowned for its powerful command line capabilities, and virtually every Linux distribution comes with a terminal application.
- The application is usually simply called Terminal. You can typically find it in your applications menu.
- A common keyboard shortcut to open the terminal on many distributions (like Ubuntu) is Ctrl + Alt + T.
- Like macOS, Linux distributions most commonly use the Bash or Zsh shell.
Understanding the Command Prompt
Once you open the terminal, you'll be greeted by the command prompt. This is the line of text where you will be typing your commands. It might look slightly different depending on your system, but it generally provides a few key pieces of information.
A typical prompt might look like this:
C:\Users\YourUsername>
on Windows.
YourMacBook:~ YourUsername$
on macOS.
YourUsername@YourComputer:~$
on Linux.
Let's break down what this means:
C:\Users\YourUsername
or~
: This part of the prompt shows your current working directory. This is the folder you are currently "in". The tilde (~
) is a special character on macOS and Linux that represents your personal home directory (e.g.,/Users/YourUsername
).YourUsername
: This is your current user account name.>
or$
: This symbol indicates the end of the prompt. It's the system's way of saying, "I'm ready for your command." You will type your commands immediately after this symbol.
Understanding where you are (your current directory) is the single most important concept for navigating your file system, which we will dive into next.
Navigating Your File System: Moving Through Directories
The first skill you need to learn on the command line is how to navigate. In a graphical interface, you click on folders to open them and see their contents. In the CLI, you use commands to move between these directories and view what's inside. The core concept here is the "working directory"—the directory you are currently inside. The commands you run will, by default, affect the contents of your current working directory.
Checking Your Current Location (pwd)
Before you start moving around, it's essential to know where you are. The command to do this tells you the full path to your current working directory.
macOS and Linux
On macOS and Linux, the command is pwd
, which stands for "print working directory".
pwd
Executing this command will print the absolute path of your current location, for example: /Users/YourUsername/Documents
.
Windows
On Windows, the concept is the same, but you can use the cd
command without any arguments, or the %cd%
variable.
cd
This will print the current directory path, such as C:\Users\YourUsername\Documents
. It's a subtle difference but a good one to know.
Listing Directory Contents (ls / dir)
Once you know where you are, you'll want to see what files and folders are in that directory.
macOS and Linux
The command to use is ls
, which stands for "list".
ls
This will display a simple list of all non-hidden files and directories in your current location. The ls
command is very powerful and has many options (called flags) to modify its output. For example:
ls -l
: This gives a "long" listing, which includes details like permissions, owner, size, and modification date.ls -a
: This shows "all" files, including hidden files (those whose names begin with a dot, like.bash_profile
).- You can combine flags:
ls -la
will show a long listing of all files.
Windows
The equivalent command on Windows is dir
, which stands for "directory".
dir
By default, dir
provides a detailed list, similar to ls -l
, showing file sizes, modification dates, and whether an item is a directory (<DIR>
) or a file.
Changing Directories (cd)
This is perhaps the most-used command in navigation. The cd
command, which stands for "change directory," allows you to move into other directories. The syntax is the same across all three platforms.
Moving into a Sub-directory
To move into a folder that is inside your current directory, you simply type cd
followed by the folder's name.
cd Documents
If the folder name contains spaces, you must wrap it in quotes:
cd "My Pictures"
Moving Back Up to the Parent Directory
To move up one level (to the folder that contains your current folder), you use two dots (..
) as the destination.
cd ..
This special symbol (..
) is a universal shortcut for the parent directory.
Navigating to a Specific Path
You can also navigate to any directory on your system by providing its full, or "absolute," path.
- On macOS/Linux:
cd /Users/YourUsername/Desktop
- On Windows:
cd C:\Users\YourUsername\Desktop
Navigating to Your Home Directory
A quick shortcut to return to your personal home directory from anywhere in the file system is to type cd
with no arguments on macOS and Linux.
cd
On Windows, cd %HOMEPATH%
will achieve a similar result. The tilde ~
character on macOS and Linux is also a shortcut for the home directory, so you can type cd ~
to go home, or cd ~/Documents
to go directly to your Documents folder.
File and Directory Manipulation: Creating, Deleting, and Moving
Once you are comfortable navigating through your folders, the next logical step is to learn command line techniques for manipulating the files and directories themselves. This includes creating new folders, making new files, renaming them, copying them to new locations, and deleting them. These commands are what allow you to manage your entire file system without ever touching your mouse.
Creating Directories (mkdir)
When you need to organize your files, you'll often need to create a new folder (or directory). The command for this is mkdir
, which stands for "make directory," and it works identically across Windows, macOS, and Linux.
mkdir NewProject
This command will create a new directory named NewProject
inside your current working directory. If you want to create a directory in a different location, you can provide the full path:
mkdir /Users/YourUsername/Documents/NewProject
If the directory name contains spaces, remember to use quotes:
mkdir "New Project"
Creating Empty Files (touch / echo)
Often, you'll want to quickly create an empty file, perhaps as a placeholder or a new script file. The method for this differs slightly between systems.
macOS and Linux
The standard command is touch
. It was originally designed to update a file's timestamp, but if the file doesn't exist, touch
creates it.
touch new_file.txt
You can create multiple files at once:
touch file1.txt file2.html file3.css
Windows
While touch
is not available by default in the classic Command Prompt, you can use a workaround with the echo
command. This command is typically used to display text, but you can redirect its output to a file.
echo. > new_file.txt
The echo.
command outputs a blank line, and the >
symbol redirects that output, creating new_file.txt
. In PowerShell, a more direct method is available:
New-Item new_file.txt -ItemType File
Copying Files and Directories (cp / copy)
Creating copies of files is a fundamental task.
macOS and Linux
The command is cp
, for "copy". The basic syntax is cp [source] [destination]
.
To copy a file within the current directory:
cp original.txt copy.txt
To copy a file to another directory:
cp report.docx /Users/YourUsername/Desktop/
To copy an entire directory and its contents, you need to use the -r
flag, which stands for "recursive".
cp -r MyProject /path/to/backup/location/
Windows
The command is copy
. The syntax is very similar: copy [source] [destination]
.
copy original.txt copy.txt
To copy a file to another directory:
copy report.docx C:\Users\YourUsername\Desktop\
For copying a directory and its contents, Windows has a more robust command called xcopy
.
xcopy MyProject C:\Backup\MyProject\ /E /I
The /E
flag ensures all subdirectories are copied (even empty ones), and /I
tells xcopy
to assume the destination is a directory if it doesn't exist.
Moving and Renaming Files (mv / move, ren)
Moving a file from one location to another is done with the "move" command. This command is also cleverly used for renaming files. The logic is that renaming a file is just "moving" it to a new name in the same location.
macOS and Linux
The command is mv
, for "move". The syntax is mv [source] [destination]
.
To move a file to a new directory:
mv important_document.pdf Documents/
To rename a file:
mv old_filename.txt new_filename.txt
This command moves old_filename.txt
to new_filename.txt
within the same directory, effectively renaming it. The same command works for moving and renaming directories.
Windows
Windows has separate commands for moving and renaming.
- To move a file, use
move
:move important_document.pdf Documents\
- To rename a file, use
ren
(for "rename"):ren old_filename.txt new_filename.txt
This is a key difference to remember between the platforms. You cannot use move
to rename a file if you are not also changing its directory path on Windows.
Deleting Files and Directories (rm / del, rmdir)
Finally, you will need to remove files and directories that are no longer needed. Be very careful with these commands, as items deleted via the command line do not go to the Recycle Bin or Trash. They are permanently deleted.
macOS and Linux
The command is rm
, for "remove".
To delete a single file:
rm file_to_delete.txt
To delete an empty directory, you can use rmdir
. However, rmdir
will fail if the directory contains any files.
rmdir empty_folder
To delete a directory and all of its contents (files and subdirectories), you must use rm
with the recursive flag -r
.
rm -r folder_to_delete
You can add the -f
(force) flag (rm -rf
) to suppress prompts, but this is an extremely dangerous command. Double-check your path before executing rm -rf
.
Windows
Windows uses del
to delete files and rmdir
(or rd
) to remove directories.
To delete a single file:
del file_to_delete.txt
To remove an empty directory:
rmdir empty_folder
To remove a directory and its contents, you need to use the /S
flag with rmdir
.
rmdir /S folder_to_delete
The system will ask you to confirm if you are sure (Are you sure (Y/N)?
), providing a safety net that rm -r
on Linux/macOS does not have by default.
Conclusion: Your Journey with the Command Line Has Just Begun
Mastering the command line is a journey, not a destination. What you have learned in this guide are the absolute essentials—the digital equivalent of learning to walk. By understanding how to access the terminal, navigate your file system with commands like cd
and ls
, and perform basic file manipulations with mkdir
, cp
, mv
, and rm
, you have built a solid foundation. These basic commands for file navigation and manipulation are the building blocks upon which all other command line skills are based. You can now move through your computer's directories, see their contents, and manage your files and folders with a speed and efficiency that a graphical user interface cannot always offer.
We encourage you to continue practicing these commands until they become second nature. Open the terminal instead of the file explorer for simple tasks. Create a practice directory and experiment with making, renaming, copying, and deleting files and folders. As you grow more comfortable, you can begin to explore more advanced topics like permissions, command chaining, and simple scripting. The command line is a gateway to a deeper understanding of how your computer works, and with the skills you've acquired today, you are well-equipped to continue your exploration and learn the command line at an even more advanced level.