piablo @ D_D Academy & Mentorship
August 15, 2022
Twitter @Skruffster
Created by:
Avatarpiablo @ D_D Academy & Mentorship
August 15, 2022
Twitter @Skruffster
A CLI is an invaluable tool for development. It's actually quite straightforward to use, but just a bit tricky to get used to initially. You can't see everything readily, like you would in your computer's graphical user interface, but we'll teach you how to navigate the pathways, so you can get around easily. CLI stands for Command Line Interface. Generally speaking, Linux users call a CLI a terminal, Mac users call it a console, Windows users a shell. All these names have some technical differences, and we are going to stick with CLI for now. Happy travels!
Come on, it's retro, so it must be cool! By using text commands, a CLI lets us right into the root of our commuter's file system i.e. its operating system, which is something very powerful, so extreme care is needed. We can also access a world of open source work (repositories, libraries, etc) that our fellow developers have created before us. It’s acts as a gateway for us to code and collaborate with the outside world using our local machine. And that's what makes it cool.
There are a few concepts we first need to understand before we start. A CLI is really old school, but it’s tried and tested. Still wondering? Using text is extremely resource efficient. No graphics. Text for input, text for output. For every task in the CLI, there’s a user command/programme for it. Each command is wrapped in the shell programme. This shell is an interpreter for any commands to the kernel (the operating system), which in turn is the gateway to all our hardware.
In our tutorial, we’ll only look at some basic commands to get you up and running.
You can’t just place your mouse pointer anywhere in the window. Keyboard shortcuts you already know, won’t work the same in here. We do want you to remember CTRL+C, because it lets you abort any programme, or command that’s running e.g. if you make a mistake and the program gets stuck. But it won’t copy any text in there for you! And your arrow keys up and down will only take you through your previous commands. Handy for not re-typing long commands that need to be reused. Arrow keys left and right will let you move along the prompt line, if you need to correct a spelling error.....of a previous command! Below is what a CLI for Linux Ubuntu would look like:
If you are using a Mac or Windows, read below how to get your set up and then you can continue along with the rest of the Linux folks. We also include an extensive list of commands nearer the end of this lesson, but don't take any risks, and learn the basics first.
Use CLI commands with great attention! Using them wrongly can easily delete files or destroy your computer system completely!!
Before using the CLI environment for MacOS, you will need to install Homebrew.
What is Homebrew and why do we need it? Homebrew or
Once it has been successfully installed, a list of its commands can be found by using the
Before you get started using your Windows machine for development, you will need to download and install Windows Subsystem for Linux 2. An easy guide to downloading and installing WSL-2 can be found right here
Once WSL-2 is installed on your machine, the commands are essentially the same as they are in Linux. You can find a list of some of the most basic Linux commands in the list above in the Linux section.
Right, now that we are all on the same Linux page, let's open a CLI up and have a look. From your Linux keyboard type press
Let’s get moving around. When you’re in your Home folder, or Home directory as we call it, you will see the
myname@pcname:~$
Let’s just see how to check which directory you are in if you ever get lost. In lower case type the
myname@pcname:~$ pwd /home/myname myname@pcname:~$
Good, you’re still in your home folder! Our last line, known as the prompt line, or command line, is prompting you to type a new command if needed.
Sometimes you will run a command and get a lot of output. At other times, no new text output appears, just a new prompt line. That signifies a successful command result. There's no “hey, it worked!” confirmation, just silent efficiency!
Now that working directory! It's what the shell understands as the default location for carrying out any file operations. So if you create, search for, delete a file or directory, the shell will assume you want to do that in the directory you are working in. Otherwise you specify exactly where you want those operations done. Remember
Our
myname@pcname:~$ cd / myname@pcname:/$
The working directory is now
myname@pcname:~$ cd home myname@pcname:/home$ pwd /home
You guessed it, you’re in your home directory, a sub directory of
Trying to
bash: cd: WRONG DIRECTORY: No such file or directory,
Let’s try a few moves. Remember to
myname@pcname:/home$ cd .. myname@pcname:/$ pwd myname@pcname:/$ cd myname@pcname:~$ pwd myname@pcname:~$ cd ../.. myname@pcname:/$ pwd
Just say you want to go up a few parents and then down into other sub-directories, you can do it in one go like this. First,
myname@pcname:/$ cd myname@pcname:~$ pwd myname@pcname:~$ cd ../../usr/lib myname@pcname:/usr/lib$ pwd
We saw the simple
myname@pcname:/usr/lib$ cd /home/myname/Documents myname@pcname:~/Documents$ pwd /home/myname/Documents
In the same way we can use the
myname@pcname:~/Documents$ cd - /usr/lib myname@pcname:~/usr/lib$ cd - home/myname/Documents myname@pcname:~/Documents$ cd ~/Downloads myname@pcname:~/Downloads$ pwd home/myname/Downloads
So it simply switches back and forth between your present and last folder. Big deal! Believe it or not, you’ll get to like it!
So these are what we call paths (pathways) in your file system. All the packages that you’ll download and install in the future use the same basic file and directory tree path structure. Simple, powerful and effective.
We’re not going too in depth here, just enough to get your first couple of projects up and running. Further down the tracks, we’ll give you a few more tricks.
Who said folders? Tut tut,tut. We call them directories, you know that now!
As you build your dev experience, you’ll probably create a dedicated directory for all your development work. For reasons of safety, and just for practice, you’re going to create a temporary one right now in the
From now on, we won’t display the whole command prompt line, just the actual commands. Now let’s
mkdir /tmp/learnbash cd /tmp/learnbash
Note how we start with
Now, all in one go, let's to create a few directories dedicated to your well-being, and after we hit Enter, we want to list them using the
mkdir learnapple learnbanana learncoconut learndate ls learnapple learnbanana learncoconut learndate
The four directories are side by side, and not nested because we left a space between each of the four parameters/arguments.
Now using the
cd learnapple touch elderberry.js fig.css grapefruit.html ls
Let’s cd back, and then straight into learnbanana and make a couple of files in there too:
cd ../learnbanana touch honeydew.ts iboga.sol
If you need to remove i.e. delete a file, you can use
cd .. rmdir learnapple learnbanana learncoconut learndate
So what happened?
rmdir: failed to remove 'learnapple': Directory not empty rmdir: failed to remove 'learnbanana': Directory not empty
Like we said, we first need to remove the files from the directories.
There is an aggressive option for removing directories and the files they contain. If you choose to, use it with extreme caution. You might be used to having a Trash folder, where if you delete something by accident, you can retrieve it. When we’re working in our machine’s file system like this, there is no Trash folder!! Once you remove your files and folders, they are gone, gone for good.
The command we’re speaking of is
cd learnbanana rm honeydew.ts iboga.sol cd .. rmdir learnbanana
There is a lot more we want to show you, but this should be more than enough to get your files and directories set up for your NFT project. We have purposely left one more directory in there with some files, as we’re going to need it in our last steps.
Last, but certainly not least…..we have a command called
As a developer, you can expect to do more than just modify a few files. You will be using code instructions that require you to perform tasks as the administrator of your machine. You’ll be searching for these online. Until now, we have been using the
Your operating system is already set up to be as safe from attack as possible. Root can change up your whole system and actually open it up to all sorts of vulnerabilities around network services and firewalls, etc. It can let you do most anything. Sometimes you will get advice to download software and change permissions by using a super user command, but it could potentially open a hole where malware can get into your system. And that one command you use could end up triggering a chain reaction of many more. Remember, we’re only human, and also prone to making mistakes, so please be careful. Use good judgement when you find content online. If someone is asking you to use the root account, or the
Each time you use a
myname@pcname:/tmp/learnbash$ cat /etc/shadow cat: /etc/shadow: Permission denied myname@pcname:/tmp/learnbash$ sudo cat /etc/shadow [sudo] password for myname: <password here>
So it gave you a list of passwords and.....stuff. Great. Now you’re going to type and run the
Just to give you some more context. If you are asked to use
Let’s see sudo in action once more before we start to tidy up. If you’re using Ubuntu, then you have its standard repositories and you can follow along, otherwise just enjoy this as a little guide.
Type in the following, provide your password, press Enter and wait a few moments while the
sudo apt install tree
When your command line prompt shows up again, you’re good to go!
tree
Wait. Is
When we use the sudo command without any switch options (the
Also new to us is our command,
We hope we’ve helped you to think critically about using online resources that suggest using these tools. Use your super powers like a super dev would!
Now let’s clean out the last of our directories and files with the elusive, and most dangerous...
cd /tmp/learnbash rmdir learnapple rmdir: failed to remove 'learnapple': Directory not empty rm -r learnapple ls <directory and files should be gone now> cd .. rmdir learnbash cd <back home>
You should be back where you started with a clean slate and hopefully a few more skills to get you building. In the same way you opened your terminal window with
We hope you enjoyed that and look forward to seeing you on your journey as a
Some basic commands for CLI:
And a more extensive list of CLI commands