Command Line Interface (CLI) Tutorial - How Advanced Users Interact with Computers

Author:Murphy  |  View: 29795  |  Time: 2025-03-23 19:35:23

Do you recall that movie scene where a hacker suddenly pulls up a dark screen on their laptop, typing away ferociously for a few seconds and some important event happens? In those scenes (and if they were actually happening), the hackers are interacting directly with the computer through textual inputs, as opposed to how we usually interact with our phones or laptops through a Graphical User Interface (GUI). In this post, we are going to walk through how you too can interact with your computer using only text. Odds are that by the end of this post you will even find this method to be more efficient than the usual GUI.

Command line interface or CLI is a method of interacting with our computers through typing text-based commands into a terminal (or console). This differs from the usual Graphical User Interface or GUI that most users are used to. An example of a GUI is an iPhone's iOS that the interface is a graphical one where users can tap and swipe to interact with the device. Advanced and technical users consider CLI a superior means of interaction with computers since CLI allows a more efficient and direct control over the computer.

I recall how daunting CLI seemed to me, before I had started playing around with it. But the learning process proved to be very easy, rewarding and more importantly fun. So I decided to create this tutorial to share my learnings – you too can consider yourself "initiated" once you go through this post!

I will first include a tabular format cheat sheet with the commands that I have covered in this post, which is convenient for future use and then will cover each of these commands in more detail with examples in the remainder of the post.

Let's get started!

(All images, unless otherwise noted, are by the author.)


CLI Cheat Sheet

Feel free to refer to this table (or save it) for future use, after you have gone through the tutorial with examples.

CLI Cheat Sheet

From this point on, I will go through each of these commands in more detail with examples. I would encourage you to follow each step and learn by practicing as you read through the tutorial.


1. Where Is The CLI?

Let's start from how to launch the CLI.

  • Mac: We are looking for the "Terminal". So go ahead and search for the "Terminal" in you Mac's Spotlight Search.

Pro Tip: You can hold down "command" and while holding it down press the space bar and the Spotlight Search should open. Then just start typing "Terminal" and press "return" when it shows up in the Spotlight Search.

  • Windows: CLI is called the "Command Prompt" in Windows. Click the "Start" button and type "cmd" in the search bar. Then click on the "Command Prompt" or "Windows Command Processor".

2. Getting To Know Commands

Once my Terminal is launched, I see the below (yours should be slightly different with the same structure):

farzad 20230211_CLI %

In the above, "farzad" is my username and the "20230211_CLI" is my current directory. This is the directory where I am creating this tutorial, dated for today. Instead of that directory, you may see a tilde symbol (~), which represents the home directory.

This is the very beginning and where we can start typing up commands.

As we saw in this example, the command line shows us where we are (e.g. "20230211_CLI" in the above example) but if we want to see the full path to our current location we can use the pwd or print working directory command as follows:

farzad 20230211_CLI % pwd /Users/farzad/Downloads/M/Archive/20230211_CLI

First line is the command and the second line shows the full address starting from my home directory (i.e. the highest level directory) up to the current location.


3. Listing Files and Directories

It would be nice to know what files and folders are available where we are. In a GUI, we can just look at the files and folders and in CLI, there are some commands to do that.

We can use the ls or list command to view the contents of a directory. Here is what I see when I use this command:

farzad 20230211_CLI % ls CLI.ipynb random-folder

The first list is the command and the second line is the results. The second line informs us of a file named "CLI.ipynb" (which is the Jupyter notebook where I compose this post) and a directory named "random-folder".

But what if we wanted to see more details about each file or directory? For example, it would be nice to see the size of the file, etc. We can accomplish this by adding a -l (i.e. long format) option to the existing list command, as follows:

farzad 20230211_CLI % ls -l total 8 -rw-r--r-- 1 mafarzad staff 3673 Feb 11 17:40 CLI.ipynb drwxr-xr-x 2 mafarzad staff 64 Feb 11 17:35 random-folder

This has a lot more information. Let's see what this all means, starting from the left.

  1. Permissions: This is a series of 10 characters indicating the permission for that specific file or directory, as follows:
  • Character 1 indicates the type of file: for regular file; d for directory; l for symbolic link
  • Characters 2 to 4: Permissions of the owner
  • Characters 5 to 7: Permissions of the group
  • Characters 8 to 10: Permissions of other users
  • These nine characters can consist of the following:

    • r: Read
    • w: Write
    • x: Execute
    • -: Permission not granted
  1. Number of Links: Number of hard links to the file or directory
  2. Owner: Username of the owner of the file or directory
  3. Group: The group name associated with the file or diretory
  4. Size: Size in bytes
  5. Date and Time: Date and time of the last changes made to the file or directory
  6. Name: Name of the file or directory

4. Navigation

Now that we know there is a directory in my location named "random-folder", let's see how we can go inside that directory.

We can use the cd or change directory command to navigate around, as follows:

farzad 20230211_CLI % cd random-folder  farzad random-folder % 

In the above command, I used cd to navigate to the "random-folder" and then the second line shows that the current location is now "random-folder" (instead of the "20230211_CLI" where we were before this).

But how can we go back one level of directory? In other words, now that we are in the "random-folder" location, how can we go back to where we were before, which was "20230211_CLI"? We can use the change directory command followed by a space and two periods. I will use pwd before and after this command to demonstrate how this works in the below example.

farzad random-folder % pwd /Users/mafarzad/Downloads/M/Archive/20230211_CLI/random-folder

First, we just used pwd to confirm we are at "random-folder". We also see that "20230211" is just one level above our current location. Then let's use the cd .. command to jump one level (make sure to include one single space between cd and ..) as follows:

farzad random-folder % cd .. farzad 20230211_CLI % pwd /Users/mafarzad/Downloads/M/Archive/20230211_CLI

Here we first used cd .. to go to "20230211_CLI" and then used pwd to confirm the location.

Because I want to use the "random-folder" in our next example, let's practice going back to the "random-folder" location again as follows:

farzad 20230211_CLI % cd random-folder  farzad random-folder % pwd /Users/mafarzad/Downloads/M/Archive/20230211_CLI/random-folder

5. Create and Delete Files and Directories

touch command can be used to create a new file. In the command below, I will create a new file named "new_file.txt" and then use the list command to return the contents of this directory:

farzad random-folder % touch new_file.txt farzad random-folder % ls -l total 0 -rw-r--r-- mafarzad staff 0 Feb 11 18:04 new_file.txt farzad random-folder % 

In the very first line, I created the text file and then using the list in long format (i.e. ls -l) I confirmed the file was created. It is interesting to note that total 0 indicates there are no files in that directory, while the file name is actually listed there, due to the fact that the newly-created file is just a name at this point, with no real content.

We can use mkdir or make directory, followed by the name of the directory, to create a new folder (or directory) as follows:

farzad random-folder % mkdir new-folder farzad random-folder % ls -l total 0 drwxr-xr-x 2 mafarzad staff 64 Feb 11 18:08 new-folder -rw-r--r-- mafarzad staff 0 Feb 11 18:04 new_file.txt

In the first line, I created a new directory named "new-folder" and then using long format list, we see the folder is created along side the file that we created in the previous step.

Next, let's delete the text file, using the rm or remove command, followed by the name of the file, as follows:

farzad random-folder % rm new_file.txt  farzad random-folder % ls -l total 0 drwxr-xr-x 2 mafarzad staff 64 Feb 11 18:08 new-folder

In the first line of command, I removed the text file and then using the long format list we see that only the directory is now present in the current location and the text file is deleted.

And finally, we can use rmdir or remove directory, followed by its name, to remove the directory, as follows:

farzad random-folder % rmdir new-folder  farzad random-folder % ls -l total 0

First line of command removes the directory and using the long format list we confirm it was removed.


6. Copy Files or Directories

6.1. Copy Files

The idea is similar (but not identical) to copy and paste in Mac and Windows operating systems but it can be done in one line with the cp or copy command, followed by the name of source file and then the name of the destination file, with spaces between each two. The format of the command would be as follows:

cp source-file destination-file

Let's implement it in an example. First we create a file named "file-1.txt" so that we have something to copy and then use the list command to confirm the file is there.

farzad random-folder % touch file-1.txt farzad random-folder % ls file-1.txt

Next, let's copy "file-1.txt" to a new file named "file-2.txt" and use the list command again to confirm our changes.

farzad random-folder % cp file-1.txt file-2.txt farzad random-folder % ls file-1.txt file-2.txt

As expected, now there are two files, including both the first text file (i.e. the source file) and the second text file (i.e. the destination file).


6.2. Copy Directories

The idea is very similar to how files are copied but we add the recursive option -r to the cp command (i.e. cp -r). Recursive means that the cp command copies the directory and all of its contents. The overall format of such a command is:

cp -r source-directory destination-directory

Let's walk through an example. First, let's see what files exist in our current directory.

farzad random-folder % ls file-1.txt file-2.txt

Next, let's go one level up in our directories and see what directories and files are available there.

farzad random-folder % cd .. farzad 20230211_CLI % ls  CLI.ipynb random-folder

Now, let's copy the "random-folder" directory and all its contents into a second directory named "random-folder-2", as follows:

farzad 20230211_CLI % cp -r random-folder random-folder-2

Next, let's use the list command to confirm the new directory was created. Then we will use the change directory command to go inside the newly-created directory (i.e. "random-folder-2") and use the list command there to make sure everything that existed in the original directory of "random-folder", has been copied into the destination directory of "random-folder-2".

farzad 20230211_CLI % ls CLI.ipynb random-folder random-folder-2 farzad 20230211_CLI % cd random-folder-2 farzad random-folder-2 % ls file-1.txt file-2.txt

As expected, the newly-created "random-folder-2" includes both text files that we had in the source directory of "random-folder".


7. Remove (Delete) Files or Directories

The idea behind this one is pretty simple – we simply wish to delete files or folders. We can use the rm or remove command, followed by a space and the file name to remove files. Removing directories is exactly the same but we need to add the recursive option of –r to the command. Here are the general formats to be followed:

rm name-of-file-to-be-removed rm -r name-of-directory-to-be-removed

Let's see what files we have in this directory and then we can remove (delete) one of them for practice.

farzad random-folder-2 % ls file-1.txt file-2.txt farzad random-folder-2 % rm file-1.txt  farzad random-folder-2 % ls file-2.txt

First line used the list command to list the contents of our current location, where there were two text files. In the third line (or the second line of command), we used the remove command to remove (or delete) "file-1.txt", and then used the list command to confirm it was removed.


8. View Contents of Files

So far, we only looked at names of files and directories but what if we wanted to see the contents of a file? We can use the cat or concatenate command to view the contents of a text file. For this purpose, I've added the link to follow me on Medium in the file named "file-1.txt". Let's see how we can use the concatenate command to view the content of this file.

farzad random-folder-2 % ls file-1.txt file-2.txt farzad random-folder-2 % cat file-1.txt  If you liked this post, follow me on Medium at:  https://medium.com/@fmnobar

The first command line used list to return the name of files in the current directory. In the second line we see there are two files in our current location. Then on the third line (or the second line of command), we are using the concatenate command to take a peak inside "file-1.txt" and you see the results in the very last two line as:

If you liked this post, follow me on Medium at:  https://medium.com/@fmnobar

9. Search

There are many occasions when we need to search for a string of characters and we can accomplish that by using the grep command. This command allows us to search for a pattern (we call the string of characters a "pattern" in this context) in a file. For example, if we wanted to search for the word "medium" in "file-1.txt", we would do so as follows:

farzad random-folder-2 % grep medium file-1.txt https://medium.com/@fmnobar

The first line of command uses grep or global regular expression print command to search for the word "medium" in "file-1.txt" and the second line is the results of this search. Note that the "file-1.txt" includes two lines but the grep command only returns the line that includes the pattern we were looking for.

The grep command is quite versatile and flexible. For example, let's look at two options that can be used:

  1. -c option is used to display only a count of the number of lines that contain the pattern we looked for. For example, let's see how many times the pattern "medium" occurs in our "file-1.txt". We know the result should be 1 and let's verify that.
farzad random-folder-2 % grep -c medium file-1.txt 1
  1. -v option is used to display all lines that do not include the pattern we looked for. We know that there was only one line in our "file-1.txt" that did not include the pattern "medium" – let's verify that in CLI.
farzad random-folder-2 % grep -v medium file-1.txt If you liked this post, follow me on Medium at: 

Conclusion

In this post, we talked about how Command Line Interface (or CLI) can differ from a Graphical User Interface (or GUI) and why technical users tend to prefer CLI over GUI for added efficiency, productivity and flexibility. Then we walked through the most common commands used in CLI with examples. Having gone through these examples, you should feel comfortable firing up your Terminal and start practicing the use of CLI in your day-to-day work. Similar to any other skill, the more you use CLI, the easier and more rewarding you will find it to be. Hope this post gave you a head start in your CLI journey!


Thanks for Reading!

If you found this post helpful, please follow me on Medium and subscribe to receive my latest posts!

Join Medium with my referral link – Farzad Mahmoodinobar

Tags: Artificial Intelligence Data Science Interview Programming Science

Comment