# Essential Linux Commands

Today we are going to discuss the Basic Linux Commands

### To view what's written in a file.

The `cat` command is used in Linux to concatenate and display the contents of one or more files. Here are some examples of how you can use the `cat` command:

1. Display the contents of a single file:
    
2. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689582810809/a05d8e8e-97fa-4f3b-9d26-d5be80292e97.png align="left")
    
    This command will output the entire contents of the `filr.txt`file to the terminal.
    
3. Display the contents of multiple files:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689582941660/fa94bfe0-d5dc-4986-a1ed-b2e24e96ca08.png align="center")
    
    This command will concatenate and display the contents of `file.txt`, `colors.txt`, and `fruits.txt` in the order specified.
    
4. Concatenate files and redirect the output to a new file:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583037816/b756af20-b16c-4f7e-86ca-4cee40bcbe44.png align="center")
    
    This command will combine the contents of `file.txt` and `colors.txt`and write them into `newfile`. If `newfile` does not exist, it will be created. If it already exists, its contents will be overwritten.
    
5. Append the contents of one file to another:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583119021/1bdfe827-5db7-40fd-8bb4-76af057c6939.png align="left")
    
    This command will append the contents of `file1` to the end of `file2`. If `file2` does not exist, it will be created.
    
6. Number the lines of a file while displaying its contents:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583183656/ef039848-14a7-4611-a20e-7d0c326506d6.png align="left")
    
    The `-n` option adds line numbers to each line of the file, making it easier to reference specific lines.
    
7. Display the contents of a file with line breaks replaced by empty lines:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583280250/09c989cc-f2ad-4277-a194-eedf5e55239b.png align="left")
    
    The `-s` option squeezes multiple consecutive empty lines into a single empty line, making the output more compact.
    

These are just a few examples of the `cat` command usage. The `cat` command has many other options and functionalities, which you can explore further by referring to its manual page using the command `man cat`.

### To change the access permissions of files.

To change the access permissions of files in Linux, you can use the `chmod` command. The `chmod` command allows you to modify the read, write, and execute permissions for the owner, group, and others. Here are some common examples:

Change the permissions using the octal notation:

```bash
chmod <octal_permissions> <file_name>
```

In this command, `<octal_permissions>` represents the desired permissions in octal form (e.g., 755, 644) and `<file_name>` is the name of the file you want to modify.

The octal notation assigns permissions based on a three-digit number, with each digit representing the permissions for the owner, group, and others, respectively. The digits can be calculated by adding the values of the desired permissions:

* 4: Read permission
    
* 2: Write permission
    
* 1: Execute permission
    

For example, to give read, write, and execute permissions to the owner, group and others, you would use `chmod 777 file_name`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583385528/76bd9bdc-6e98-4ba0-9b32-7db275d7cffd.png align="center")

Change permissions using symbolic notation:

```bash
chmod <permissions> <file_name>
```

In this case, `<permissions>` represents the desired permissions using symbolic notation. The symbolic notation allows you to add or remove permissions based on the existing permissions.

Some symbols used in symbolic notation are:

* `+`: Adds the specified permissions
    
* `-`: Removes the specified permissions
    
* `r`: Read permission
    
* `w`: Write permission
    
* `x`: Execute permission
    

For example, to give execute permissions to the owner, you would use `chmod u+x file_name` .

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583459620/180d135d-fad4-41c6-a1c5-904e60f5ce5b.png align="center")

Note that changing file permissions requires appropriate privileges. If you are not the owner of the file or do not have sufficient permissions, you may need to use `sudo` or contact the system administrator for assistance.

### To check which commands you have run till now.

In Linux, you can check the history of commands you have run in a terminal session using the `history` command. By default, the `history` command will display a list of previously executed commands along with their line numbers. Here's how you can use it:

1. Open your terminal.
    
2. Type the following command:
    
    ```bash
    history
    ```
    
3. Press Enter.
    
    This will display a numbered list of the commands you have executed in the current terminal session, with the most recent commands appearing at the bottom.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583686900/188f3abf-a414-44ac-88bb-93fd606d3261.png align="left")
    
4. If you want to search for a specific command in your command history, you can pipe the output of `history` to the `grep` command. For example, to search for commands containing the word "cat", you can use:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583732112/a60384a2-159a-4891-a985-2f15bedfc1da.png align="left")
    
    This will display a filtered list of commands that include the word "cat".
    

Please note that the command history is typically specific to each terminal session. If you open a new terminal or start a new session, the command history will be different. Additionally, the command history is usually limited to a certain number of entries, and older commands may eventually be truncated or removed from the history.

### To remove a directory/ Folder.

To remove a directory (also known as a folder) in Linux, you can use the `rmdir` or `rm` command. Here's how you can use each of these commands:

1. `rmdir` command: If the directory you want to remove is empty, you can use the `rmdir` command. It is specifically designed to remove empty directories. Here's the syntax:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583856851/cee88e08-c0c7-4894-9908-d4c36c1e354f.png align="center")
    
    Replace `directory_name` with the name of the directory you want to remove. For example, to remove a directory named "my\_folder", you would use `rmdir my_folder`.
    
    If the directory is not empty, the `rmdir` command will not work and will display an error message. In that case, you can use the `rm` command with additional options.
    
2. `rm` command: The `rm` command is more versatile and can be used to remove directories and their contents, including non-empty directories. Use the following syntax:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689583942405/397189e6-2421-49c0-b85f-558d44b1841e.png align="center")

Options:

* `-r` or `-R`: Enables recursive removal. This option allows you to remove directories and their contents, including subdirectories and files within them. It is commonly used when removing directories.
    
* `-f`: Forces the removal of files and directories without prompting for confirmation. It suppresses error messages when files or directories are not found.
    
* `-i`: Prompts for confirmation before removing each file. It asks for user confirmation individually for every file or directory specified.
    
* `-v`: Enables verbose mode, displaying detailed information about the removal process.
    

Example usages:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689584163332/1e564110-4489-463f-a22f-5ebb97f1fe29.png align="left")

This command forcefully removes a file named `filename.txt`, suppressing any error messages if the file does not exist.

It's important to exercise caution when using the `rm` command, especially with the `-r` and `-f` options, as it permanently deletes files and directories without the possibility of recovery. Always double-check the files and directories you are removing to prevent unintended data loss.

### To create a fruits.txt file and to view the content

To create a `fruits.txt` file and view its contents in Linux, you can use the `touch` command to create the file and a text editor such as `cat`, `less`, or `nano` to view its contents. Here's how you can do it:

1. Open your terminal.
    
2. Use the `touch` command to create the `fruits.txt` file:
    
    ```bash
    touch fruits.txt
    ```
    
3. Once the file is created, you can use a text editor to view and modify its contents. Here are a few examples:
    

Using `cat`:

```bash
ubuntu@ip-172-31-18-189:~$ cat fruits.txt
Apple
Banana
mango
Lichi
Grapes
```

This command will display the entire contents of `fruits.txt` in the terminal.

### Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

To add the specified content to a file named `devops.txt`, with each item on a new line, you can use the `echo` command in combination with the output redirection operator (`>` or `>>`). Here's how you can do it:

1. Open your terminal.
    
2. Run the following command to add the content to `devops.txt`:
    
    ```bash
    echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt
    ```
    
    The `-e` option is used with `echo` to enable interpretation of backslash escapes. Each fruit is separated by `\n`, which represents a new line.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689584360493/8b803d57-662f-4b2e-96ea-c06aecdd0ce4.png align="center")
    
    This will display the contents of `devops.txt` in the terminal, with each fruit on a separate line.
    

After following these steps, you will have added the specified content to the `devops.txt` file, with each fruit item on a new line.

### To Show only top three fruits from the file.

If you want to display the top three fruits from a file in Linux, you can achieve that using the `head` command with the `-n` option. Here's an example assuming your file is named `devops.txt`:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689584437602/2e5fcf51-4612-4a6e-8107-06be99ffbae6.png align="left")

This command will display the first three lines (top three fruits) from the `devops.txt` file.

The `head` command is used to output the beginning of a file, and the `-n` option specifies the number of lines to display. In this case, we set it to 3 to show only the top three fruits.

Note that this method assumes that the top three fruits are already in the correct order in the file. If you need the top three fruits in a specific order, you may need to sort the file before using the `head` command.

### To Show only bottom three fruits from the file.

To display only the bottom three fruits from a file in Linux using the `tail` command, you can use the `-n` option with a value of 3. Here's an example assuming your file is named `devops.txt`:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689584482903/f61316fd-8f1a-450a-b5f7-f432122848d1.png align="left")

This command will display the last three lines (bottom three fruits) from the `devops.txt` file.

The `tail` command is used to output the end of a file, and the `-n` option specifies the number of lines to display. By setting it to 3, we ensure that only the bottom three lines (fruits) are shown.

Make sure that the file contains at least three lines (fruits) for the command to work as expected. If the file has fewer than three lines, `tail` will output all the lines in the file.

Using `tail -n 3` is a straightforward way to display the bottom three fruits without the need for sorting or additional commands.

### To create another file colors.txt and to view the content.

To create a file named `colors.txt` and view its contents in Linux, you can use the `touch` command to create the file and a text editor such as `cat` to view the content. Here's how you can do it:

1. Open your terminal.
    
2. Use the `touch` command to create the `colors.txt` file:
    
    ```bash
    touch colors.txt
    ```
    

Once the file is created, you can use a text editor to view and modify its contents. Here are a few examples:

* Using `cat`:
    
    ```bash
    cat colors.txt
    ```
    
    This command will display the entire contents of `Colors.txt` in the terminal.
    

### Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

To add the specified content to a file named `colors.txt` using the Vim text editor, you can follow these steps:

1. Open your terminal.
    
2. Type the following command to open the `colors.txt` file in Vim:
    
    ```bash
    vim colors.txt
    ```
    
3. Once Vim opens, press the `i` key to enter insert mode. This allows you to start editing the file.
    
4. Copy and paste the content you want to add to `colors.txt`. In this case, the content is:
    
    ```bash
    Red
    Pink
    White
    Black
    Blue
    Orange
    Purple
    Grey
    ```
    
5. Press the `Esc` key to exit insert mode.
    
6. Type `:wq` and press Enter to save the changes and exit Vim.
    

Now you have added the specified content to the `colors.txt` file using the Vim text editor. You can view the contents of the file using the `cat` command or open it again with Vim to verify the changes.

### To find the difference between fruits.txt and colors.txt file.

To find the differences between two files, such as `fruits.txt` and `colors.txt`, in Linux, you can use the `diff` command. The `diff` command compares two files line by line and displays the differences between them. Here's how you can use it:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689584614104/d9ff2972-0117-46bb-aa37-cf1d0eccd0b5.png align="left")

The command above will compare the contents of `fruits.txt` and `colors.txt` and display the differences between the two files. The output will indicate which lines are unique to each file or if there are any differences within matching lines.

If you want to ignore the case sensitivity during the comparison, you can use the `-i` or `--ignore-case` option:

```bash
diff -i fruits.txt colors.txt
```

This will perform a case-insensitive comparison between the two files.

Note that the `diff` command may produce a large output if there are many differences between the files. In such cases, you may want to consider redirecting the output to a file for easier analysis:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689584702448/646cbee0-e6dd-4f49-a2f3-638b59a56c51.png align="left")

This command will save the differences to a file named `differences.txt` instead of displaying them on the terminal. You can then open `differences.txt` to view the results using a text editor or any other suitable tool.

Remember, practice makes perfect. As you gain more experience, you'll uncover additional Linux commands and expand your proficiency. Happy exploring!
