The command line is a text-based interface that lets you enter commands to perform various tasks. Think of it like a mini-programming language that you can use to control your computer. This guide will show you just enough about the command line interface (CLI) to be dangerous.
* The command line interface may differ slightly for various operating systems, but for the most part everything covered in this article should work on all of them.
Why do I need the command line?
There are many reasons you might want to learn and use the command line interface, but it's especially important if one of these describes you:
You're a programmer who needs to work with code or other text-based files on a regular basis
You want to do something that can't be done with a GUI, or would be very time consuming
You're a sysadmin who needs to automate tasks or manage hundreds of servers from the command line
You're a power user who wants to be able to customize your system to do exactly what you want it to do
You need to learn how to use the command line in order to pass a class or get a job.
You want to be an ethical hacker and you love the challenge of getting a computer to do what you want it to do by typing in arcane commands
You're a masochist who enjoys typing in commands instead of using a mouse
Of course, this isn't an exhaustive list and if nothing else the CLI can actually be pretty fun to use and customize. Plus, everyone knows that using the CLI automatically makes you
Common Use Cases
The list of what you can do with the CLI is limited only by your imagination (and hardware), but these are some common uses:
- Create and delete files
- Move, copy, and rename files
- Search for text in files
- Install and remove software
- Connect to remote servers
- Perform system administration tasks
The command line interface (CLI) is a text-based interface that allows users to interact with a computer using text commands. The CLI was first developed in the early 1970s, when computer users began to need a way to interact with the growing number of computer systems. The CLI was initially developed for use with the Unix operating system, but it has since been adapted for use with other operating systems, such as Microsoft Windows and Apple macOS.
The CLI is typically used by advanced users who are comfortable working with text-based commands. The CLI has a learning curve and is not always the easiest way to interact with a computer, so many users prefer to use a graphical user interface (GUI) instead. However, the CLI can be faster and more powerful than a GUI, so it is still used by many users who need to get work done quickly.
Opening a Terminal
To use the command line, you need to open a terminal. On Linux and Mac, this can be done by opening the "Terminal" application. On Windows, you can use the "Command Prompt" application.
On Linux, the Terminal applicationcan be opened by pressing the CTRL + ALT + T keys simultaneously.
On Mac, the command line interface can be opened by pressing the CMD + SPACE keys simultaneously and then typing "terminal" into the Spotlight search bar. The Terminal app can also be found in the Utilities folder inside Applications.
On Windows, the command line interface can be opened by pressing the Windows key + R keys simultaneously and then typing "cmd" into the Run dialog box.
Navigating the Filesystem
You can't do much if you don't know how to navigate the filesystem on your computer. To do this, you use the
cd command to enter a folder. You can type an absolute path, a relative path, or prefix with
~/ to enter a folder relative to the home directory of the user:
# absolute paths start with forward slash cd /etc/ # relative paths start with the folder name, or a period referring to the current directory cd etc/ cd ./etc/ # paths relative from home start with a tilde cd ~/Documents/
* Anything on a line after a
# is ignored. These are used to add comments
You can also traverse into parent folders by using
.. to go up one directory:
# go up one directory cd .. # go up two directories, then into the folder named 'project' cd ../../project
If you forget where you are, you can use the
pwd to display the current directory.
To list files in the current directory, use the
Options are often added to commands to alter their behavior. These are usually passed with two hyphens and an option name, or a single hyphen and a letter for shorthand. In most cases they can be combined to further tailor the command.
Let's use options to recursively list files and folders or sort them by size:
# list all files in the directory and its subfolders ls -R # list files sorted by size ls -S
If you want to list files in a specific directory, use the
ls command followed by the absolute path (starting with
/ to the directory:
If you're in an empty folder, there's probably not a lot to look at, so let's change that.
Create and Remove
To create a file, enter the folder where you want the file to live (you know how to do that, right?) and use the
touch command, followed by the name of the file—be sure to include the extension!
If you want it gone, remove it with
But what about folders? Just use the
mkdir command to create a directory:
To remove a folder, we'll use the
rm command again, but with an important option passed to it:
rm -r test-folder
-r options tell the command that we want to
recursively remove a folder and all of its contents.
** Note: The
rm command deletes files permanently and does not move them to Trash. Be careful!
Now that we have some files and folders, let's look at a how we can move them around.
Moving Files and Folders
Let's move some things around using the
mv command. We do this by targeting a source file or folder, and then adding a destination:
# move a file inside a folder mv sourcefile.txt destination-folder/
There are few very important caveats when using
mv to move a file or folder.
When moving a file inside of a folder, the destination directory must already exist. If it doesn't then the file will be renamed instead. This can be mitigated by adding a trailing slash (
/) to the destination folder name, in which case it will throw an error if it does not exist. It's generally a good practice to end directory names with a trailing forward slash to avoid confusion.
By default, if a file already exists with the same name inside the destination folder, it will be overwritten without prompting you. I recommend overriding this by using the "interactive" option
-i, which will ask when a file would be overwritten.
mv behaves is important, since we'll be using the same command to rename files:
# rename the "original.txt" file to "updated.txt" mv original.txt updated.txt
You can also change the filename and move it inside a folder, all in one command!
mv original.txt destination-folder/updated.txt
Now we can move and rename our files, but what if we want to duplicate them?
cp command is short and sweet, just add the file or files that you want to copy, and the directory that you want it to be copied into. If you want to copy a file into the same directory, include the new name of the duplicated file.
# copy a file into a directory cp test.txt myfolder/ # copy multiple files into a directory cp test1.txt test2.txt myfolder/ # copy a file into the same directory cp original.txt duplicate.txt
Let's wrap up with a nice and easy one. The
clear command does exactly what it says: it clears the console and gives you a clean slate if things get a bit too cluttered.
Done and done.
Of course, this just scratches the surface of what can be done in the terminal. There are plenty of resources around if you want to learn more, like this article by Artūras B on Hostinger or the Command Line Crash Course on MDN.
The command line interface is a powerful and versatile tool that can be used to perform a wide variety of tasks on a computer. With a little practice, you can use the CLI to do everything from managing files and folders to installing software and performing system administration. It can be daunting at first, but once you get the hang of it, you'll wonder how you ever lived without it.
Next time, we'll look at some more exciting features of the command line interface like piping, the superuser, permissions, personalization, and more!
Let me know in the comments if this was helpful or if you have any questions.
Be sure to follow me for more like this!