Those of you old enough to remember when the earliest computers were introduced may recall MS-DOS and other command-line systems. The first computers did not have pretty desktops like we have come to expect today and were only told what to do using a series of written commands.
Desktops have largely replaced command-line interfaces for modern users and with good reason: it takes a lot of work to navigate a command line. Who wants to type in a command to find a file when you can just use your desktop? While the command line may not be as useful for ordinary users, it can give programmers superpowers.
Command lines may seem intimidating. There’s lots of black and white text, and sometimes you can see scary error messages. In this guide, we’re going to help you get to grips with the command line so you’ll be ready to work with command line interfaces like a professional.
Why Do We Use the Command Line?
Consider the following scenario: you need to read the first three lines of all the files in a folder. You could go and find those files on your desktop and then open them all individually, but that would take a lot of work. What if you wanted to search all of those files?
This is not a feature of desktop-based interfaces because most consumers don’t need to use it. Programmers, on the other hand, do often need these features.
The command line is still used widely by developers because it gives you more control over your computer. You can find files, navigate through your computer, monitor processes and perform a number of other tasks through the command line. You can even sign in to other computers and access their command line interfaces, if you have permission to log in.
The command line is most associated with Linux users and system administrators, but it’s a tool most developers use at some point. To install Python on your computer, you can either download IDLE online or you can use the command line. But to run a complex program and install its libraries, you may have to use the command line.
Getting Started With the Command Line
The command line allows you to navigate through your computer and run programs. You can think of the command line as a way to talk directly with your computer. Whereas desktop interfaces control what you can and can’t do, the world – or computer – is your oyster when you’re working with the command line.
Firstly, open a terminal window. On Linux and Unix-based systems such as macOS, the command line is called Bash and you can open it by searching for the program “Terminal” on your computer. There are also applications out there like Hyper which you can install as alternatives to the default terminal on your computer.
Once you’ve set up a terminal, you’re ready to start. Let’s discuss a few of the most common commands – instructions – you’ll need to know when using the command line.
We’re going to split this tutorial into three parts:
- Moving around the file system
- Changing files and directories
- Input and output
Part 1: How to Navigate Through Your Files
Before we talk about making changes to a file system and scripting, we’ve got to talk about the basics. The place where every newbie to Linux starts is finding files on their system.
In your command line, type in the following command:
What do you see? In my case, I see the following:
Applications Desktop Documents Downloads Library Pictures Public Sites
This command allows you to list the contents of a folder. Running this command helped me see that the folder I’m in right now contains a number of other folders, like Pictures and Desktop. You can also list the contents of a specific folder by specifying the name of that folder:
Working Directories and Filesystems
The folder that you’re currently in when using the command line is called your current working directory. By default, the
ls command runs in your current working directory, unless you specify a folder whose contents you want to list.
Files in Linux use a tree structure and so each folder can contain many subfolders. At the top of the tree is a folder called the root directory. On a Linux computer, this is found at /.
If you want to know what directory you are currently viewing, you can use the command pwd:
This command returns: /Users/James. pwd allows me to see where I am in the filesystem.
Moving Around the Filesystem
On a desktop computer, it’s easy to move around the filesystem. All you need to do is open a folder viewer like “Finder” on Mac and click on a folder. With the command line, it’s also easy to move around the filesystem. Instead of clicking on folders, you can use the
cd command to move around the filesystem:
In my case, if I run
cd Desktop/, I am moved to the Desktop. If I run
pwd again, /Users/James/Desktop is returned. This is because cd has changed my current working directory to the Desktop folder.
cd is an easy command to remember because it stands for change directory. When you start using this command, you’ll know that in order to Change Directory, you need to use cd.
How to Create a Directory
You now know how to move around the filesystem and view files and folders. The next step is to actually create a folder. Creating a folder in the command line is accomplished using the
mkdir command. This command creates a new empty folder at the location you specify.
The following command will create a folder called “new-folder” in your current working directory:
Now, if I run
ls again, I can see that a new folder has been added to my current working directory (which we changed in the last example to be /Users/James/Desktop):
As you can see, there are two items in my Desktop folder. There’s a file called “to-do.md”, and there is the new folder that we just created called “new-folder”.
Part 2: Changing Files and Folders
Well done on making it this far. Go make yourself a cup of tea or coffee as a reward, or do something else that you enjoy! The Linux command line isn’t the easiest tool to learn for beginners, so you should congratulate yourself on completing part one of this tutorial.
Now we’re going to discuss how to change the filesystem. So far, we’ve listed files and created folders, but that’s only the start of what we can do with the command line.
How to Create and Edit a File
You are probably used to clicking “File > New” to create a new file on your computer. That’s the procedure that most programs use to create new files. In the Linux command line, it’s a different story. All you need to do to create a file is run the “touch” command:
When executed, this command creates a file called “linux_tutorial.txt”. It is an empty file, but we can open and edit it as we want.
In Linux, there are a number of different ways you can edit files. One of the most common ways in which beginners edit files is through
nano, an easy-to-use text file editing tool for the command line. Run this command in your terminal:
This command will allow you to see the contents of the file we created and edit the file. Once you’ve made your edits, press Command-X (or equivalent), type Y, then press enter. This will save your changes to the file.
How to Copy a File
Right click. Copy. Right click. Paste. That’s how you copy and paste files on a desktop computer. You’ll notice a trend going on, but this is not the case with the command line. To copy a file or folder on the command line, you can use the
Here’s an example of this command in action:
cp linux_tutorial.txt linux_tutorial_part_two.txt
This command allows us to copy the file “linux_tutorial.txt” into a file called “linux_tutorial_part_two.txt”. The first word after “cp” is the file you want to copy and the second is the destination where you want to copy that file or folder.
How to Move a File
You can use the
mv command to move a file into a folder in Linux. The first argument you specify should be the file that you want to move and the second one should be the folder in which you want to move that file.
The following command allows us to move our “linux_tutorial.txt” file into the folder “new-folder”:
mv linux_tutorial.txt new-folder/
/ at the end of our folder name is not required, but it helps show that we are moving our file into a folder.
How to Delete a File or Folder
You can use the
rm command to delete files and folders. The
rm command itself allows you to delete files or blank folders. If you want to delete a folder which contains files, you need to specify the
-r flag alongside the command.
In Linux, a flag is used to modify the behavior of a command. For instance, the “-r” flag removes all files and folders within a folder, when used with the “rm” command. Another example of a flag is “ls -a”, which lists all the contents of a folder, including hidden files and folders.
The following command deletes the contents of the “new-folder” folder on our desktop:
rm -r new-folder/
It’s worth noting that when a file is deleted in Linux, it is gone. There is no trash can like there is on your desktop. Before you delete a file, double check to make sure that the file you have selected is in fact the one that you want to delete.
Part 3: Input and Output
You’ve reached part three, the final portion of this tutorial. In this section, we’re going to talk about viewing files, searching files and redirection. You now know how to move around the filesystem and make changes to files, but there’s more we can do with these commands.
When you see Linux experts using the command
cat, you should know that they’re not talking about the animal. They are talking about the command that allows you to view a file. The
cat command displays the contents of a file to the terminal.
This command allows us to see the contents of the “linux_tutorial.txt” file:
In my case, this returns:
This is a Linux tutorial. It's still a work in progress.
When you’re working with files in Linux, it’s common to want to search through that file. That’s where the
grep command comes in handy. While the
grep command can get quite complicated if you use different flags, it’s quite easy to use if you keep it simple.
Suppose we want to search for the word “Linux” in our “linux_tutorial.txt’ article. We could do so using this command:
grep "Linux" linux_tutorial.txt
This command returns:
This is a Linux tutorial.
You can see that the
grep command has searched through our file and returned the line of text which includes our search. If the word “Linux” had appeared multiple times in our file, then each line containing the word would have been returned.
In Linux, redirection is a technique where you can use the output of a file or command as an input for another.
Suppose you want to make a list of all the files and folders in your “Desktop” folder and save it to a folder called “folders.txt”. You could accomplish this task using the following command:
ls > folders.txt
Open the “folders.txt” file using nano (or your preferred text editor) and you’ll see that the output of the
ls command was added to this file.
In this example, the” >” symbol is used to redirect outputs from the command on the left hand side of the symbol to the command on the right hand side. It’s worth noting that this symbol replaces the contents of a file. If you want to add the result of a command to the end of a file, you should use the “>>” symbol.
Congratulations! You’ve made it. Give yourself a pat on the back because the Linux command line is a difficult tool for beginners to learn. It looks very different from a desktop interface and movies don’t exactly do it justice.
In movies, the command line looks intimidating and is associated with hacking; in the real world, it’s a valuable tool in the arsenal of any Linux operating system user or programmer.
In this guide, we discussed three main parts of working with Linux systems: navigating the file system, making changes to files, and input and output.
There’s still a lot more for you to learn. There’s a variety of flags you can use to customize the commands we’ve discussed in this tutorial even further. We’ve not even talked about piping or environmental variables. To find out more about a command, you can run “man [command]” in your terminal. This will show you the manual page for that command.
The commands we’ve talked about here are the foundation of the Linux command line. If you don’t know how to use them, you’ll never be able to use more complex commands.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more