DEV Community

Krunal Kanojiya
Krunal Kanojiya

Posted on

20 Shell Scripting Command You should know

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
Enter fullscreen mode Exit fullscreen mode

2. changes the current working directory

Command: cd
Example: cd /home/user/documents
Output: changes the current working directory to /home/user/documents
Enter fullscreen mode Exit fullscreen mode

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

Command: ls
Example: ls
Output: file1 file2 directory1 directory2
Enter fullscreen mode Exit fullscreen mode

4. creates a new directory

Command: mkdir
Example: mkdir new_directory
Output: creates a new directory called "new_directory"
Enter fullscreen mode Exit fullscreen mode

5. removes an empty directory

Command: rmdir
Example: rmdir empty_directory
Output: removes the directory called "empty_directory"
Enter fullscreen mode Exit fullscreen mode

6. copies a file or directory

Command: cp
Example: cp file1 file1_copy
Output: creates a copy of "file1" called "file1_copy"
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

8. removes a file

Command: rm
Example: rm file1
Output: removes "file1"
Enter fullscreen mode Exit fullscreen mode

9. creates an empty file

Command: touch
Example: touch new_file
Output: creates an empty file called "new_file"
Enter fullscreen mode Exit fullscreen mode

10. displays the contents of a file

Command: cat
Example: cat file1
Output: displays the contents of "file1"
Enter fullscreen mode Exit fullscreen mode

11. displays the first few lines of a file

Command: head
Example: head -n 5 file1
Output: displays the first 5 lines of "file1"
Enter fullscreen mode Exit fullscreen mode

12. displays the last few lines of a file

Command: tail
Example: tail -n 10 file1
Output: displays the last 10 lines of "file1"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

15. sorts the lines in a file

Command: sort
Example: sort file1
Output: displays the lines in "file1" in alphabetical order
Enter fullscreen mode Exit fullscreen mode

16. removes duplicate lines from a file

Command: uniq
Example: uniq file1
Output: displays the lines in "file1" with duplicates removed
Enter fullscreen mode Exit fullscreen mode

17. compares two files and displays the differences

Command: diff
Example: diff file1 file2
Output: displays the differences between "file1" and "file2"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)