Complete List: Command Line Prompt (CMD)
Complete List: Command Line Prompt (CMD)
Update: Cheat Sheets BETA is here!
- Learn the Command Line:Navigating the File System
- Learn the Command Line:Viewing and Changing the File System
What is the command line?
Thecommand lineis a text-based interface used to interact with our computer’soperating system. Instead of clicking through files and folders using a graphical interface like Windows Explorer or Finder on macOS, we can use typed commands to navigate, create, edit, and manage files efficiently.
The command line gives users more control and flexibility, especially for developers, system administrators, and power users. Once you learn the key commands, it becomes a powerful tool for managing your system quickly and effectively.
Below is a handy reference of commonly used command-line commands.
Redirection commands
Redirection is the process of sending the output or input of a command to a file or another command.
1.>
The>operator redirects a command’s output into a file, creating a new one or overwriting an existing one.
Syntax:
command > filename
Example:
echo "Hello, World!" > hello.txt
Copy to clipboard
echo "Hello, World!" > hello.txt
This command takes the output of echo"Hello, World!"and writes it to a file calledhello.txt. Ifhello.txtalready exists, it will be overwritten with the new content.
2.>>
The>>operator appends the output of a command to an existing file.
command >> file
echo "This is appended text." >> hello.txt
Copy to clipboard
echo "This is appended text." >> hello.txt
This command appends “This is appended text.” to the filehello.txt. If the file doesn’t exist, it will be created.
3.<
The<operator redirects input from a file into a command.
command < file
sort < unsorted.txt
Copy to clipboard
sort < unsorted.txt
This command takes the contents ofunsorted.txtand sorts them using thesortcommand, displaying the output in the terminal.
4.|
The pipe|sends the output of one command as input to another command.
command1 | command2
cat file.txt | grep "pattern"
Copy to clipboard
cat file.txt | grep "pattern"
This command takes the output ofcat file.txtand sends it togrepto search for the word “pattern” in its content.
Navigation commands
Navigation commands help move around the file system and display information about directories and files.
1.cd(Change directory)
Thecdcommand is used to change the current working directory.
cd directory
cd /home/user/Documents
Copy to clipboard
cd /home/user/Documents
This command changes the current working directory to/home/user/Documents.
2.cd ..(Go up one directory)
Thecd ..command is used to move one directory level up.
cd ..
3.pwd(Print working directory)
Thepwdcommand prints the current working directory.
pwd
4.ls(List directory contents)
Thelscommand lists the files and directories in the current directory.
Syntax:
ls
Copy to clipboard
ls
This basic command can be combined with various options to display additional details or modify the output format. Here’s a table showcasing the options:
File handling
File handling commands are used to copy, move, remove, create, and manage files and directories.
1.cp(Copy files)
Thecpcommand copies files or directories from one location to another.
cp source destination
cp file1.txt /home/user/Documents/
Copy to clipboard
cp file1.txt /home/user/Documents/
This command copiesfile1.txtto the/home/user/Documents/directory.
2.mv(Move files)
Themvcommand moves or renames files and directories.
mv source destination
mv file1.txt /home/user/Documents/new_file.txt
Copy to clipboard
mv file1.txt /home/user/Documents/new_file.txt
This command movesfile1.txtto the/home/user/Documents/directory and renames it tonew_file.txt.
3.rm(Remove files)
Thermcommand is used to delete files.
rm file
rm file1.txt
Copy to clipboard
rm file1.txt
This command removes the filefile1.txtfrom the current directory.
4.rm -r(Remove directories)
Therm -rcommand deletes a directory and all its contents recursively.
rm -r directory
rm -r /home/user/Documents/old_folder
Copy to clipboard
rm -r /home/user/Documents/old_folder
This command deletes the directoryold_folderand all its contents under/home/user/Documents/.
Note:Be careful withrmandrm -r. Files deleted using these commands cannot be recovered easily.
5.touch(Create empty file)
Thetouchcommand is used to create an empty file or update the timestamp of an existing file.
touch file
touch newfile.txt
Copy to clipboard
touch newfile.txt
6.mkdir(Make directory)
Themkdircommand creates a new directory.
mkdir directory
mkdir new_folder
Copy to clipboard
mkdir new_folder
This command creates a new directory namednew_folderin the current directory.
Search commands
Search commands allow users to search for specific text patterns within files or directories.
1.grep(Search for pattern)
Thegrepcommand searches for a specified pattern in a file.
grep pattern file
grep "error" log.txt
Copy to clipboard
grep "error" log.txt
This command searches for the word “error” in thelog.txtfile and prints lines containing the word.
2.grep -i(Case-insensitive search)
Thegrep -icommand performs a case-insensitive search.
grep -i pattern file
grep -i "error" log.txt
Copy to clipboard
grep -i "error" log.txt
This command searches for “error” inlog.txtregardless of case (i.e., it will match “ERROR”, “Error”, etc.).
3.grep –R(Recursive search)
Thegrep -Rcommand searches for a pattern in files recursively within directories. This is useful for searching across logs, codebases, or config directories.
grep -R pattern directory
grep -R "error" /var/log
Copy to clipboard
grep -R "error" /var/log
This command searches for the word “error” in all files within the/var/logdirectory and its subdirectories.
4.grep –Rl(Recursive search with file names only)
Thegrep –Rlcommand performs a recursive search and lists only the names of files that contain the pattern.
grep -Rl pattern directory
grep -Rl "error" /var/log
Copy to clipboard
grep -Rl "error" /var/log
This command shows only the file names in/var/logwhere “error” is found.
Environment variables
Environment variables store configuration data used by processes.
1.env(Show environment variables)
Theenvcommand displays the current environment variables.
env
2.env | grep VARIABLE(Filter environment variables)
Theenv | grep VARIABLEcommand filters the environment variables to show only those that match a specific pattern.
env | grep VARIABLE
env | grep PATH
Copy to clipboard
env | grep PATH
This command filters and displays only the variables related toPATH.
3.export(Set environment variables)
Theexportcommand sets environment variables that other processes can access.
export VARIABLE=value
export PATH=$PATH:/new/path
Copy to clipboard
export PATH=$PATH:/new/path
This command appends/new/pathto thePATHvariable, making it available to other processes.
4.PATH(System path variable)
ThePATHenvironment variable contains directories where executable files are stored.
echo $PATH
echo $PATH
Copy to clipboard
echo $PATH
This command displays the currentPATHvariable, showing where the system looks for executable files.
5.HOME(Home directory)
TheHOMEenvironment variable represents the user’s home directory.
echo $HOME
Editors and environment configuration
Editing and configuring the environment are done through text editors and configuration files.
1.nano(Text editor)
nanois a basic terminal-based text editor.
nano file
nano .bash_profile
Copy to clipboard
nano .bash_profile
This command opens the.bash_profilefile in the nano editor for editing.
2..bash_profile(Bash configuration file)
The.bash_profilefile contains configuration settings for the Bash shell.
nano ~/.bash_profile
This command opens the.bash_profilein the nano editor to configure shell settings.
3.alias(Create aliases)
Thealiascommand creates shortcuts for commands.
alias name='command'
alias ll='ls -l'
Copy to clipboard
alias ll='ls -l'
This command creates an aliasllfor thels –lcommand, so it can be run easily.
4.source(Apply configuration)
Thesourcecommand is used to apply the changes made to configuration files.
source file
source ~/.bash_profile
Copy to clipboard
source ~/.bash_profile
This command reloads the.bash_profilefile, applying any changes made to it.
Utilities
These commands perform text processing operations.
1.uniq(Remove duplicate lines)
Theuniqcommand filters out duplicate lines in a file.
uniq file
uniq file.txt
Copy to clipboard
uniq file.txt
This command removes duplicate lines fromfile.txtand displays the unique lines.
2.sort(Sort lines)
Thesortcommand arranges lines of text in alphabetical or numerical order.
sort file
sort file.txt
Copy to clipboard
sort file.txt
This command sorts the lines offile.txtin alphabetical order.
3.sed(Stream editor)
Thesedcommand is used for manipulating and transforming text.
sed 's/old/new/' file
sed 's/hello/world/' file.txt
Copy to clipboard
sed 's/hello/world/' file.txt
This command replaces the first occurrence of “hello” with “world” infile.txt.
Concepts
Standard input, output, and error streams are fundamental concepts in Unix-like systems.
- stdin(Standard Input):stdinis the default input stream from which data is read.
- stdout(Standard Output):stdoutis the default output stream to which data is written.
- stderr(Standard Error):stderris the default error stream for error messages.
Wildcards
Wildcards are used to match patterns in filenames.
*(Asterisk)
The*wildcard matches any number of characters in a file or directory name.
command *
ls *.txt
Copy to clipboard
ls *.txt
This command lists all files with a.txtextension in the current directory.
?(Question mark)
The?wildcard matches exactly one character in a file or directory name.
command ?.txt
ls ?.txt
Copy to clipboard
ls ?.txt
This command lists files likea.txt,b.txt, or1.txt, but notab.txtorabc.txt, because?matches only one character.
[](Square brackets)
The[]wildcard matches any one character inside the brackets.
command [abc]*.txt
ls [abc]*.txt
Copy to clipboard
ls [abc]*.txt
This command lists files starting witha,b, orcand ending in.txt, such asapple.txt,bat.txt, orcat.txt.
Conclusion
In this article, we explored some of the essential commands and concepts in CMD, ranging from basic file manipulation to more advanced scripting techniques. Understanding these commands will help you efficiently navigate and manage the Windows command-line environment. You can enhance your workflow, troubleshoot issues, and even automate tasks with practice.
If you’re eager to dive deeper into CMD, scripting, and automation, check out the Codecademy course on Learn the Command Line.
Frequently asked questions
1. How do I get a list of commands in CMD?
You can view a list of built-in CMD commands by typing:
help
Copy to clipboard
help
This displays a summary of available commands. For detailed info about a specific command, type:
help <command>
Copy to clipboard
help <command>
Or
<command> /?
Copy to clipboard
<command> /?
2. How many commands are in CMD?
Depending on your Windows version, there are dozens of built-in commands in CMD, typically over 100. These include basic file operations, system tools, networking utilities, and scripting support.
You can view them using:
help
Copy to clipboard
3. What does==mean in CMD?
In CMD scripting (batch files),==is a comparison operator used in if statements to check if two values are equal.
Example:
if "%name%" == "Alexa" echo Hello, Alexa!
Copy to clipboard
if "%name%" == "Alexa" echo Hello, Alexa!
This checks if the variable%name%equals"Alexa"and prints a message if true.
4. What is the Z in CMD?
TheZ:drive letter often appears in CMD when:
- A network drive is mapped toZ:
- A virtual drive (e.g., boot or install media) is assigned the letterZ:
- Some custom scripts or corporate environments map tools or backup folders toZ:
It’s not special to CMD itself — just a drive label, likeC:orD:.
5. What is%%in CMD?
In CMD scripts (batch files),%%represents loop variables inside for loops.
Example (inside a batch file):
for %%i in (*.txt) do echo %%i
Copy to clipboard
for %%i in (*.txt) do echo %%i
This loops through all.txtfiles and echoes their names.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Complete Payment to Unlock Full Guide & Source Code
You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.
Ready to access the complete step-by-step tutorial?
Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.
Related Blueprints & Guides
View all 166 →Android Studio Quick-Start Guide
Android Studio often intimidates beginners, but by learning a few handy shortcuts and getting the lay of the land, we can navigate this powerful development environment with ease.
Developer toolsApache Kafka Tutorial: A Complete Guide for Beginners
Apache Kafkais a well-known distributed event streaming platform designed to efficiently manage real-time data feeds. It was originally developed by LinkedIn in 2011 and later open-sourced. Apache Kafka has gained immens...
Developer toolsBuilding Websites with Wix
Creating a website has never been easier. Previously, making a presentable website meant learning HTML, CSS, andJavaScript. The whole process could take weeks, months, or even years before creating a website that you’re...
Ready to build production-grade AI systems?
Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.