Getting Started with the CLI

Becoming a truly developer

Lucas Torres
7 min readApr 25, 2022

Written by Lucas Torres— April 24, 2022

Introduction

Since you’re reading this article, you’re probably having problems dealing with CLI. To be honest: I used to have a lot of problems with this, but that all changed when I stopped what I was doing and said to myself : “Bro, what are you doing? You’re spending hours to do something stupidly easy on the CLI. You should study it”. Well, that’s what I did and now you’re doing exactly the same. Awesome! So let’s begin our tour.

First of all, CLI is an abbreviaton for Command Line Interface. Now I have a question for you: do you really know what is CLI? Well, if your answer is “yes” that’s really impressive! But if your answer is “no”, don’t be sad, I had no idea what CLI was when I started studying it. Here’s a definition from codecademy, which I think is the best one:

The command line interface is a program on your computer that allows you to create and delete files, run programs, and navigate through folders and files. On a Mac, it’s called Terminal, and on Windows, it’s Command Prompt.

As you saw, each OS has it own “CLI”, but in this article we will focus our efforts in Linux CLI.

Syntax

The annatomy of commands are:

  • command parameter1 parameter2 … parameterN
  • command -options arguments

Please, do not forget about this syntax, this will make your life easier forever. Also, every time you want to run a command, just press “enter” on your keyboard.

Where are we?

The fisrt command I’ll show to you has the function to asnwer the question “where are we?” in terminal. Every time you open a terminal, you are always working in a directory, or folder, in your computer. To see which directory is it, you just have to type pwd and hit enter.

Here we can check the my rootfolder — represented in terminal as “~” symbol — is the directory /home/lucastorres. Which makes sense, sincelucastorresis my user and homeis a default folder in Ubuntu.

List files and folders

We can also look for all files and folder in our directory using the command ls. This command has the following syntax:

ls -options directory

The options that you’ll probably want to use sometimes are listed below.

  • ls -a - List all files including hidden file starting with “.”.
  • --color- Colored list.
  • -d- List directories (usually used likels -d */to list only directories in the current folder).
  • -l- List with long format (show permissions).
  • -la- List long format including hidden files.
  • -ls- List long format with file size.
  • -s- List file size.
  • -t- Sort by time and date.
  • -X- Sort by extension name.

Change Directory

Ok, but what if we want to work in a different directory? So you have to use the Change Directory command, or cd . This command has the following syntax:

cd directory

There are some Unix/Unix-Like commands you can use to make your CLI navigation faster, here are some:

  • cdor cd ~- Will always put you in the root directory.
  • cd .- Will always put you in the same directory you are currently.
  • cd ~username- To change to /home/usernameroot directory.
  • cd ..- To move you one folder up (if you are in. /home/lucastorresdirectory and use this command, you will go to /homedirectory).
  • cd -- Will move you to the previous folder.

Making a Directory

To make a directory (or dir) is an easy job to do in CLI. You just have to use the command mkdir. This command has the following syntax:

mkdir dir1 dir2 ... dirN

Here, we make sure we are in the/home/lucastorres/Documentsfolder, we list all files and/or folder inside it and we create three new directories named “CLI”, “CLI2” and “CLI3”.

Note: if you try to create a folder that already exists, the command will raise an error like mkdir: cannot create directory 'dir': File exists, where 'dir'is the name of the directory you just tried to create .

Removing a Directory (or File)

Ok, now you might be thinking “How can I delete these dumb folder I have just created?”. The answer is also simple: if “make directory” is mkdir, so “remove directory” is rmdirright? YES! IT’S EXACTLY THAT! But maybe not…

Well, if you want to remove an empty folder, you can just use rmdir dircommand. But most of the time you’ll probably want to remove a dir with all files and folders inside it. We call this “recursion”. To recursively remove a directory you must use rm -r dircommand.

Actuallyrmcommand can be used to remove files or folder. This command has the following syntax:

rm -options file1 file2 file3

The options that you’ll probably want to use sometimes are listed below.

  • -for --force- Ignore nonexistant files, and never prompt before removing.
  • -i- Prompt before every removal.
  • -I- Prompt once before removing more than three files, or when removing recursively. This option is less intrusive tha -i, but still gives protection against mistakes.
  • -r, -R, --recursive- Remove directories and their contents recursively.
  • -d- Equivalent to usingrmdir.
  • -v- Verbose mode. Explain at all times what is being done.

In the example above:

1- We list all folders and files inside /home/lucastorres/Documentsdirectory;

2- We list all folders and files inside CLI2, CLI3 and CLI repectively;

3- We remove empty folders CLI2 and CLI3 rmdircommand;

4- We list /home/lucastorres/Documentsagain to confirm that those folders were removed;

5- We remove dumb text files from /home/lucastorres/Documentsdirectory;

6- We list/home/lucastorres/Documentsagain to confirm that those files were removed;

7- Then, we recursively remove CLI folder and everything inside it, with verbose mode (that’s why “removed ‘CLI/dumb.txt’” and “removed directory ‘CLI’” were promped);

8- Finally, we double check if CLI folder was correctly removed from /home/lucastorres/Documentsfolder.

Creating Files

To create a new file in CLI, we use the command touch. It syntax is:

touch file.format

This is extremely useful, mainly when you are coding and want to refactor your code. Here’s an example where we create a directory for our project and four new files, with different formats.

See how easy is it? Awesome, isn’t it? Now, let’s go to the next section (and by “section” I actually mean “command” hahaha).

Moving Files/Folders

Well, you already know how to create and remore files and/or folders, how to list them and change directories, but what about moving them?

Well, to move a file you can simply use:

mv -options source destination

There are only two options you can use in this command:

  • -i- Interactively proccess, write a prompt before moving a file that would overwrite an existing file;
  • -f- Force overwriting the destination.

You can see an example of mvusage below.

App2is a copy of Appfolder. If we try to move files from Appto App2, an error would be raised. So to avoid that, we can use -ioption, to ask what files we want to ovewrite, or -foption, if we want to overwrite all files. In this case, we used -ioption and overwrited all files except main.py. That’s why when we call ls Appcommand, only main.pyis shown.

Copying Files

To copy files or folder we use the command cp. The syntaxfor this command is:

cp -options source1 source2 ... sourceN destination

You can check all available options below.

  • -f- Specfies removal of the target file if it cannot be opened for write operations;
  • -H- Makes the cpcommand follow symbolic links (symlinks) so that the destinations has the target file rather than a synlink to the target;
  • -i- Prompts with the name of a file to be overwritten;
  • -n- prevents accidentally overwritting any files;
  • -p- Preserves the following characteristics of each source path in the corresponding target: the time of the last modification and the time of the last access, the ownership (only if it has permissions to do this), and the file permission-bits;
  • -Ror -r- Copy directoris recursively;

Note that you can copy many source files/folder to a single destination folder.

Conclusion

Of course, there are hundreds of commands you can learn to improve your CLI knowledge, but you should feel pretty confident if you at least mastered all these commands.

Also, this article was inspired by How to use the Command Line Interface (CLI) | The Startup (medium.com). Which I strongly encourage you to read.

As always, if you have any feedback or found mistakes, please don’t hesitate to reach out to me.

References

  1. How to use the Command Line Interface (CLI) | The Startup (medium.com)
  2. Linux and Unix commands, information, and help (computerhope.com)
  3. Dataquest: Learn Data Science — Python, R, SQL, PowerBI

--

--

Lucas Torres
Lucas Torres

Written by Lucas Torres

Electrical Engineering Student in Universidade Federal do Rio Grande do Norte. Enthusiast of Data Science, Programming, Automation and control.

No responses yet