DEV Community

Cover image for Commanding the Command Line
Phillip A. Wessels
Phillip A. Wessels

Posted on • Originally published at pawper.dev

Commanding the Command Line

You may experience some emotions of trepidation when staring at the empty prompt in the terminal.

$
Enter fullscreen mode Exit fullscreen mode

That is very normal for beginners. But I want you to rewrite the story you're telling yourself from:

❌ I am going to mess something up with this.

to:

🚀 I am going to take off with this.

The command line does have deep functionality -- but it also has basics, and they're not complicated. By the time you finish this article, you'll understand those basics, and you can start to find your flow in the command line. Now let's get into:

  • Bash (your shell if you're on Windows or Linux)
  • Zsh (your shell if you're on macOS)

Either way, in the VS Code integrated terminal!

What does the command line do?

The command line allows you to interact with the operating system through the shell in your terminal.

Why use the command line interface (CLI) instead of a graphical user interface (GUI)?

  • It gives more control. There are things you can only do via the command line.
  • Because typing is fast and you can also automate tasks, the command line is far quicker; the small gains in speed per task cumulate into a significant amount of efficiency.
  • Compatibility; the command line will run on any machine, though Windows requires additional setup if you want to use the Unix command line. Cloud computing often requires the use of the command line. And, some AI tools are only available as CLIs.

What am I looking at?

The first thing you see is the prompt. The default composition of the prompt is as follows:

currentuser@hostname:workingdirectoryUID
Enter fullscreen mode Exit fullscreen mode

Your prompt should look something like this if on WSL:

your-name@machine-name:/mnt/c/Users/your-name$
Enter fullscreen mode Exit fullscreen mode

And not much different on macOS:

your-name@machine-name ~ %
Enter fullscreen mode Exit fullscreen mode

What's $/%/# mean in the prompt? What's sudo?

Before we go any further we have to talk about privileges.

The prompt has a UID indicator to remind you if your privileges are standard ($ - Bash; % - Zsh) or root (#). Root is not recommended. Anyone accessing your workstation (digitally or physically) could take advantage of or sabotage your system. If you see # in the prompt, you should take it as a red flag.

With standard privileges ($/%), you can temporarily gain administrative privileges by using the sudo command. sudo ("soo doo", short for "substitute user do") will prompt you for your user password, then for 5 minutes the Linux environment will run any command-line with the root privileges (in other words, all privileges). If you need to use sudo for a command, you will receive a message stating something like "Permission denied."

Remember to store passwords securely.
http://pawper.dev/l/guide-password-management-cybersecurity-beginners

What's ~? Where am I?

Your prompt shows the absoltue path from the root directory.

Think of a path as instructions for how to navigate the file system. Every / (Bash/Zsh's path separator) is a going from a directory into one of its contents. If you've spent time on Windows, you may be used to \ as the path separator, but you probably recognize / as a path separator from URLs on the Internet.

A path is an absolute path if it gives directions all the way from the root (the other kind of path is a relative path, but we'll return to that in a bit).

If you're in what's called the home directory for your user profile, you'll see ~. In Bash/Zsh, home is represented by the metacharacter ~, and if you use it in the CLI, the shell just sees it as the absolute path of your home directory. So you can always return home by entering cd ~! No ruby slippers required.

If you're using WSL and in the mounted Windows filestystem, you'll see something like /mnt/c/Users/your-name in the prompt. You should use cd ~ to change to your home directory in Linux. Stay on the Linux side of the fence!

Anatomy of the Prompt (Generated by Claude Design)

What do I type here?

You can submit a command-line from the prompt. The general syntax is:

command -options arguments
Enter fullscreen mode Exit fullscreen mode

Letter options follow a single dash and can be combined. For example, -lh. Word options follow two dashes, such as --help. Try the following commands as we go through them.

Some commands are just a command/utility name, such as clear.

clear
Enter fullscreen mode Exit fullscreen mode

Others take arguments:

echo hello
Enter fullscreen mode Exit fullscreen mode

Others accept letter options. Letter options are prefixed with a dash (-)

ncal -3hMj
Enter fullscreen mode Exit fullscreen mode

Some options might be longform word options (--) and can't be combined in the above way.

df --human-readable --total --type=ext4
Enter fullscreen mode Exit fullscreen mode

If you want to learn what options and arguments a command takes, use the --help option:

clear --help
Enter fullscreen mode Exit fullscreen mode

Anatomy of a Command Line (Generated by Claude Design)

Help, --help isn't working!

Enter:

history --help
Enter fullscreen mode Exit fullscreen mode

You'll see it doesn't work.

If the option --help doesn’t work, it’s a built-in. Built-ins like history are built into the shell (Bash/Zsh) and aren’t a standalone application executed from a file. They are processed faster and have a slightly different way of getting help & usage. Try passing them to other commands, like:

help history
info history
man history
Enter fullscreen mode Exit fullscreen mode

You can determine if a command is a builtin or external program using the type command:

type help
type history
type type
Enter fullscreen mode Exit fullscreen mode

How do I navigate the filesystem?

When typing in the prompt, you can use absolute paths, but you'll usually use relative paths. Relative paths begin at the current directory, represented by ..

You can type .. in the path to indicate a parent directory. So, cd ../../.. goes up 3 times in the filesystem.

Note that Bash/Zsh are case-sensitive, and you'll need quotes around any paths with spaces in them.

Understanding the Root Directory

You can start using cd .. now to change directory up, but you should know -- The root directory (represented as /) is the top-level of your entire filesystem—the foundation from which all other directories branch out. It contains system files, configuration, and installed software that makes your operating system run. You'll rarely need to navigate here for your own work, but understanding what's there helps you navigate confidently and avoid dangerous mistakes.

⚠️ Caution: Never delete or modify files in the root directory or its subdirectories unless you know exactly what you're doing. One wrong command with rm can break your entire system. Always stay in your home directory (~) for your work.

The Filesystem & Paths (Generated by Claude Design)

Commands for Navigating & Working with the Filesystem

Command Description
pwd Print the working directory (where you are)
mkdir -p path Create directories in the path(s) within the working directory
cd path Change to the home directory (or specified path)
ls path List the content of the working directory (or path)
file file Print what kind of file it is
mv source destination Move source to destination; also used to rename files (e.g., mv old.txt new.txt)
ln -s path link Create a symbolic link (nickname) for a file or directory
cp file1 file2 Create a copy of file1 named file2
rm file Remove the file; use -r to remove directories recursively. ⚠️ No undo!
locate x Search for x in the shell’s filesystem database
updatedb Update the database that locate uses
pushd / popd Save your location (pushd); return to it (popd)
code . Open the current directory in VS Code

There is no undo. If you move, overwrite or delete a file or directory, it is a permanent change.

How do I make a new file?

Command Use When
touch filename.txt You need a quick empty file
echo "content" > filename.txt You want to create a file with text in one command
cat > filename.txt then type content, Ctrl+D to save, Ctrl+C to cancel You want to type content interactively
code newfile.txt You want to create and edit in VS Code

Naming files

Capitalization matters and you should only use alphanumeric characters, hyphens and underscores. Other special characters cause issues, as they have special meaning to the operating system. Also avoid spaces because they are problematic; they do work if you put the filename in quotes ('my website').

Extensions

A file can have a file type that doesn't match its extension and files technically work without an extension (however this is not recommended). Use the file command to determine the actual file type.

How do I open / edit a file?

Command Purpose
code filename.txt Edit in VS Code — Best use this.
cat filename.txt View file (read-only, printed to your terminal)
less filename.txt Page through file (read-only) — View a file page-by-page.
Space to scroll, B to go back, Q to quit.

If you accidentally end up in a terminal text editor like nano or vim:

  • Nano: Press Ctrl+X to exit (it will ask to save)
  • Vim: Press Esc, then type :q! (colon, q, exclamation) and press Enter to quit without saving

Helpful Notes

  • Bash is case-sensitive. Most of the time the command-line will be lowercase.
  • Use / to cycle through previous submissions.
  • You can enter a list of commands separated by ; or &&, all in one command line.
  • Enter !! (“bang bang”) to repeat the previous command line.
  • Right click with your mouse to paste.

Command Completion

If you start typing a command, directory or file name, you can use command completion (a.k.a. auto completion) with the Tab key to have the shell to guess where you are trying to get to. If it isn’t working because there are multiple possibilities, press Tab twice to see a list of conflicting files:

$ cd DoTab doesn’t autocomplete.

$ cd DoTabTab prints the conflicts:

Documents/ Downloads/

$ cd DocTab autocompletes to:

$ cd Documents/

WSL: converting Windows paths

wslpath is a CLI tool to convert Windows paths to Linux paths and vice-versa.

WSL users: Should you ever need to, you can open the current directory in Windows Explorer by entering explorer.exe .

Beyond the Basics (Optional)

Once you're comfortable with the fundamentals above, you can explore:

  • Pipes (|) — Chain commands together: cat file.txt | grep “search term” sends the output of one command into another
  • Redirection (>, >>) — Save output to files: ls > files.txt writes directory listing to a file; >> appends
  • grep — Search text: grep “pattern” filename finds lines matching a pattern
  • find — Advanced searching: find . -name “*.js” finds all JavaScript files recursively
  • sed / awk — Text processing and manipulation for advanced workflows

These tools unlock the real power of the command line, but they're not essential for getting started. Master the basics first, then explore these as you need them.

Permissions

You can see files’ permissions using the ls command with the long format argument (-l). They are represented within a string of characters and dashes at the beginning of a file’s line.

drwxr-xr-x 2 user user 4096 Jan  9 10:11 documents
-rw-r--r-- 1 user user  675 Jan  7 12:05 .profile
Enter fullscreen mode Exit fullscreen mode
  • The first character indicates if the file is a directory or file. A d indicates it is a directory. A - indicates it is a file and is not a directory. The next 9 characters are the permissions.
  • Every file or directory belongs to a user owner and a group owner. Then there are other people. Files can be readable (r), writeable (w) and/or executable (x) for each, in that order. If you see a - where one of these characters should be, that means the owner, group or public does not have that permission.
  • chmod changes the mode/permissions of a file. You can optionally specify who for by using the corresponding letter for the owning user (u), owning group (g), and others (o). Use + to add a permission or - to remove it. Then follow that with the applicable permission(s): r, w and/or x. E.g., chmod +x file (adds executable permission for all) or chmod go-w file (removes write permission for the owning group and others).

File Permissions (Generated by Claude Design)

Customize Your Prompt

When you open Bash/Zsh in VS Code, your prompt might look like:

user@computer:/path/to/project$
Enter fullscreen mode Exit fullscreen mode

You can customize this by editing your .bashrc/.zshrc file (in your home directory):

code ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Add this near the bottom to create a simpler, colorful prompt:

export PS1="\[\033[1;36m\]\[\033[0m\] "
Enter fullscreen mode Exit fullscreen mode

Or a prompt showing the current directory:

export PS1="\[\033[1;32m\]\w\[\033[0m\] \[\033[1;36m\]\[\033[0m\] "
Enter fullscreen mode Exit fullscreen mode

Save with Ctrl+s.

Reload your Bash configuration:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Prompt Key

Use these variables to build your custom prompt:

Variable What It Shows Recommendation
\u Current username ✓ Keep it
\h Hostname (computer name) ✓ Keep it (useful for Docker)
\w Full path of current directory Consider switching to \W
\W Base name of current directory only ✓ Recommended (cleaner display)
\@ Current time (12-hour am/pm format) Optional
\$ Shows # if root user, otherwise $ ✓ Keep it (security indicator)

Color Codes

To customize colors, find where you want the color to change and insert: \[\033[X;Ym\] where X;Y is the color code below.

Example: \[\033[1;36m\]→\[\033[0m\] makes the arrow bright cyan.

Color Code Color Code
Black 0;30 Dark Gray 1;30
Red 0;31 Light Red 1;31
Green 0;32 Light Green 1;32
Brown 0;33 Yellow 1;33
Blue 0;34 Light Blue 1;34
Purple 0;35 Light Purple 1;35
Cyan 0;36 Light Cyan 1;36
Light Gray 0;37 White 1;37

Note: Not all terminals support all colors. If using a terminal outside of VS Code, test and adjust if needed.

Troubleshooting

How to Escape Terminal Apps

If you're stuck in a terminal application (like a text editor, pager, or hung process), here are the universal ways to escape:

Keyboard Shortcut What It Does
Ctrl+C Interrupt/Cancel — Stops the current process. Works in most apps.
Ctrl+D Exit — Signals end-of-file. Closes some apps like cat in input mode.
Q Quit — Works in pagers like less and man. Just press Q.
Ctrl+Z Suspend — Pauses the app and returns to prompt (type fg to resume, bg to run in background).
Esc then :q! Vim exit — Press Esc, type :q!, press Enter to force-quit without saving.
Ctrl+X Nano exit — Exits to prompt (asks if you want to save).

If nothing works: Try Ctrl+C first. If that hangs, try Ctrl+Z. In VS Code's terminal, you can also close the terminal tab directly.

Permissions Denied When Running Scripts

If you ever get a bash script, and you can't run it, use chmod +x:

chmod +x ./script.sh
./script.sh
Enter fullscreen mode Exit fullscreen mode

The chmod +x command makes the script executable.


You are now in command of the command line. You can navigate the filesystem, manipulate files, run programs, and understand what's happening under the hood. That empty prompt that felt intimidating at the beginning? You know what to do with it now.

Sources / additional material:

https://code.visualstudio.com/docs/editor/command-line

This article was revised & expanded with AI for the purpose of providing practical information. I have reviewed it for accuracy and edited it appropriately.

Top comments (0)