
Master the Command Line in One Guide
You’ve probably seen the terminal before—the black screen with white text. It looks confusing, right? All those details make it seem hard to learn. But don’t worry! In today’s blog, we’re going to cover everything you need to know to become a CLI pro. By the end, you’ll be using your PC like a champ and unlocking its full potential!
Using the CLI gives you:
- Greater Control: Direct backend access to the system.
- Speed and Efficiency: Execute tasks faster than navigating through GUI menus.
- Remote Server Access: Manage servers from anywhere.
- CLI Tools: Powerful utilities for automation and scripting.
- Career Benefits: Command line proficiency is a highly valued skill in many technical jobs.
Terminal, Command Line, and Shell
- Terminal: A tool (historically hardware, now software “terminal emulators”) used to type and execute commands.
- Command Line: The interface inside the terminal where you type commands. (On Windows, the terminal program is often called “Command Line.”)
- Shell: The program that runs inside the terminal (for example, Bash on Linux/Mac, or PowerShell on Windows). Think of it as the “operating system” of the terminal.
Note for Windows Users: Many native commands differ on Windows. It’s often easier to use Git Bash or the Windows Subsystem for Linux (WSL) for a more Unix-like experience.
Common Shortcut Keys
- Up/Down Arrow: Cycle through previous/next commands.
- Tab: Auto-complete commands or file names.
- Ctrl + L: Clear the screen for a clean view.
- Ctrl + C: Cancel or end the execution of a command.
- Ctrl + R: Search through command history (find a command similar to what you typed).
- Ctrl + D: Exit the terminal session.
Getting Help: Manual & --help Commands
- Manual (man):
- On Linux/Mac, use
man [command]
(e.g.,
) to display the manual page.man ls
- On Linux/Mac, use
- --help:
- On Windows (or when
man
is not available), append--help
to get usage details (e.g.,
).ls --help
- On Windows (or when
Basic System Commands
- whoami: Displays the current logged-in username.
whoami
- date: Shows the current date and time.
date
- clear: Clears the terminal screen.
clear
File System Navigation
Understanding your file system is key:
-
pwd: Print the current working directory.
Note: A tilde (
~
) indicates your home directory.pwd
-
ls: List the contents of a directory.
- Basic listing:
ls
- To list another (child) directory:
ls DIRECTORY-NAME
- Common Flags:
-a
: Show hidden files (those beginning with a dot).-l
: List in long format (includes permissions, size, date, etc.).-r
: Reverse the order of the listing.- Combined examples (order doesn’t matter):
ls -al ls -a -l ls -ral
- Basic listing:
-
cd: Change directory.
- To go to your home directory:
cd
- To move into a specific directory:
cd [dirname]
- To return to the previous directory:
cd -
- To move to the parent directory:
cd ..
- To go to your home directory:
-
Opening Folders/Files in GUI:
- Windows:
explorer . start [dirname or URL]
- Mac:
open [dirname or URL]
- Linux:
xdg-open [dirname or URL]
- Windows:
File and Directory Management
Creation Commands
- mkdir: Create a new directory.
mkdir new_folder
- touch: Create one or multiple files.
- Single file:
touch filename.txt
- Multiple files example (creates 100 files):
touch file-{001..100}.txt
- Single file:
Copying, Moving, and Renaming
- cp: Copy files.
cp file1.txt destinationfolderpath/file1.txt
- mv: Move or rename files and directories.
- Move:
mv file.txt destination_folder/
- Rename:
mv oldname.txt newname.txt
- For verbose output (showing source and destination):
mv file.txt newname.txt -v
- Move:
Deletion Commands
- rm: Remove files or directories.
-
Remove a file:
rm file.txt
-
Flags with rm:
-i
: Prompt before deleting each file or directory.-r
: Recursively remove directories.-rf
: Force deletion (use with caution, especially on non-empty directories).
-
Example to remove all files matching a pattern:
rm -f file-*
-
Editing and Viewing File Contents
Viewing and Editing Files
-
cat: Concatenate files or display file contents.
- View a file:
cat file.txt
- Concatenate multiple files:
cat file1.txt file2.txt
- Create (or overwrite) a file using input redirection:
(Press Ctrl + D to save and exit.)cat > file.txt
- Append to a file:
cat >> file.txt
- Display with line numbers:
cat -n file.txt
- View a file:
-
less: View file content with scrollable output.
less file.txt
- Exit by pressing
q
.
- Exit by pressing
-
nano: A terminal-based text editor.
nano file.txt
- Exit with
Ctrl + X
, then pressY
to save orN
to cancel.
- Exit with
-
echo: Display text or write text to files.
- Print text to the terminal:
echo "Hello World"
- Create (or overwrite) a file:
echo "Hello World" > file.txt
- Append text to a file:
echo "Hello Again" >> file.txt
- Print text to the terminal:
Viewing Portions of Files
- head: Display the first 10 lines of a file by default.
head file.txt
- To show a specific number of lines:
head -n 5 file.txt
- To show a specific number of lines:
- tail: Display the last 10 lines of a file by default.
tail file.txt
- To show a specific number of lines:
tail -n 5 file.txt
- To show a specific number of lines:
Searching Within Files
-
grep: Search for patterns or text in a file.
grep "searchterm" file.txt
- You can search multiple files at once:
grep "searchterm" file1.txt file2.txt
- You can search multiple files at once:
-
find: Locate files or directories based on conditions.
- Basic usage (find a specific file in the current directory):
find . -name "file-002.txt"
- Find empty files:
find . -empty
- Save output to a file:
find . -name "file*" > outputfile.txt
- Example Workflow:
- Create multiple files:
touch file-{001..100}.txt
- Append text to a file:
echo "Hello World" >> file-002.txt
- Check for empty files again:
find . -empty
- Create multiple files:
- Basic usage (find a specific file in the current directory):
Piping and Redirection
Piping allows you to send the output of one command as input to another.
-
Piping Example:
- Create a set of files:
touch file-{001..010}.txt
- Pipe the results of
find
into a file:find . -name "file-0*" > output.txt
- View the output:
cat output.txt
- Create a set of files:
-
Right-Angle Bracket (>):
Directs output to a file. For example, creating a file:> newfile.txt
(Press Ctrl + D when done if inputting text directly.)
Creating Symlinks
Symlinks (symbolic links) are like shortcuts or pointers to another file or folder.
- On Linux/Mac:
ln -s target_folder symlink_name
- On Windows:
- Using Git Bash or WSL, you can use the above command.
- Alternatively, use the Windows command:
mklink symlink_name target_folder
File Compression with tar
The tar
command creates and manipulates tarball archives.
- Create a tarball (with gzip compression):
tar czvf archive_name.tar.gz directory_name
- List contents of a tarball:
tar tzvf archive_name.tar.gz
- Extract a tarball:
tar xzvf archive_name.tar.gz
Common tar Flags:
-c
: Create an archive.-x
: Extract an archive.-f
: Specify the filename of the archive.-t
: List the contents of the archive.-v
: Verbose output.-z
: Compress/decompress with gzip.- Other flags include
-j
for bzip2,-W
for verification, etc.
Command History
- history: Displays the list of commands you have executed.
history
- Re-run a command:
Use!
followed by the command number to run it again:!100
Final Notes
-
Cross-Platform Considerations:
- Many commands behave similarly on Linux and Mac.
- Windows users should note that some commands may not work natively—using Git Bash or WSL can provide a more consistent Unix-like experience.
-
Combining Commands:
Use the&&
operator to execute multiple commands sequentially. For example:cd test2 && mkdir test3
Conclusion
The CLI can be tricky to remember at first, but it’s fast and reliable. It’s an important tool if you want to get ahead in your backend journey. Keep practicing, and you’ll get the hang of it!
Thanks for reading ~Jai Hanuman
3 Reactions
0 Bookmarks