Getting Started With The Command Line Interface(CLI)

George Githuma
10 min readMar 7, 2023

--

Note to reader: This tutorial has a video on The Machine youtube channel which you can reference whenever you get stuck.

Kali Linux Command Line Interface (CLI)/Terminal

What is a command line interface?

A command-line interface (CLI) also called the terminal, is a text-based user interface used to run programs, manage files and interact with a computer’s operating system or an application. The user enters text commands also called input and the computer responds with text output.

The most common command line interfaces include Bash Shell used in Unix and Linux operating systems, Z shell or Zsh used by mac operating systems, command prompt used by windows, and PowerShell developed by windows but can be used by any operating system.

Command Line Interface Syntax

In computer programming, syntax is the structure of statements in a computer language.

The command Line Interface syntax looks like this:

[command] [option] [argument]

Command — This is a directive to the command line interface program to perform a specific task. It may include checking the current working directory, listing the content of that directory, creating, copying, and deleting contents of that directory, and more.

Option — Sometimes referred to as a flag or a switch, this is a single-letter or word that comes immediately after the command to modify its behavior in a predetermined way. Options bring out more features of a command.

Argument — An argument is a parameter that you pass to the command for it to act on. For example, when creating a file or folder, the name of the file or folder is the argument.

Please note that not all commands take options and arguments, and not every time a command will take in an option or an argument.

Common Command Line Interface Commands

This section will explore some of the most commonly used command line interface commands.

pwd — print working directory

The pwd command displays the current working directory. A directory is also referred to as a folder and defines the user’s location in a computer system.

Syntax:

pwd

When this command is called, the CLI will display the absolute path of the current directory. The absolute path of a file contains the root element and the complete directory list required to locate a file or folder. This is different from a relative path, which is a path relative to the current directory.

Example 1.0

Input:

pwd

Output:

/home/themachine

In the above example, the first forward slash “/” in our file path is the root directory of our computer system. This second part is the “home” directory, which stores all the users in that computer system. The last part is the user account currently using the computer system, the machine.

Please note that the last name in my results will differ from yours. Yours will print the name assigned to your computer system.

ls — list directory content

The ls command lists the content of a directory, including both files and folders.

Syntax:

ls [options] [argument]

Despite having three parts to its syntax, the ls command can be used without an option or an argument. When used this way, it will list everything in its current working directory.

Example 2.0

Input:

ls

Output:

Desktop Documents Downloads Music Pictures Public Templates Videos

These are all the folders in our current working directory.

The second way to use the ls command is with an option or a flag, and without an argument. In this example, we will use the (-l) flag, which will print the long format of the contents of our directory. We will still get the contents of our directory, but with more information.

Example 2.1

Input:

ls -l

Output:

total 32
drwxr-xr-x 2 themachine themachine 4096 Feb 26 22:33 Desktop
drwxr-xr-x 2 themachine themachine 4096 Feb 26 22:33 Documents
drwxr-xr-x 2 themachine themachine 4096 Feb 26 22:33 Downloads
drwxr-xr-x 2 themachine themachine 4096 Feb 26 22:33 Music
drwxr-xr-x 2 themachine themachine 4096 Feb 26 22:33 Pictures
drwxr-xr-x 2 themachine themachine 4096 Feb 26 22:33 Public
drwxr-xr-x 2 themachine themachine 4096 Feb 26 22:33 Templates
drwxr-xr-x 2 themachine themachine 4096 Feb 26 22:33 Videos

The first column is the file type, followed by file permissions, then links (the numbers), owners and groups, file size, date and time, and lastly, the file name. This information is beyond the scope of this tutorial, and we will leave it at that.

The third and last way to use the ls command is with an argument passed to it. As we saw earlier, an argument is a parameter passed to a command for it to act on. In our case, we will ask the computer to display only the content inside our download folder. To see this, I will first download something into the download folder to ensure it is not empty. I will also use the -l flag to show the full command in action.

Example 2.2

Input:

ls -l Downloads

Output:

total 108
-rw-r - r - 1 themachine themachine 108354 Feb 27 21:52 command-line-image.png

cd — change directory

The cd command changes the directory from the current working directory to another directory.

Syntax

cd [argument]

To see how this command works, create a folder called “new_folder” on your desktop. Please include the underscore when naming your new folder, this is a naming convention for files and folders in a command line interface. It helps to minimize errors when your command line program reads your files and folders.

Example 3.0

Input:

cd

Output:

no output

When you run this command, there is no output like in our earlier commands. However, it will make sure that you are in your home folder. To confirm this, let’s use pwd.

Input:

pwd

Output:

/home/themachine

Now that we have established that we are in our home directory, let’s list the content of our home directory.

Input:

ls

Output:

Desktop Documents Downloads Music Pictures Public Templates Videos

Now that we have established there is a desktop directory, let’s cd into it and confirm this using the pwd command.

Input:

cd Desktop

Input:

pwd

Output:

/home/themachine/Desktop

If you did everything correctly, you should see that Desktop has been added to our path, meaning that our current working directory is Desktop. We can now check the content of the Desktop using the ls command.

Input:

ls

Output:

new_folder

Let’s now move into the new folder using the cd command again and confirm our location with the pwd command.

Input:

cd new_folder

Input:

pwd

Output:

/home/themachine/Desktop/new_folder

If you did everything correctly, your output should have “new_folder” added to the file path. This confirms that our current working directory is the new_folder. If you list the content of that folder you will see that it is empty.

We can also use the cd command to move out of new_folder into Desktop by adding a double dot as an argument like this “cd ..”.

Example 3.1

Input:

cd ..

Input:

pwd

Output:

/home/themachine/Desktop

And to go back to our home directory, use the cd command with no argument.

Example 3.2

Input:

cd

Input:

pwd

Output:

/home/themachine

mkdir — make a directory

The mkdir command creates a new directory in the current working directory.

Syntax

mkdir [options] [argument]

To see this command in action, let’s move into Desktop using the cd command and confirm with the pwd command.

Input:

cd Desktop

Input:

pwd

Output:

/home/themachine/Desktop

Let’s make a new folder named “new-folder_2” inside the desktop folder. We will use the -v flag to print out the newly created folder. The v in the -v flag stands for verbose. The verbose option specifies that you want to display detailed processing information on your screen.

Example 4.0

Input:

mkdir -v new_folder_2

Output:

mkdir: created directory 'new_folder_2'

You can confirm that the new folder exists using the ls command.

Input:

ls

Output:

new_folder new_folder_2

You can also create a folder inside another folder without necessarily being inside that folder. This is done by assigning a specific path to the new folder to be created. Let’s create “new_folder_3” inside “new_folder_2” using this method. We will omit the -v flag to see the difference in output.

Example 4.1

First, confirm that you are inside the Desktop folder with the pwd command.

Input:

pwd

Output:

/home/themachine/Desktop

Then go ahead and call the mkdir command passing it the file path argument like this:

Input:

mkdir new_folder_2/new_folder_3

Output:

no output

This time there is no output because the -v flag was not used. We can confirm that the new folder is created using the ls command.

Input:

ls new_folder_2

Output:

new_folder_3

touch — create a new file

The touch command creates a new file inside a directory or folder.

Syntax

touch [argument]

While still inside the Desktop folder, let us create a new file called “new_file.txt” and confirm that it has been created using the ls command.

Example 5.0

Input:

touch new_file.txt

Input:

ls

Output:

new_file.txt new_folder new_folder_2

We can see our newly created file next to our folders. Please note that the file is the one with the .txt extension. This is one way of telling the difference between a file and a folder.

cp — copy file or directory

The cp command, short for copy command, is used to copy files and directories from one location to another.

Syntax:

cp [options] [source_file] [destination]

In the first example, we will copy a file into a folder.

Example 5.0

First, be sure you are inside the Desktop folder using the pwd command. Then confirm that you have the “new_file.txt” file and the “new_folder_2” folder inside the desktop folder with the ls command.

Then copy “new_file.txt” into “new_folder_2” and print the output using the -v flag.

Input:

cp -v new_file.txt new_folder_2

Output:

'new_file.txt' -> 'new_folder_2/new_file.txt'

From the output, we can see that “new_file.txt” has been copied to “new_folder_2”. This can be confirmed by listing the contents of “new_folder_2”.

Input:

ls new_folder_2

Output:

new_file.txt new_folder_3

You can see the “new_file.txt” next to “new_folder_3” inside “new_folder_2”.

The other way to use the cp command is to copy a folder into another folder. To do this, you use the -r flag, which stands for recursive.

In computing, “recursive” refers to a function, algorithm, or process that calls itself repeatedly until a certain condition is met.

In the context of file systems, a recursive operation on a directory means performing the same operation on all of the subdirectories and their contents within that directory. For example, recursively copying a directory will copy the directory and all of its subdirectories and files within them.

Example 5.1

Confirm that you are in the Desktop folder and inside it, you have “new_folder” and “new_folder_2”.

Now copy “new_folder” with the -r flag into “new_folder_2” and confirm the copy by listing “new_folder_2” content.

Input:

cp -r new_folder new_folder_2

Input:

ls new_folder_2

Output:

new_file.txt new_folder

mv — move file or directory

The mv command can rename files and directories or move them from their current location to a new location.

Syntax:

mv [options] source destination

Make sure we are in the Desktop folder using the pwd command. If you are not, move into the Desktop folder using the cd command.

In the first example, you will rename the “new_file.txt” to “new_file_a.txt”. Use the -v flag to print out an output.

Example 6.0

Input:

mv -v new_file.txt new_file_a.txt

Output:

renamed 'new_file.txt' -> 'new_file_a.txt'

You can confirm that there is no longer a file named “new_file.txt” but “new_file_a.txt” using the ls command.

Input:

ls

Output:

new_file_a.txt new_folder new_folder_2

In the second example, we will rename “new_folder_2” to “new_folder_2.1”. Use the -v flag to print out an output of our action.

Example 6.1

Input:

mv -v new_folder_2 new_folder_2.1

Output:

renamed 'new_folder_2' -> 'new_folder_2.1''

You can again confirm that there is no longer a folder named “new_folder_2” but “new_folder_2.1” using the ls command.

Input:

ls

Output:

new_file_a.txt new_folder new_folder_2.1

In the last example, we will move the “new_file_a.txt” into the “new_folder_2”. We will again use the -v flag to print out the output.

Example 6.1

Input:

mv -v new_file_a.txt new_folder_2.1

Output:

renamed 'new_file_a.txt' -> 'new_folder_2.1/new_file_a.txt'

Please note that when you move a copy of a file or folder, it seizes to exist in the current location and moves to the new location. You can confirm this with the ls command where you will find that “new_file_a.txt” no longer exists in the Desktop folder but has moved to the “new_folder_2.1”, its new location.

Check that “new_file_a.txt” is not in the current location.

Input:

ls

Output:

new_folder new_folder_2.1

Check that “new_file_a.txt” has moved to the new location.

Input:

ls new_folder_2.1

Output:

new_file_a.txt new_file.txt new_folder

rm — remove file or directory

The rm command removes files and directories from the current directory.

Syntax:

rm [options] [argument]

To see the command in action, move into “new_folder_2.1” using the cd command. You should see the following content:

Input:

ls

Output:

new_file_a.txt new_file.txt new_folder

Now remove the two .txt files using the rm command. Use the -v flag to print out the result of our action.

Example 7.0

Input:

rm -v new_file_a.txt new_file.txt

Output:

removed 'new_file_a.txt'
removed 'new_file.txt'

Please note the space between the files in our command. You can list as many files as you wish to remove as long as they exist inside your directory. You can do the same when creating, copying, or moving files. You can confirm that these files are deleted using the ls command.

To delete directories or folders, we have to use the recursive flag -r. For our example, first, move back into the Desktop folder using the cd command. Remember the command for moving back to the previous folder is “cd ..”.

Confirm that we have the following folders inside Desktop.

Input:

ls

Output:

new_folder new_folder_2.1

Finally, let’s delete both of these folders. Use a combination of the -r and -v flags, to recursively delete the folders and print out our results.

Input:

rm -rv new_folder new_folder_2.1

Output:

removed directory 'new_folder'
removed directory 'new_folder_2.1/new_folder'
removed directory 'new_folder_2.1'

That is all for the rm command.

Conclusion

Command line interface skill is also referred to as Linux skill. What we have gone through are just a few commands to get you started. If you are interested in learning more I recommend this 8hrs course by Cisco Network Academy. If you prefer youtube, check out this course by freeCodeCamp.

One last thing, have a look at the “man” command. It will help you understand all other commands.

If you liked this #tutorial:

Subscribe to my youtube channel for the #video tutorial

Follow me on Twitter | and LinkedIn for #snippets

--

--