20 Shell Scripting Command You should know
Tags: shell, command, networking, programming
Published: 2022-12-28 06:52:20

In this tiny article i gonna share with you 20 shell scripting commands with example and expected output :)

**1. displays the current working directory**

```
Command: pwd
Example: pwd
Output: /home/user/documents
```

**2. changes the current working directory**

```
Command: cd
Example: cd /home/user/documents
Output: changes the current working directory to /home/user/documents
```

**3. lists the files and directories in the current working directory**

```
Command: ls
Example: ls
Output: file1 file2 directory1 directory2
```

**4. creates a new directory**

```
Command: mkdir
Example: mkdir new_directory
Output: creates a new directory called "new_directory"
```

**5. removes an empty directory**

```
Command: rmdir
Example: rmdir empty_directory
Output: removes the directory called "empty_directory"
```

**6. copies a file or directory**

```
Command: cp
Example: cp file1 file1_copy
Output: creates a copy of "file1" called "file1_copy"
```

**7. moves a file or directory to a new location or renames it**

```
Command: mv
Example: mv file1 /home/user/documents/file1
Output: moves "file1" to the directory /home/user/documents/
```

**8. removes a file**

```
Command: rm
Example: rm file1
Output: removes "file1"
```

**9. creates an empty file**

```
Command: touch
Example: touch new_file
Output: creates an empty file called "new_file"
```

**10. displays the contents of a file**

```
Command: cat
Example: cat file1
Output: displays the contents of "file1"
```

**11. displays the first few lines of a file**

```
Command: head
Example: head -n 5 file1
Output: displays the first 5 lines of "file1"
```

**12. displays the last few lines of a file**

```
Command: tail
Example: tail -n 10 file1
Output: displays the last 10 lines of "file1"
```

**13. searches for a pattern in a file**

```
Command: grep
Example: grep "pattern" file1
Output: displays all lines in "file1" that contain the word "pattern"
```

**14. counts the number of lines, words, and characters in a file**

```
Command wc
Example: wc file1
Output: displays the number of lines, words, and characters in "file1"
```

**15. sorts the lines in a file**

```
Command: sort
Example: sort file1
Output: displays the lines in "file1" in alphabetical order
```

**16. removes duplicate lines from a file**

```
Command: uniq
Example: uniq file1
Output: displays the lines in "file1" with duplicates removed
```

**17. compares two files and displays the differences**

```
Command: diff
Example: diff file1 file2
Output: displays the differences between "file1" and "file2"
```

**18. searches for files or directories**

```
Command: find
Example: find /home/user -name "*.txt"
Output: displays a list of all text files in the /home/user directory
```

**19. searches for files or directories using a database**

```
Command: locate
Example: locate "*.txt"
Output: displays a list of all text files on the system
```

**20. displays the size of a file or directory**

```
Command: du
Example: du -sh /home/user/documents
Output: displays the size of the /home/user/documents directory in human-readable format
```