Beginner Guide to learn Coding (part 4)

beginner guide

beginner-guide-to-learn-coding-part-4

Welcome back, guys! This week we continue to next step to learn coding, part 4!

Step 4: A beginner guide to some command-line basics for practice

The Command Line (CL) can be scary at first glance. Some movies can use that CL feature as a cryptic black screen with incomprehensible text, numbers, and symbols scrolling by. It is usually associated with an evil hacker or genius techie sidekick.

The truth is that it does not take a genius to use or understand the command line. In fact, it allows us to perform many of the same tasks that we are comfortable doing via a computer mouse.

The main difference is that it primarily accepts input via the keyboard, which can speed up inputs significantly once you get the hang of it.

In this beginner guide, you can use the CL to browse through folders, list a folder’s contents, create new folders, copy and move files, delete files, execute programs, and much more. The window in which you can type commands on the Command Line is a terminal.

Let\’s start some basic navigation commands that will give you a feel for working on the command line.

Once you open your terminal, a typical first question is \”Where am I\”? We can use the pwd command (which stands for \”Print Working Directory\”) to figure that out. It gives our current location in the file system as output, which tells us which folder we are currently in.

 

How to Use the Command Line

If you’re on a Mac, open the Terminal app, which is essentially a Unix Command Line terminal.

If you’re running an operating system without a GUI (Graphical User Interface), like Linux or Unix, you should be at the Command Line by default when you start the computer. Or your Operating System does have a GUI, you’ll need to open the terminal manually.

At the prompt, type pwd and press <ENTER>. In Windows, type cd. The Command Line will print out the path to the folder that you’re currently in.

By default, the active folder when opening the Command Line is the logged-in user’s home directory. This is customizable in case you want the convenience of starting in a different location.

Now that we know what folder we’re in, we can use the ls command to list the contents of the current directory. The ls command stands for \”List\”.

Type ls and press <ENTER>. In Windows, type dir. The contents (files and subfolders) that reside in the current directory are display to the screen.

Create new folder

Next, we can create a new folder using the mkdir command, which stands for \”Make Directory\”. Below we create a new folder called \”testdir\”.

Type mkdir testdir and press <ENTER>. Then type ls and press <ENTER> (in Windows, type dir). You should see your new directory in the list.

To create multiple nested directories at once, use the -p flag to create a whole chain of directories like this: mkdir -p directory1/directory2/directory3 or in Windows, we use \’mkdir mynewfolder1 mynewfolder2 testfolder1\’

In CL, we can also browse through different directories in the file system. We can do this via the cd command, which stands for \”Change Directory\”.

First, type cd testdir and press <ENTER>. Then type pwd and press <ENTER> (in Windows, type cd). Note the output now shows that we are inside the \”testdir\” directory specified in the cd command. We went inside it!

Type cd .. and press <ENTER>. The .. tells the Command Line to browse backwards to the parent directory.

Then type pwd and press <ENTER> (in Windows, type cd). Note the output now shows that you are back in the original directory. We went backwards!

Create new files

Next in this beginner guide, we will learn how to create a new empty file in the current directory.

Type touch newfile1.txt and press <ENTER> (in Windows, type \”type nul > newfile1.txt\”). You can use the ls command (in Windows, use cd) to see that the new file was created in the current directory.

Now we’ll copy that file from one folder to another using the cp command.

Type cp newfile1.txt testdir and press <ENTER>. Now use the ls commands (in Windows, type dir) to see that the new file still exists in the current directory and was copied to the \”testdir\” directory.

We can also move files instead of copying using the mv command.

Type touch newfile2.txt and press <ENTER> to create a new file, (in Windows, type \”type nul > newfile1.txt\”)
Next, type mv newfile2.txt testdir and press <ENTER> to move the file into the \”testdir\” folder. (in Windows type, move newfile2.txt testdir)

The rename command can also be used to rename files.

To do that, type touch newfile3.txt and press <ENTER> to create a new file, (in Windows, type \”type nul > newfile1.txt\”). Then type mv newfile3.txt cheese.txt and press <ENTER> to update the file’s name, in Windows, use rename newfile3.txt cheese.txt). Use ls to confirm that the filed was renamed, (in Windows, use dir).

Remove files

Finally, we can delete files and folders using the rm command, (in Windows, use del)

Type rm cheese.txt and press <ENTER> to remove the file, (in Windows, use del). Use ls to confirm the file was removed, (in Windows, use dir)

That\’s the end of step 4! See you soon!

 

Leave a Comment

Your email address will not be published. Required fields are marked *