Introduction to Linux Commands
Tags: linux, devops, terminal, programming
Published: 2022-02-04 05:09:37

I am learning DevOps for a while and I will be writing about everything I learn, which will eventually be beneficial for the community and aspiring DevOps learners.
I previously wrote about YAML which is very important for the DevOps journey have a look [here](https://dev.to/sumitmukharjii/introduction-to-yaml-18e).

**Prerequisite**
- *1. A Linux terminal *
- *2. Enthusiasm for learning*

**For setting up Ubuntu in windows terminal head over to this [article](https://ubuntu.com/wsl)** officially by **Ubuntu**.

Okk...Let's start with **shell**, this is the place where we write our commands but what it is??
A shell is a command-line interface that will take all our command as input, interprets them, and converts them into useful things.

For some commands I have not included screenshot as they have to done by self.

![Screenshot (2).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643268353299/Q7oPG92tp6.png)
Here's an image of what shell looks like.

Shells are of many types like Bourne shell,c-shell,z-shell, etc but here we won't discuss that as it is a very different topic, but as always you can head over [here](https://www.tutorialspoint.com/unix/unix-what-is-shell.htm) to know about it.

Let's deep dive with the commands -
 
- 
**$ clear**
``` - Clears the shell.

- 
**$ ls**
 - This command lists all files and folders in the current directory.

![Screenshot (3).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643269252944/mH_07NNWc.png)

- ```
**$ mkdir**
``` - Makes new folder

![Screenshot (4).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643269355911/RdC7Gh6Bh.png)

- $ cd - This one changes the directory
if we write ```

cd ..
``` then this takes us one folder back.

- **$ whereis** - This gives the exact directory of any file.

- **$ xdg-open** - Opens particular directory.

- **$ ls -a** - Shows hidden files and folders.

![Screenshot (5).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643269424623/wnfzmdChz.png)

- **$ pwd** - This prints the current working directory.

![Screenshot (6).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643269680832/4pMdPwG7sG.png)

. -> current directory
.. -> previous directory

- **$ ls -l** - Gives more arguments like writable, readable, and executable state of the file, permission, and stuff.

![Screenshot (7).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643269813606/5tEwhxg9R.png)

- **$ ls -R** - Lists all-recursive folder i.e folder inside folder .... and so on

- **$ cat** - This outputs the content of a file.

![Screenshot (8).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643269943866/reeJ0PdvY4.png)

If there is no file then **$ cat > hello.txt** can be used to make a file and give inputs to it.

Also **$ cat file.txt hello.txt > sumit.txt** can be used to replace the content of sumit.txt with the other two.
**$ echo "Hello World" > file.txt** this replaces all content of file.txt with Hello-World.

- **$ man** - This command gives information about a particular command. If you specify **$ man sudo** in the terminal you will get something like this 👇

![Screenshot (9).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643270242573/KErv7YsIW.png)

- **$ touch** - touch command create a new file.

**$ touch new.txt**

- **$ cp** - copy contents of one file to another.

**cp file.txt newFile.txt**

- **$ mv** - this can rename as well as move a file.

**mv fike.txt newName.txt** (This renames)

**mv file.txt usr/local/bin**  (This copies)
- **$ rm** - Remove a file

**rm file.txt**

- **$ rmdir** - Removes a directory only if it is empty.

**$ rmdir <dir_name>**

- **$ sudo** - This command gives administrative privileges, if you are a Linux user you may be fully aware of it.

- **$ df** - gives a capacity of the disk.

**df -m** (Size in Megabytes)

**df -k**  (Size in Kilobytes)

**df -h** (In human-readable form)



![Screenshot (11).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643270938295/uF92u9GBe.png)


![Screenshot (10).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643270945874/oJhfkCOoj.png)

- **$ du** - This gives disk usage statistics.

**du -l** 

**du -h** (In human-readable form)


![Screenshot (12).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643271109699/U2yZnW_2q.png)

- **$ head** - Used to vies first few lines of any file, by default,
![Screenshot (13).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643271358562/xT4wBBa0d.png)
it's 10.

As you can see after using the head command it's only showing 10 lines.
**$ tail** command shows the last few lines.

- **$ diff** - This differentiate content of a file.

- **$ locate ** - This locates any file. **$ locate ".txt"** this will locate all the files which have .txt extension.

- **$ whoami** - Displays username.

![Screenshot (14).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643271847326/CkNxTxW7M.png)

- **File Permissions - ** There are 3 file permissions Read, Write and Execute

**$ chmod** is the command to change file permissions.

**Performing action on multiple files **

If you wanna let's say remove multiple files at once then use:-

**$ find . -type f -name "*.txt" -exec rm -rf {} +**

basically {} is the placeholder and rm shows that remove these.

- **$ grep** - Global regular expression print, grep allows to look for particular text in the file. GREP is case-sensitive.
I have made a text file that contains names of different random people and applied the following commands 👇


```$ grep "Kunal" names.txt
``` 

```$ grep -w "Kunal" names.txt
``` (Shows complete word)

```$ grep -i "kunal" names.txt 
``` (shows case sensitive names too)

```$ grep -iw "kunal" names.txt
``` (shows case sensitive names with word specified)

![Screenshot (15).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643281349556/Rz4x2zYHC.png)


```$ grep -n "Kunal" names.txt
``` (Shows line number)

```$ grep -win "Rahul" names.txt
``` (Combined with the word, case sensitive, and line number)

```$ grep -b 3 "Rahul"  ./.*txt"``` (WIll search for Rahul in all txt files)

```$ grep -rwin "Rahul" ./.*txt``` (Will search recursively in all directories)


![Screenshot (16).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643281962227/9b3S-V2b_.png)

**Terminal Shortcuts** - 


1. *Ctrl + A* - Moves the cursor to begin.
2. *Ctrl + E* - Moves the cursor to end.
3 *Ctrl + U* - Removes Everything written.
4. *Ctrl + K* - Removes a word after the cursor.
5. *Up and Down Keys* - Previous commands.
6. *$ !<Previous_Command>* - With ! sign it will run the previous command.
7.  *$ command_1;command_2;command_3* - Multiple commands with semi-colon.
8. *$ sort names.txt* - will sort in Ascending order.
9. *$ sort -r names.txt* - will sort all the names in reverse order.
10. *$  jobs* - displays current job.
11. *$ ping <url>* - Check your server.
12. *$ wget <URL>* - Download file.
13. *$ top* - shows all the running processes.
14 . *$ kill <process_id>* - Kills the running process.
15. *$ zip file_name.zip file_to_be_zipped* - Zips the file

if this doesn't work then use   ``` $ sudo apt install zip ``` (installs package). Multiple files can be zipped.

16. *$ unzip file_name.zip* - Unzipes the file
17. *$ hostname* - Displays hostname.
18. *$ hostname -i* - Displays IP too
19. *$ uname* - Gives OS name
20. *$ uname -m* - Gives Architecture of os.
20. *$ uname -r* - Gives Kernal version.
21. *$ cat /etc/os-release* - Gives Operating System Info.
22. *$ free -h* - Displays free memory.


**Networking Commands - **

 - ```$ nslookup google.com``` - Gives IP address of the particular site, here google.com .

- ```$ netstat``` - Looks all active stat.

**Some more useful commands - **

- ```$ cut -c 1-2 names.txt``` - Gives names after cutting two letters and we can specify the condition as we need.


![Screenshot (18).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1643290282731/n5NuTAjSh.png)

- ```$ ps aux``` - Gives all the processes and it'd ids.

So this was it from my side and there are tons of commands but you will get everything while learning. Things will come naturally.

If you have read till here..then thanks.
Keep Learning😊😊