Linux Commands easier than ever before 💪
Tags: linux, cli, ubuntu
Published: 2022-12-22 18:21:02

The Linux command is a utility of the Linux operating system. When operating Linux, you need to use a **shell** – a program that gives you access to the operating system’s services. Most Linux distributions use a graphical user interface (GUI), making them beginner-friendly.

However, we recommend utilizing the command-line interface (CLI) because it’s quicker and offers more control. Tasks that require multiple steps on the GUI can be done in a matter of seconds by entering commands into the CLI

In this blog, you will learn all the commands that you should know to get comfortable with the terminal.

To start let’s first understand some basic terms:

- **Command Line Interface ( CLI )**: A CLI is a text-based user interface to run programs.
- **Shell**: A shell is a piece of software that interprets typed commands and runs them.
- **Terminal**: A terminal is software that a shell program runs inside of.

**What are commands?**

Commands are programs that are available on a system. When we run a command, the system takes some specific action. Most of the commands have a short name.

**General command syntax:** 

```jsx
command        option(s)      argument(s)
```

We don’t always need to have options and arguments.

The **option** tells the command how to operate.

ex: 

```jsx
ls -l

ls -a -l

ls -alh
```

Here -l is an option. We can have the option of single characters starting with a dash(-), and multiple single-character options can be written together as shown above in the third command. Options with more than one alphabets start with two dashes(—) and those options cannot be written along with other options.

You can use —help option to know details about some commands.

The **argument** is where we tell the command what to operate on.

ex: 

```jsx
ls -l ./Documents
```

Here ./Documents is an argument.

**File system in Linux:**


![Linux File System](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmoq8njv5awcl45q26di.png)

The above image shows what the file system looks like in Linux.

/home is the directory where normal user store files.

There are two ways to specify a path:

- Absolute path: An absolute path is defined as specifying the location of a file or directory from the root directory(/).
- Relative path: Relative path is defined as a path relative to the present working directory (pwd).


![General CLI notation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18pvahbbkk64xog1p0p5.png)

In the above image shivam is the username, ASUS is the hostname and ~/Desktop is the present working directory.

It is good to know the following directory to get a better understanding of how the commands are working.

- Root user’s home directory: `/root`
- User’s home directory: `/home`
- Common configuration files reside in: `/etc`
- Common programs and commands in: `/bin`, `/sbin`
- Shared libraries and modules: `/lib`
- Shared location for mounting other file systems: `/mnt`, `/media`
- Kernel and system information: `/dev`, `/proc`, and `/sys`.

**Some useful keyboard shortcuts:**

- **`Up and Down arrow keys`**: Using the up arrow key you can get previously written commands.
- **`Tab`:** It comes very handy while writing arguments in a command if there is some directory with a long name then you can press the tab after writing some of the starting portions of the command if that portion uniquely matches with some directory then the tab will autocomplete it for you.
- **`Tab-Tab`**: In case you remember only some initial characters of a command, then pressing the tab twice will give a list of commands starting with those characters.
- **`Ctrl-A`**: Move to the beginning of the line.
- **`Ctrl-E`**: Move to the end of the line.
- **`Ctrl-Shift-C`**: Copy text to the clipboard.
- **`Ctrl-Shift-V`**: Paste text from the clipboard.

Let’s start to look into commands now:

##COMMANDS:

*  **`man`**: This command is used to open the manual of any other command. 
    
    ex: on typing command `man ls`.
    
     
![man command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vka1x1u6rexbnt5fldfr.png)
    
* **`apropos`:** This command will help you to find the command you want to use. Let’s say you wanted to list everything present in the current directory, but you don’t remember the ls command for it, then using the `apropos list` you will get all the commands related to listing things.
    
    
![apropos command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9csvshldsb3egc4cehi.png)
    
* **`pwd`**: This command is used to know the present working directory.
    
    
![pwd command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/glknf0fhwh2vrjq7bilx.png)
    
* **`cd`:** Changes the shell working directory.
    
    To move into the folder which has space in its name. 
    
    For example: Let’s say our current working directory is `~/Desktop` and there is a folder with the name “folder name”, then in order to change the working directory to `~/Desktop/folder name/`, we will need to use the command like this: `cd folder\ name`. Here `\`  is used to ignore the space between the words. 
    
    
![cd command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihh8e9xaik9io0a0h46s.png)
    
* **`ls`**: Lists directory contents.
    We can use various useful options with the ls command.
    
 

1. `ls -l`: Here -l will tell the ls command to use the long listing format.
2. `ls -a`: Here -a will tell the ls command to show all the files including hidden, files that start with ‘.’ are hidden files. 
3. `ls -R`: Here -R will tell ls to show all the files recursively i.e. everything that is present in the directory will be printed in the output. If there is a folder inside the current working directory and it has some files inside it, then even those files will be printed as output in this case.
    
    
![ls command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzru73pm4faxphchq1f4.png)
    
* **`stat`:** Displays the status of a file.
    
    
![stat command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3rihirbyqw40f90gffh.png)
    
* `mkdir`: This will help to create directories
    
    
![mkdir command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1gbhmxihdp0hj4dzmsw.png)
    
Here we can see all the files present in the current working directory were listed. Then we wrote the command `mkdir newFolder`which created a new folder in the working directory, which we can see using the ls command.
    
* `rmdir`: This will help to delete a directory. Writing a command followed by the path name of the directory will delete the directory, but it will work only when the directory is empty.
    
    
![rmdir command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zd9xps5a8qp5ce3snnom.png)
    
In the above image, we can see that when the directory is not empty we cannot delete it.
    
    
![rmdir command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cy7qto6i98u58flm1u8s.png)
    
Here we can see that we deleted everything from the newFolder folder, then we are able to delete it using the `rmdir` command.
    
* `cp`: This command is used to copy files and folders from one location to other.
    
    
![cp command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bx9ipdcq979sau82jngg.png)
    
In the above image, we can see that we copied the `abc.txt` file from the Desktop to the `newFolder` folder present on the desktop.
    
* `mv`: This command can be used to move files between different directories. This command can also be used to move files to different directories with different names, which we can also use to rename files.
    
    
![mv command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0igmd3zq41t36ijljyig.png)
    
In the above image, we can see that Desktop has a file with the name abc.txt and newFolder folder doesn’t have anything inside it. Then by using the command mv abc.txt xyz.txt, we are renaming abc.txt to xyz.txt, then by using the command `mv xyz.txt newFolder/mno.txt`, xyz.txt is moved to newFolder folder.
    
* `rm`: This command can be used to delete files from directories, we can also use it to delete non-empty directories using the -r option, which could not be done by `rmdir` command.
    
    
![rm command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysi49cbmh5lc8dr8ncqa.png)
    
In the above image, we can see that abc.txt was present in the Desktop directory but after using the command rm abc.txt, it is not there anymore. Similarly, we can see newFolder folder in the Desktop directory with mno.txt file inside it, using the command rm -r newFolder folder newFolder is not there anymore.
    
* `find`: This command can be used to search files or folders using options as filters. You can explore this command using man.
    
    
![find command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfjhrfh9ecdhlqox5qsg.png)
    
In the above image, using -name option we are able to find files after applying some filter to the search.
    

## Multi-user Environment

Before moving forward let’s understand the multi-user environment in Linux. In a multi-user environment, the system allows keeping the user’s files separate from other users’ profiles.

* `whoami`: In order to check which user is currently logged in, we can use whoami command.
    
    
![whoami command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfg0n43rtezcsrwomaal.png)
    
* `su`: This command can be used to substitute the user. One of the uses of switching users is to do system administration tasks. 

There are generally two kinds of users, Normal users, and Superusers.

**Super Privileges**

- The root is often disabled on the modern Linux system.
- Admin can borrow the root’s privilege using the sudo command.
* `sudo`: Root privileges can be borrowed using sudo command followed by a password verification.
    
    
![sudo command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oeq68sq125ktnsse9qj4.png)
    
In the above image, we can see that using ls without sudo to see the root folder’s file is not allowed. But on using sudo command followed by the password we can use it.
    
To switch to a root user, we can use the -s option after sudo, and to go back to the normal user, you can use the exit command.
    
    
![root user switch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbmp0pkspran4gd2q4kp.png)
    

**File Permissions**

For every file, there are permissions details for three types of users:

1. user (owner of the file)
2. group (collection of users for whom we can define specific actions)
3. others (all other users)

When we write command stat with some file name then we get the details of access related to that file.


![stat command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5ztwks588bu7q90dz2p.png)

In the above image, we can see the Access details: (0664/-rw-rw-r—).

For -rw-rwr—, first - is to tell that it is a file then the next three characters belong to the user, the next three to the group, and the last three to others.

There are two methods to represent permission:

1. Octet (like 755, 644, and 777)
2. Symbolic (like r—, rw-, —x)

In the symbolic representation, ‘r’ means read, ‘w’ means write, and ‘x’ means to execute. 

If a user has permission rwx, that means he can read, write and execute the file.

In octet way of writing permission: 

**Changing File Permissions**

* `chmod`: This command can be used to change the permission of different users.
    
    
![chmod command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3j60we86xgjjgim1hkqw.png)
    
In the above image, we can use that, the first user in one.txt had permission to read and write only, but after using the command `chmod u+x one.txt` it also gets permission to execute.
    
* `chown`: This command can be used to change the file’s owner. This command needs root permission to run.
    
    
![chown command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyn0yfuivnuvhap2zi1f.png)
    
In the above image, we can see on writing the command ls -l all the details of file one.txt got outputted and there we can see two shivam written, first shivam is in the place of the user and the second shivam is in the place of the group. On writing the command chown without sudo, we are getting operation not permitted, therefore we are using sudo with chown command, which is changing the user from shivam to root.
    

## **Links**

Links are files that reference other files. It is used to avoid having duplicate files. A link establishes a pointer in one place to a destination in another place.

There are two types of links:

1. Soft Link: A soft link or symbolic link points to another file.
2. Hard Link: A hard link points to specific data on the disk.

* `ln`: This command is used to create a new link for some file. We can use -s option with ln for soft links followed by the file we want to point to and then the pointer name
    
    
![ln command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ib5y05ibq52cohzs2pzq.png)
    
If we would not pass any option with the command then the link will be a hard link.
    
One thing to note with the soft link is that if we change the location of the file to which the pointer points to then the pointer would not give the correct output or even if the location of the pointer file gets changed, in that case also it would not work. 
    

## **Commands to read text**

* `cat`: This command displays the contents of one or more files without having to open the file for editing.
    
    
![cat command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4mkmira7fbc7hyzvvox.png)*
    
* `head`: This command will output the first 10 lines of the file you give as an argument. But we can also pass the option to get as many lines as you want using the -n option.
    
    
![head command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6x08yqilqx6dxgx2ggy.png)
    
* `tail`: This command is similar to the head command but the only difference is that it will print the last lines.
    
    
![tail command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzbcl3dfjw9vl45um7ry.png)
    
* `less`: This command displays the text of one page or screenful at a time and provides navigation control.
    
    
![less command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60bm9bvk7czhyxfmi24s.png)
    

If you have any suggestions, please write them in the comment section. I would love to add it to the blog. ✌️