What the command line is
The command line is a text-based way of telling a computer what to do. Instead of clicking icons and menus, you type an instruction and press Enter. A program called a shell reads what you typed, works out what it means and asks the operating system to carry it out. The window you type into is often called a terminal or console.
It can look unfriendly at first: a blinking cursor and very little guidance. But the idea is simple. You are having a short, precise conversation with the computer, and it only does what you ask.
Why people use it
Graphical interfaces are good for exploring and for tasks you do occasionally. The command line is often preferred for several reasons.
- Repeatability. A command can be typed again, copied into a note or saved in a file. Clicking through ten menus is harder to reproduce exactly.
- Speed for bulk work. Renaming or finding hundreds of files can be one line instead of hundreds of clicks.
- Remote work. Servers often have no screen or desktop. Administrators connect over a network and type commands. This is common with the machines described in server basics, which often run without a desktop at all.
- Automation. Commands can be combined into scripts, which is the starting point of shell scripting.
- Precision and detail. Many tools show more information, or offer more options, in text form.
None of this makes graphical tools worse. Most people use both, depending on the task.
The anatomy of a command
A typical command has up to three parts, separated by spaces:
- The command name, which says what to do.
- Options (sometimes called flags), which change how it does it. They usually start with a dash.
- Arguments, which say what to act on, such as a file or folder name.
For example, a command to list the contents of a folder might look like ls -l Documents. Here ls is the command, -l asks for a longer, more detailed listing, and Documents is the folder. The exact command names differ between operating system families, and some systems have more than one shell with its own commands, but the structure is much the same.
Some simple examples
The following examples use names common on Linux and macOS shells. Windows has equivalents, and some of the commands are even accepted in similar forms. Check the documentation for your own system before relying on any of them.
pwdprints the folder you are currently in (the working directory).lslists the files and folders there.cd Documentsmoves into a folder called Documents.cd ..moves up one level.mkdir notescreates a new folder called notes.cp report.txt backup.txtcopies a file.mv backup.txt archive.txtmoves or renames a file.cat report.txtdisplays the contents of a text file.man lsshows the built-in manual page for a command, on systems that include them.
Almost every command also offers some form of help, often through an option like --help. Reading it is a good habit when you meet an unfamiliar command.
Paths: telling the computer where things are
Most commands need to know which file or folder you mean. A path describes the location. An absolute path starts from the top of the file system and gives the whole route. A relative path starts from where you are now. If you are in a folder that contains Documents, then Documents/report.txt is a relative path to a file inside it.
Understanding paths comes more easily once you have a mental picture of the way file systems organize data into a tree of folders. Names containing spaces usually need to be put in quotation marks so the shell does not read them as two separate arguments.
Putting commands together
One strength of the command line is that small tools can be connected. A pipe, written |, sends the output of one command into another. For instance, you might list files and then pass that list to a tool that counts lines or searches for a word. Many experienced administrators do much of their troubleshooting this way, for example by searching system logs for a particular message.
Staying safe
Typed commands are powerful and usually do not ask whether you are sure. A few habits reduce risk.
- Read before you run. Do not paste a command from the internet unless you understand what it does.
- Be careful with deletion. Commands that remove files often skip any recycle bin, so deleted data may be hard or impossible to recover.
- Be careful with elevated rights. Running a command as an administrator lets it change anything on the system. Using only the access that a task needs is a sound habit.
- Try things in a safe place. A spare folder, or a practice machine such as a home lab, is a good place to experiment.
- Keep backups. If a mistake does happen, good backups limit the damage.
Getting comfortable
You do not need to memorize hundreds of commands. Most people learn a dozen well, then look up the rest as needed. Tab completion, available in many shells, finishes file names as you type, and the up-arrow key brings back earlier commands, which saves effort and avoids typing mistakes.
A sensible way to start is to open a terminal and spend ten minutes just looking around: find out where you are, list what is there, move into a folder and back out again. Nothing in those steps changes your files. After that, small tasks, such as copying a file or creating a folder, build confidence. The goal is not to replace the mouse, only to add a tool that is sometimes much faster and often much easier to repeat.