Let’s start this post with a “high-tech” movie scene. The protagonist wants to enter a building and steal some confidential files but there are cameras everywhere. He hires a “tech” guy to help him while he enters the room where the files are. Remember how this tech guy opens up a black screen and types some green lines and shuts down all the cameras? 👨💻 Well, we will definitely not be shutting any cameras in this post (if you got a thought 😋), but will introduce you to that black screen with green lines; the command line interface, or the CLI.
The command-line interface is the medium to interact with the shell of the system. The command line goes way back to the time when fancy icons and user interfaces were not invented. The computer was big and understood just specific commands rather than touches. Today, the attractive graphical user interface has overshadowed the use of the command-line to a bare minimum. A lot of us may never open the command line on our own in our systems while many don’t even know what a Powershell is on Windows. For a long time, it has been regarded as “a programmer’s thing” and not something that can give you a little more control over your system.
This post is written to introduce you to the software and highlight how you can start using the command line to perform basic tasks with it. Collectively, this is a post for beginners and will help you get started with the command-line interface or as we will refer it through the post – CLI.
Table of Contents – Command Line Interface (CLI)
- What is the Command Line Interface (CLI)?
- Basic CLI commands
- Listing all the directories through CLI – The ls command
- Where am I? – The “pwd” command
- How to make a new directory (folder) with CLI?
- How to create a file in a directory (folder) with CLI?
- How to edit a file with CLI?
- How to view file contents using CLI?
- The Flags and Options in CLI
- The Help Command!
- A Few Tips
- Benefits of Using CLI
- Should You Learn CLI?
- Conclusion
What is the Command Line Interface (CLI)?
The command-line interface is an interface that takes the input as command lines and gives the shell’s output on the screen. There is a lot of software that comes as a command-line interface, such as the command prompt on Windows (or Powershell on Windows 10) and “Terminal” in the Mac Operating System. In this post, we will be using “Git Bash” to send the commands to the operating system.
Here is the command prompt on Windows operating system:
Here is the terminal on the Mac operating system.
This interface is just a medium for us to send a few command lines as input to the system via shell. You can search this application on your system in the “Utilities” (Mac) or through the Start menu (Windows). Once we have the command line interface opened in front of us, we can try to get familiar with it.
Basic CLI commands
To start with our journey to know the command line interface, let’s start with the first screen that we get inside it. In this post, we will be using Git Bash to perform the command line activities. The commands are similar to the Linux operating system or Terminal in general. A few commands may vary on command prompt or PowerShell which will be mentioned explicitly.
So, the first thing to note is that the command line interface can be opened directly in any directory through right-clicking (if it is Git Bash) or navigating through File -> Open Powershell.
The first screen will show the current directory we are in:
This shows that my terminal is opened in directory D and the current folder name is “TestProject”. This is also called a “path” of the directory and is known as the “current working directory” or “present working directory”. So when you hear people say “Tell me the path of TestProject folder”, the above output is the answer to that. Command-line interfaces do not use the pointy little friend of ours to navigate. All you have is the keyboard to perform all the actions. Although it is intimidating at first, grasping this skill is really exciting 🤩
While using the command-line interface, consider yourself inside a room. It does not matter how you got here at first but the path at which you open the CLI is the room in which you start. Now, you need to navigate to other rooms as you would do inside a house. To navigate to the correct room, we should know their names to correctly identify where do we want to go.
This is achieved through “ls” command.
Listing all the directories through CLI – The ls command
The “ls” command will list all the directories for you inside.
Or in PowerShell:
Powershell gives us a little bit more information on each directory. Also, notice that the forward slashes in the Git bash become the backward slashes in the Powershell. This has no significance in particular but you would need to always use forward slash while denoting a path in Powershell.
Since now we know the names of our rooms, we can navigate to these directories using the “cd” command which is abbreviated for “Change Directory”. Use the keyword “cd” and the directory name with a space to jump to that directory.
Now you can again list the directories with the “ls” command to know what’s inside the “A” directory. Although from here it might look like we would need to “ls” a lot of the time but you can skip this if you are aware of the complete path of the directory you are looking to navigate.
For example, if I am in the root directory and need to navigate to the “B” directory directly, I can specify the full path with the CD command:
Remember to always specify the path starting from the current directory. Specifying the same path as in the above image by already standing in the “B” directory would give us an error:
This reminds me that CLI is an expert in calling out the errors but shy away when it comes to successful execution. When there is no response to your command on the CLI, consider it as the successful execution of the command.
The CLI also gives us a smoother option to navigate to the past from where we came from but only step by step. This is achieved through the “cd .. “ command. The “cd ..” command takes us back to the parent directory of the tree node in which we currently are.
This was all about navigation basics. Let’s see a few more commands that we can use with the CLI.
Where am I? – The “pwd” command
As a user, if you do not know the present working directory (the directory in which you currently are) you can hit the “pwd” command to let the operating system help you!
Although it was already shown in the yellow color, we would still need it when we start executing complex statements to pick up the directory.
How to make a new directory (folder) with CLI?
Since our shell communicates directly with the operating system and we communicate with the shell, we get the power of using the operating system functions such as making a directory, deleting a directory, etc.
To make a directory, we use the “mkdir” command followed by a space and the directory name. Here, we create a new directory with the “mkdir” command:
Notice there are no success messages which means our execution was successful. Let’s verify it using the “ls” command:
Yes, we got our new directory through the CLI. You can now navigate to the new directory with the cd command.
How to create a file in a directory (folder) with CLI?
Now that we have navigated to the directory, the next step would be to create a file that can be contained in that directory.
To create a file with the command-line interface, we make use of the “touch” command followed by a space and the file name.
You can now “ls” into this directory and confirm if the file is created or not.
How to edit a file with CLI?
Currently, we have a directory and a file already created with us. But an empty file is of no use – like a SIM card without balance. So, we need to recharge our SIM and thankfully bash gives us an excellent interface for that.
To edit the file content, you can use in-built text editors such as Vim or Nano. To access the vim editor, execute the “vi” command followed by a space and the file name.
The editor will open up and you can write the required content into it.
How to view file contents using CLI?
We are now all set with a directory created, a file created and the content is pushed into the file. All we need now is to learn how to look at the content of the file.
To view the content of the file using CLI, make use of the “cat” command followed by the file name.
The content of the file, “this is a file created through CLI” is then shown on the interface.
The Flags and Options in CLI
Flags in the command line are the external operations that enhance the usage of a command line. For example, to view the hidden directories, we can make use of the “all” flag along with the “ls” command.
There are literally hundreds of flags to use with the commands and make use of them in a much richer way. Being in such a large number, there is a very hard choice that you would be able to learn them all. Although, it does not hurt to brief yourself with these flags once in a while because they may sometimes present you with the combination of two or three commands into a single line. This makes them extremely helpful in our usage. So, how to find out flags related to a command? Well, you may take some “help” from the “help” command.
The Help Command!
CLI and shell are very smart in recognizing that we may not be knowing all the commands on our tips of hand and either today or tomorrow will require help. Instead of searching rigorously on the internet about the command you are stuck in, you can ask for help from our shell.
The help command can be executed with the “help” keyword which will list down all the generic commands in the bash or you can use a command and with help to know specific options.
For example, if I am stuck at the “ls” command, I can use “help” as a flag to know about more options and flags that I can use with the “ls” command.
Using the help command with the above-listed commands in this post, you can use them to develop a deeper understanding of them.
A Few Tips
Before wrapping up the basic command section, there are a few tips that should be considered while working on the command line.
- The “up” arrow key will show the latest executed command.
- Capitalization matters in the command line execution.
- If a directory or a file name contains space(s), use inverted commas (“) to wrap the file name into it.
- The “clear” command clears the shell although the present working directory does not change.
Benefits of Using CLI
Coming this much into this post, a small thought might occupy your mind 🧠 If you are investing so much of your time, is there a real use in learning the command-line interface? Out of so many advantages of CLI, a few of them offered by the command-line are listed below.
- Advanced Security: The command-line interface can enhance security and minimize system vulnerabilities.
- Extremely Fast: The command-line interface is much faster in executing the same commands that would take a bit more time using the graphical user interface.
- Multiple Commands into One: The command-line interface lets you handle repetitive processes by combining multiple commands (which means multiple actions) into a single command and cut downtime.
- Control: The CLI gives us more control over the operating system with the shell commands rather than using a GUI.
- Lower Resource Usage: CLI uses extremely fewer resources compared to heavy software performing the same tasks that could be done with a few command lines.
- Stability: CLI is very stable. Once you know the commands, that is it! These commands do not change like software versions.
With all these benefits of the command-line interface which cannot be ignored, people often find themselves comfortable with heavy GUI software. I agree that using GUIs is great as they are interactive while CLI looks a bit dull. Also, CLI is a bit hard to learn at the beginning but once picked up, I am sure you will love the strengths of the command-line interface. Some matured programmers still use Vim editor as their default editor and are proud of that. I hope once you give CLI a chance, you will be loving it as much as they do 🧡
Should You Learn CLI?
There is a great chance that if you navigated to this post, you are probably related to information technology and related branches with a hunch for technical development. You are probably at the beginner’s stage where you have seen developers use this black screen to do stuff that you could not understand. Looked cool, didn’t it? 😎
Not only it looks cool, but there are also a few things that require the command line as an extremely important tool for their execution. For example, Git code push and pull require Git Bash which makes use of the shell. Or perhaps you want to use TestProject’s Agent CLI 😊 When you will start to go deep into your field, the requirement of the command line will keep on increasing in your work. So the developer you saw before starting your career, they probably weren’t acting cool but were doing their “normal work”.
Another probability is that you are a non-technical user who is fascinated by the command line interface and wants to learn about it. As far as my experience goes, you have made a very good choice in utilizing your system effectively. Consider a scenario where you want to know the IP of your system. You can visit a website for that or download a tool. But compare it with the time it would take to open the CLI and type “ipconfig” in it. There are a lot of things that take a few seconds on CLI and you would require sooner or later. In addition, it would definitely make you look cool in front of your non-technical friends 😉
Conclusion
The command-line interface is an extremely important tool to use. A must-know skill if you are involved with a computer in your day-to-day life. The CLI can process things a lot more faster and gives you more control over your system. CLI is a deep-sea which cannot be described using a single post in any way 🌊
This post is crafted for beginners who have just started to show their interest in CLI and are exploring the potential with it. With all the basic commands and introduction to the flags, this post introduced you to the most powerful software in your system. I hope your CLI becomes your good friend after reading this post and would love to hear your favorite command in CLI. It can be as basic as the “ls” command or as complex as a custom command.
Looking forward to seeing some great commands in the comments section below! 🤓