LEARN COMMAND LINE (Tutorial)
Tags: tutorial, linux, bash, codenewbie
Published: 2020-06-18 15:49:29



Terminal might be the scariest application on your computer right now, but the end of this course, you'll better understand:
• why the terminal is important,
• what you can do with it,
• how to avoid messy situations (hint: you'll be fine!),
• how to run useful commands for everyday situations and developer careers!
It may have a hacker reputation, but that’s only because fictional characters in CIA movies use it to do wild and fake stuff.
Yes, Don't let Terminal's black background or ominous vibe make you nervous! We'll be taking it easy in there and will even show you how to change the terminal's design to make it less serious.
✨Let's explore some background on the terminal🏃‍♂️🏃‍♂️🏃‍♂️....

#🔰What is the terminal?

Terminal is an application that lets you interact with your computer.You usually give instructions to your computer through clicking around in applications or typing keys to make
things happen.
Terminal lets you do many of the same things; it’s just
more direct. You can give clear, structured orders to your computer using the terminal.
You just have to be patient and willing to try them!

###🔰What can I do in Terminal?
Here are several classic ways you already interact with your computer for which you can also use Terminal.
Menus to interact with files. You're already familiar with drop-down menus like this if you're on Mac, Windows, or other types of machines. In applications such as Microsoft Word, you can have a "Save" option, "Open," etc:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/27ye0sl3v7mqqwbonyef.png) When you edit text, image, or other files, you often have a visual menu like the screenshot above that let you do stuff to the file. You can update the file, duplicate it, see information about it, and more. You know what's up!
Good news: you can use Terminal to perform these same operations.

##Interfaces to browse files
If you're on Mac, you may be familiar with Finder. On Windows, you can find files and programs you want using the Start menu.
Either way, you're already at ease finding what you need on your computer by searching and clicking for programs or content:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uvuz8bua8dv1pgyilcox.png) You can also use Terminal to create, open, and update files,which will save you hours of time if you ever want to become a web developer or designer.

##🔰Open The Terminal
How you open Terminal depends on your operating system.
If you're on a Mac, open your Applications folder and find the Utilities folder inside. You can open Terminal from here.
Note: consider right-clicking terminal in your dock and selecting “Keep in dock” for easier access!
If you're on Windows, you'll need to install Cygwin (Follow) or Cmder.

##🔰"Where am I?" in my computer
When you first launch Terminal, you'll likely have an empty window with not much going on. My Terminal window looks like this, but yours is probably a different color or set of text on the left (which is fine!)
I see a simple dollar sign and a blinking cursor, but you might see the name of your computer too or other symbols:
💁💁💁.. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oraxqkdwrmer8cstl0iv.png) Rest assured that nothing is currently happening. Imagine that your computer is just sitting there, waiting for instructions!

##🔰Command line
Let's visually break down what you see upon opening the terminal. The line is called the command line:![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oraxqkdwrmer8cstl0iv.png) First line when opening Terminal
 On the left-hand side of your command line within Terminal, you have what's called a prompt. You might see $,%,>, or other symbols, or you might see the name of your computer. This will vary per computer and per operating system!

###What is a terminal in general?
Imagine you're that you're using a microwave. You press "3," "0," and "Start" on a keypad in order to tell the microwave to heat something for 30 seconds. The command line on your computer is a similar communication interface; it's a portal in which you'll use your keyboard to type in a language the computer can read, and the computer will act out your instructions.
The prompt is the computer's way of saying "Hello, give me instructions!" 🖥💬 That's why, on the right-hand side, you have a cursor and space to type commands. 
 Press "Enter" several times. You'll get the same empty line over and over! Since you're not giving the computer any actual instructions, it keeps showing you a new command line each time, waiting for your input.

##🔰Run your first command
First, let's talk about location.
Let's say I'm at Disney World. The things I can do depend on where I am in the park. For example, if I'm in Epcot, I can't ride the rides in Magic Kingdom. It's important to know where I am so I can plan my visit. That's why those red "You are here!" signs are so helpful. They help you figure out your location and therefore what's just around you.
The same is true on your computer. Depending on where you are in your system, you'll be able to do different things with Terminal. For example, I could give instructions to Terminal to modify all image files in my "Christmas party photos" folder. This means I need to be in my "Christmas party photos" folder within Terminal.
In order to see where you are at any time within your computer, you can type the command  pwd  .   
pwd  stands for print working directory. Type it into your command line now:

>pwd 

You can do this anytime, and it's very safe!
"Directory" in the world of computers just means "folder." By running this command, we're asking our computer which folder we're in. 
It's like Russian nesting dolls! My computer just told me exactly where I am within my computer's folders. Location -- where you are in your computer -- matters for almost everything you'll do in Terminal. It's the equivalent of a "You are here" sign so that you're never lost in your machine. You'll see why this is important as you move farther in the course.
Congrats on running your first command in Terminal! 🎉 That wasn't so scary, right? The rest of the commands we'll run in this course will be more action-oriented but equally safe.

##🔰Navigate your system
I'll be philosophical for a second. Two things matter in life:
    • Where you are 🌎
    • Where you're going ✈️
The same is true in Terminal! While you're exploring your computer using Terminal, you'll want to be able to know where you are in the system and where you need to go to perform the operations you want. 
Two commands that will be helpful for navigating through your system: ls and cd. Both are low-pressure and straightforward, even for beginners.

##🔰Finding destinations
In your computer, you can go pretty much anywhere you want. First, though, you must know the destinations available to you.
On the command line, type the following command (ls , which is short for list), and press the Enter key on your keyboard.

>ls

When I type ls and press Enter, here's what I see:![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7iltky3jueawlftus05l.png)
ls is one of the commands you'll run the most, even as a professional developer. Memorize it!
     Some useful ls command options:- 

>ls -a | list all files including hidden file starting with'.' 
> ls -l   | list with the long format
> ls -la  | list long format including hidden files   

##🔰Going to destinations
Now that you've revealed possible directories (folders) available to you, you can move around them using a different command:cd which is short for "change directory," plus the name of the folder where you want to move.
For example, let's say I want to change locations in my system and move into the "Music" folder. This was listed as one of the options in the output from our ls command. I'd type:

>cd Music

Here's what my Terminal looks like after running cd Music:![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q4679w9j5md7z2o87xgb.png)

##🔰Stop a hanging command
Although you'd have to work very hard to actually harm your computer using Terminal, sometimes you can run commands that are more tricky for your computer than you expect. Maybe you make a typo that means the command isn't running as it should. Maybe you've changed your mind and don't want a command to finish running. Your handy "Stop!" key combination is 
>ctrl + c

##🔰Create directory
It's now time to create a folder within your system.
Yes, you could just do this via Finder or whatever tool you use to browse and create files now. However, there are advantages to doing this via the terminal, especially if you want to get more into programming!
How does one go about creating a folder in Terminal? Time for a new command!
Use the command  mkdir  to create a directory.  mkdir  is short for "make directory." Specify the name of the directory (folder) you want to create just after it. If I wanted to create a folder called  new-folder , I would run:
>mkdir new-folder

Simply making a folder using mkdir doesn't move you into that folder. You need to run cd plus the folder name to do that!   

##🔰Folder names with multiple words
If the folder should be called long John. You need to make special considerations for folder names that contain spaces.
Notice that, by running  
>mkdir  Long john

two folders (1. "Long," 2. "John") are created instead of one. This is because my computer interprets the space between the two words as a delineation between two folders names, not just a space in the middle of one name. Clearly, we need a different way to indicate Long John is all one file name; not two separate ones.

To create or reference a folder with multiple words in it, you have three options:
###✔ use quotation marks
>mkdir "Long john"

◦ This says the folder name should include all characters inside the quotation marks escape the special character (the character you're computer is interpreting in a special way).

###✔ Escape the special character (the character you're computer is interpreting in a special way) using a backslash
>mkdir Long\John

◦ This says that the special meaning of the space between "long" and "John" should be ignored. We want a space to just be a literal space, not for Terminal to interpret it as a way to separate multiple folder names. 
◦ In programming, you'll often use backslashes to escape special characters!

###✔ Use hyphens instead of spaces to create a different folder name entirely 
>mkdir Long-John

##🔰Create files via the command line
You can create all types of files from the command line itself. This is much faster than creating the file individually via different applications like Microsoft Excel or a word 
processor and running "Save As.". If you use the command cd to move into a directory, you can then use a command called  touch  to create a file.

Touch is a fairly creepy sounding command, but here's what it does:
    • tells your system to look for a certain file
    • If the file not already exist, your system will create that file for you.

The command will look like this:        
>touch file-name.extension

It's as simple as that! Now if you look back in Finder, you will be able to see that this file has been created inside your folder. It is currently empty.

##🔰ADDING TEXT TO FILE
Running  echo  plus text in quotation marks, followed by a  >  and a filename, will add the text to the file you specify (and create the file if it doesn't already exist)! ✨
>echo "content">file-name.extension

##🔰Display the Content of a File: 
We can display the content of a file using the cat command. To use it type cat file_name.
>cat file_name

##🔰TO LOCATE A WORD 
Remember, you have to specify:
    • the text for which you're looking
    • the file(s) in which you want to look
>grep word filename.extension

grep  will return the lines that contain the content you seek.grep  can be customized depending on the type of project on which you're working.

##🔰To Move content
Let's say you've accidentally created a file in the wrong place. , like putting your  research spreadsheet in your “long john” folder. 😖
If you use a visual interface like Finder (or another visual interface), you would have to click and drag this file into its correct location. In Terminal, you don't have a visual interface, so you'll have to know the  mv  command to do this!
mv  , of course stands for move. The  mv  command requires several pieces of information.
    • the original file we want to move.
    • the new destination for the original file.
My first piece of information is therefore research-findings.csv , and my second piece of information is therefore the file path to the Research folder.

###➡Absolute and relative file paths
I could type this in two different ways. Let's explore the difference between absolute and relative file paths.

Option 1 (absolute path):
>mv research-findings.csv  ~/Desktop/Research

Option 2 (relative path):
>mv research-findings.csv  ../Research

Option 1 contains the absolute path towards the Research folder. Each folder is listed in the path from top to bottom.
Option 2 is a relative path . It is the path relative to where we are now.  ..  indicates "move up one level" (to "Desktop"), and from there, move into the Research folder.

###➡➡Rename content using mv
You can also rename files and folders using the mv command. To rename  research-findings.csv  to  final-report.csv , you could run:
>mv research-findings.csv   final-report.csv

Technically, you're "moving" the one file to another and renaming it in the process. 👍
You can also use mv with folders in order to move them! It's not just for files.

##🔰Copying files
Copying content works similarly to moving content. Let's start with copying a file because it's simpler.
The same arguments are true for  cp  (copy) as for  mv  (move):
    • The first argument is the original file we want to move.
    • The second argument is the new destination for the copy of the original file.

Scenario: you want to copy the report from your Research folder to a new folder called “office work". You don't want to move the files; just copy them to a different folder so you can have copies of your final papers in one centralized place.
From within "Desktop" folder, make a directory to contain office work.
mkdir office\work

you can either change directories (cd) into the Art history folder and copy the final paper from there, or just copy it without changing directories.

>cd Research
>cp report.text ../office\ work

or

>cp Research/report.txt office\ work

##🔰copying folders
Copying folders is more tricky. You will need to use something called an option. Options exist for many terminal commands. They are a way for you to tell the computer that a command should run in a specific, non-default way.
To copy folders, you'll use the recursive option. Recursive is similar to iteration, in that the command will run in small increments in order to achieve its final goal. This is not worth worrying about right now, though you can read more about [recursiveness](https://softwareengineering.stackexchange.com/questions/184596/what-does-recursive-mean-for-a-program-which-processes-files-and-directories) here.
For this example, we'll hop back to our code project. I want to duplicate my code folder in order to use the code I've written as a base for another project. The original directory is called code-project, so I'll name my copy  code-project-2.
To copy this folder from one place to another, you'll run:

>cp -R ~/Desktop/code-project  ~/Desktop/code-project-2

This copied my code-project folder (including its contents) into a new folder called  code-project-2. The  -R  is an option that specifies you want the command to run with recursion.
Now you can move and copy files and folders. Nice! 

##🔰Remove Files & Directories: 
To remove (or delete) a file, you use either the rm (remove) or unlink command.

The unlink command allows you to remove only a single file
>unlink filename

while with rm you can remove multiple files at once.
>rm filename

Be extra careful when removing files or directories, because once the file is deleted, it cannot be easily recovered.

##➡Clear Screen: 
Clear command is used to clear the terminal screen.
>clear

#🔰Conclusion
That was the last command you ran in this course! Here's a recap of everything you've learned:
    • pwd  stands for "print working directory." Terminal will tell you which folder you're currently in.
    • cd  stands for "change directory." Pass it the name of another folder, and the terminal will move you to that folder. 
    • ls  stands for "list." Running this command will show the contents of your current folder.
    • mkdir  stands for "make directory." Running this command, plus the name of your desired new folder, will create a new folder. 
    • touch  plus the name of your desired new file, will create a new file.
    • mv  stands for move. Running this command, plus the name of the content you want to move, plus the location where you want to move it, will move the content.
    • cp  stands for  copy. Running this command, plus the name of the content you want to copy, plus the location where you want to move it, will copy the content to another location.
    • Several shortcuts allow you to be more nimble on the command line, like ctrl + e, ctrl + a, option + click, etc.

🎉🎉🎉 Congratulations on taking your first steps on the command line. Whether you keep these tips in your back pocket, or go further with them as you evolve in a potential tech career, they'll serve you well. 💻