Command-line
Tags: linux, programming, cybersecurity, beginners
Published: 2023-02-18 02:52:14


> Note: This blog will only cover the command line that comes with Mac and Linux aka Bash. Windows users can also download Bash. The tutorial can be found [here](https://itsfoss.com/install-bash-on-windows/)

## Introduction

The Command Line is a text interface for the computer's operating system. It is used to manipulate the computer's file system. It is used to create, edit, delete, and more. On Mac and Linux, we access the command line through something called Bash. Windows on the other hand come with a different built-in command line. 

## Usage
From the Command Line, we can navigate through files and folders, just as we would with Finder for Mac or File explorer for Windows. The only difference is that the Command line is fully text-based. 

The advantage of using the command line is that it can run programs, write scripts to automate common tasks, and combine simple commands to handle difficult tasks. All of these traits come together to make it an important programming tool. 

## Structure 
When using the command line, folders as referred to as directories. Files and directories on our computer are organized into a filesystem. A filesystem organizes a computer's files and directories into a tree structure: 



1. The first directory in the filesystem is the root directory. It is the parent of all other directories and files in the filesystem.

2. Each parent directory can contain more child directories and files. 

3. Each directory can contain more files and child directories. The parent-child relationship continues as long as directories and files are nested. 
![terminal strcuture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yspnzrdygdps62vfb2mw.png)

Macs Finder and Windows File Explorer represent the file system as trees as well. 
![Finder Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zc8thx7ew0qj6roob7q4.png)

## Commands
In the terminal, the first thing we see is the $ sign. This is called a shell prompt. It appears when the terminal is ready to accept a command. 

There are several commands used in Bash(command line). Some of them are: 

- pwd
- ls
- cd 
- mkdir
- touch
- cat
- cp
- mv
- rm & many more

## ls
A command is a directive to the computer to perform a specific task. When we type ls, the command line looks at the directory we are in, and "lists" all the files and directories inside of it. Make sure to type l as n "List" and not the number 1. 
![ls](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42ez6vc3xcpyhz7fimc2.png)

## pwd
pwd stands for "print working directory". It outputs the name of the directory we are currently in, called the working directory. As an example, if we are in the users downloads directory, typing pwd will out the current directory, which is downloads. Together with ls, the pwd command is useful to show where we are in the filesystem. 
![pwd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o32vce5lt4uii113hnir.png)

## cd 
cd stands for "change directory". Just as we would click on a folder in Finder or Windows File explorer, cd switches us into the directory we specify. cd changes the working directory. 
![cd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ykb2yvd9x4fqaveqxu1.png)
when a directory or a file is passed into a command, it is called an argument. in this case, Users would be the argument, because that is what's being passed to the cd. 

we can also use double dot (..) after cd to move up one directory. 
![cd..](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3ibglh23xb2y5n068uc.png)


## mkdir
When creating folders/directories, we use mkdir command. The mkdir command stands for "make directory". It takes in a directory name as an argument and then creates a new directory in the current working directory. 
![mkdir](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejyg86fh8ysccccvavhz.png)

## touch
The touch command created a new file inside the working directory. It takes in a filename as an argument and then creates an empty file with that name in the current working directory. 
![touch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8cfftjdzowtlwv1k54m.png)

## cat
The cat command outputs the contents of a specified file. 
![cat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mewrbdznv8du5sk3ld6l.png)

This is a useful command for peeking at files without opening them and confirming the result of other commands that change the contents of files. We can also get output using paths to a specified file. To output the contents of blogs.txt within the home directory, we can run: 
![cat2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5v9yjjwbm1oc0dsgnde.png)
This will output the contents of blogs.txt within the Desktop directory. 

## cp
The cp command copies files or directories. Below, we copy blogs.txt into the blogs directory. We can see it has been successfully copied when we ls into the blogs directory. 
![cp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ajm3kbk40c26teu9wo2.png)

We can also use cp to copy multiple files into a directory. We can use cp with a list of source fields as the first argument, and the destination directory as the last argument. Below, we copied the files blog.txt and blogs2.txt into the blogs directory. 
![cp2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yh5dvcehr484qcw6tvt.png)

## mv
The mv command is very similar to cp in its usage. The main difference is that mv moves a file without making a copy. We can use mv with the source file as the first argument and the destination directory as the second argument. Below, we moved blogs.txt into the blogs directory. 
![mv](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frf26pvjrbqjyc33j9xo.png)

mv can also be used to rename a file or directory. 
![mv2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ttvthna56gb4vkc2na1.png)

## rm
The rm command deletes files and directories. Keep in mind when using rm. It deletes files and directories permanently. There is not an undelete command. Once we delete a file or directory with rm, it's forever gone. 

> The -r (recursive) option is used to delete a directory and all its child directories followed by the directory name. When we delete a file, however, we can just use rm followed by the file name.

This illustrates how to delete a file. 
![rm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j67lt7o7bvaobv3cisd5.png)

This illustrates how to delete a directory with files in it. 
![rm2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ew52ne15h7olba5w1qcc.png)


These are a few of the many commands used in the command line. All the commands can be found [here](https://ss64.com/bash/).

## Other Helper Commands
There are some helper commands that are helpful in the command line. 

- **tab** can be used to autocomplete our command. However, the file or directory should exist in the first place. 

- The **up** and **down** arrows can be used to cycle through previous commands. The up arrow will take us through the most recent commands, and the down arrow will take us back through the recent one. 

- **clear** is used to clear the terminal. This can come in handy when the terminal is full of previous commands and outputs. It really doesn't change or undo our previous commands. It just clears it from the view. 

## Conclusion 
The command line is an essential tool for developing projects. We can use the command line to manage all of our project files. We can move, delete, and reorganize our projects. We can have many things running at once in our command line environment. As you become a more and more advanced developer, you will find it necessary to configure the command line environment to your convenience. 

Thank you! 
[Jonny](linkedin.com/in/jonnytilahun)