Learn Basic Linux Commands pt.1
Tags: linux, bash
Published: 2023-02-27 11:48:14

Linux is a family of open-source Unix operating systems based on the Linux Kernel. They include Ubuntu, Debian, Fedora, Red Hat etc. As developers, we have used linux commands when we interact with the terminal. It might be by creating files and directories, deleting them, moving from one folder to another, checking list of files and folders in a particular folder etc. This implies that linux commands plays very important roles in the lives of developers. Without further ado, let's check out some of the common linux commands and their uses.


**ls**
This is used to list the files and directories in a the current folder. You can use several options on this ls command; ls -a (ls with the option a) is used to list all files (including hidden files) in the current working directory. The hidden files starts with a .

```
ls -a
```
Another option is the l flag. The l flag is used to show more details about each of the files and directories(such as the permissions) in the current folder. 

```
ls -l
```
The information displayed by the ls -l flag is shown below

![List files and directories with l flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmwxlsw46e7g6umvkpq3.png)

There are other options such as h, s, S, t, r, R etc. These options can also be combined together, e.g., ls -al list all the files with their permissions and other info. 


**pwd**
This command is used to show the path of the current working directory.

```
pwd
```
The sample output is:

![Print working directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7tqf16wrgn7y6t2c1a3.png)


**cd**
cd is used to change the path from the current working directory to another directory specified. If no argument is specified, the directory's path is changed to the root directory. To change path to a specific directory, specify the path(absolute or relative) of the directory you are moving to.
From the example below, the current working directory is sample. To move to another directory inside sample e.g., another, we write:

```
cd another
```
The output is shown below. 

![Change directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjoiyiasaphg2l1g6tcq.png)
To move back one step to parent directory use the command

```
cd ..
```


**touch**
The touch command is used to create a file if it does not exist. If it already exist, the touch command updates the file access and modification timestamps to the current date.
We will create a file file1.txt in the sample folder. Let's first check if the file exists in sample folder.

![List file in sample folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtnq7n8plrugzfgng6py.png)
From the above we can see the files inside sample folder along with their timestamps. Let's create the file1.txt

```
touch file1.txt
```
Let's list the files and folders in sample.

![List files in sample after creating file1.txt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8y5q85idbssk1ubhg8h.png)
From the image above, we can see that file1.txt is now present, and we can also see its timestamp. Let's try creating file1.txt again and observe its timestamp.

![Difference in timestamps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1e2cljohrx82zd5o8fg.png)
From the above, we can see the difference in the two timestamps of file1.txt before and after the touch command.


**mkdir**
This command is used to create a directory if they do not exist. If the directory exists, an error message mkdir: <dirname>: File exists will be displayed.

let's create a directory called test.

![Create a new directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dj5k5uggxzcm5hczc9zu.png)

From the above, we can see the directory was successfully created by using ls command to check. Let's see what happens if we try creating the test directory again.


![Try create an existing directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hr4i5x84hp8o1s05dgo3.png)

The output above shows that an already created directory in a folder cannot be created again with the mkdir command.


**rm**
The rm command is used to remove a file or directory. to remove a file, we specify the command with the name of the file.

![Remove file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qngjws3ko72vawoni2ta.png)

From the above, we can see that file1.txt no longer exists after executing the rm file1.txt.

To remove a folder, we specify the rm with a r flag. The r flag means remove recursive, and this removes all files, directories, and subdirectories contained in the specified directory.

![Remove directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aob6j458avf9wjexi2x5.png)
The output above shows that the folder has been removed successfully.


**cp**

The cp command is used to copy file(s) from one location to another. The original file copied remains unaffected in its location. To copy file.txt to another folder, say another2

```
cp file.txt another2
```

![Copy file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ev1qujmuei4gxhc17jl.png)
From the above, we can see that the file was copied to another2 while the original file remain unaffected.

**mv**

The mv command is used to move a file or directory from one place to another or to rename it. In this case, the moved file will no longer exists in its original location. To move a file from a particular folder to the parent of that folder, we use

```
mv file2.txt ../
```

![Move file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lft7wxjmsyhv1dje1int.png)
From the above, we can see that file2.txt was moved from another2 to its parent folder sample.
N.B.: ../ means move up to parent folder

**cat**

It reads data from a file and gives the content as output.

![Display content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5dlvnrtfe8kozskiryr.png)


**echo**

It is used to display a line of string/text that is passed as the arguments.

![echo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lnam4tpmj5hdwrgepdgq.png)

**output redirection operator > and >>**
both > and >> are used to redirect output to a file. The > is used to overwrite an already existing file or to create a new file (if the file does not exist).

![> output redirect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36expnuyj6ozhtwzfssn.png)

The >> symbol is used to create new file or append to the already existing file.

![>> output redirect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8mtyf5rgsx8v062jc7s.png)