DEV Community

Cover image for Bash cheat sheet: Top 28 commands for you to learn 🀺
Ankit Anand ✨
Ankit Anand ✨

Posted on

Bash cheat sheet: Top 28 commands for you to learn 🀺

Hi there,

Today I want to bring you a summary of top Bash commands. Bash is a very popular shell program. It stands for 'Bourne Again Shell'. There are also other types of shells, but Bash is the most widely available and used shell.

Anything inside [ ] means that it's optional.

1. ls - List directory contents

ls is one of the most common commands. It is used to list the contents of a directory.

Syntax: ls [options][files]
Common options: -a, -l

ls -a shows hidden(dot) files
ls -l shows file details

2. echo - Prints text to the terminal window

echo prints text to the terminal window. It is used widely for printing status texts and values of environment variables.
Syntax: echo [option(s)] [string(s)]
Common options: -e, -n



bash-3.2$ echo "hello"
hello


Enter fullscreen mode Exit fullscreen mode

3. touch - Creates a file

touch can be used to create an empty file quickly. It can also be used to modify file timestamps.
Syntax: touch [option(s)] file_name(s)
Common options: -a, -m, -r, -d



bash-3.2$ touch a b
bash-3.2$ ls
a     b


Enter fullscreen mode Exit fullscreen mode

4. mkdir - Create a directory

mkdir is used to create new directories. You can create multiple directories simultaneously.
Syntax: mkdir [option(s)] directory_name(s)
Common options: -m, -p, -v



bash-3.2$ ls
a     b
bash-3.2$ mkdir c
bash-3.2$ ls
a     b    c


Enter fullscreen mode Exit fullscreen mode

5. grep - Search for particular pattern

grep is used for finding text in files. It comes in handy when you're tasked to find a particular string or pattern within a file.
Syntax: grep [option(s)] pattern [file(s)]
Common options

  • -i performs a case-insensitive search
  • -n displays the lines containing the pattern along with the line numbers
  • -c displays the count of the matching patterns.

6. man - Print manual or get help for a command

Use the man command to bring up a manual for any command. For example, if you don't know what mkdir does, then use the man command to see its manual.
Syntax: man [option(s)] keyword(s)
Common options: -w, -f, -b



bash-3.2$ man mkdir


Enter fullscreen mode Exit fullscreen mode

7. pwd - Print working directory

pwd command prints the working directory you are currently in.

8. cd - Change directory

You can change to a different directory with cd command. Use cd .. to go to the parent directory.

9. mv - Move or rename directory

mv is used to move or rename directories. Without this command, you would have to individually rename each file which is tedious.
Syntax: mv [option(s)] argument(s)
Common options: -i, -b

10. rmdir - Remove directory

rmdir will remove empty directories. This can help clean up space on your computer and keep files and folders organized. There are two ways to remove directories: rm and rmdir. rm will remove directories and files even if it's not empty whereas rmdir will only delete empty directories.
Syntax: rmdir [option(s)] directory_names
Common options: -p

11. locate - Locate a specific file or directory

It is the simplest way to find a file or directory. It searches a cached list of files to find the location of a specified file.
Syntax: locate [option(s)] file_name(s)
Common options: -q, -n, -i

12. less - view the contents of a text file

The less command allows you to view files without opening an editor. It’s faster to use, and there’s no chance of you inadvertently modifying the file.
Syntax: less file_name
Common options: -e, -f, -n

13. compgen - Shows all available commands, aliases, and functions

compgen is a bash built-in command which is used to list all the commands that could be executed in the Linux system.
Syntax: compgen [option(s)]
Common options: -a, -c, -d

14. > - Redirect stdout

This takes the output from the preceding command that you’d normally see in the terminal and sends it to a file that you give it. As an example, take echo β€œtest” > dev1. Here it creates a file called dev1 and puts the echoed string into it.

15. head - Read the start of a file

The head command allows you to read the first few lines of a file. The default number is 10.
Syntax: head [option(s)] file(s)
Common options: -n, can be used to specify the number of lines to show

16. tail - Read the end of a file

tail outputs the last few lines of a file. And, just like the head command, the default is 10.
Syntax: tail [option(s)] file_names
Common options: -n

17. cat - Read a file, create a file, and concatenate files

cat, short for concatenate, is one of the most commonly used commands. It allows you to create single or multiple files, view file inclusions, concatenate files and redirect output in a terminal or file.
Syntax: cat [option(s)] [file_name(s)] [-] [file_name(s)]
Common options: -n

18. | - Pipe

A pipe takes the standard output of one command and passes it as the input to another.
Syntax: |

19. chmod - Sets the file permissions flag on a file or folder

The chmod command is used to change the access mode of a file. The name is an abbreviation of change mode.
Syntax: chmod [reference][operator][mode] file...
Common options: -f, -v
References are used to distinguish the users to whom the permissions apply. Operator is used to specify how the modes of a file should be adjusted. The modes indicate which permissions are to be granted or removed from the specified classes.

20. exit - Exit out of a directory

exit command is used to exit any kind of shell.
Syntax: exit

21. history - List your most recent commands

history command prints out your recent commands, one command per line. It also prints the history number for each command. Each command is associated with a number for easy reference. You can truncate the output by specifying a number after the command.
Syntax: history
Common options: -c, -d

22. clear - Clear your terminal window

This command is used to clear all previous commands and output from consoles and terminal windows. This keeps your terminal clean and removes the clutter so you can focus on subsequent commands and their output.
Syntax: clear

23. cp - Copy files and directories

cp stands for copy. This command is used to copy files or groups of files or directory.
Syntax: cp [option(s)] current_name new_name
Common options: -r, -i, -b

24. kill - Terminate stalled processes

The kill command allows you to terminate a process from the command line. You do this by providing the process ID (PID) of the process to kill. You can use the ps command to find the PID. kill is a built-in command of the bash shell.
Syntax: kill [option(s)] PID(s)

25. sleep - Delay a process for a specified amount of time.

sleep command is used in shell scripts to take a pause before the execution of the next command. For example, it can be used inside a script to pause before retrying a failed operation or inside a loop.
Syntax: sleep number [suffix]
Suffix is used to specify the unit of time and can be:

  • s - seconds(default)
  • m - minutes
  • h - hours
  • d - days

26. passwd - Change account password

You can change your account password using passwd command. It will ask for your old password and then ask to enter your new password twice.
Syntax: passwd

27. df - Check Disk Space

df command is used to get a detailed report on the system’s disk space usage. When used without any argument, the df command will display information about all mounted file systems.
Syntax: df [options]... filesystem...

28. comm - Compare two files

You can use comm command to compare two sorted files line by line. To compare two sorted files using 'comm', you have to pass their names as arguments.
Syntax: comm [OPTION]... FILE1 FILE2


I hope you enjoyed this summary and learned something new. I will be coming up with more such notes soon.

Please give me a follow if you liked it. πŸ™Œ


Currently building SigNoz - an open-source APM & observability tool πŸ’™

GitHub logo SigNoz / signoz

SigNoz is an open-source observability platform native to OpenTelemetry with logs, traces and metrics in a single application. An open-source alternative to DataDog, NewRelic, etc. πŸ”₯ πŸ–₯. πŸ‘‰ Open source Application Performance Monitoring (APM) & Observability tool

SigNoz-logo

Monitor your applications and troubleshoot problems in your deployed applications, an open-source alternative to DataDog, New Relic, etc.

Downloads GitHub issues tweet

SigNoz helps developers monitor applications and troubleshoot problems in their deployed applications. With SigNoz, you can:

πŸ‘‰ Visualise Metrics, Traces and Logs in a single pane of glass

πŸ‘‰ You can see metrics like p99 latency, error rates for your services, external API calls and individual end points.

πŸ‘‰ You can find the root cause of the problem by going to the exact traces which are causing the problem and see detailed flamegraphs of individual request traces.

πŸ‘‰ Run aggregates on trace data to get business relevant metrics

πŸ‘‰ Filter and query logs, build dashboards and alerts based on attributes in logs

πŸ‘‰ Record exceptions automatically in Python, Java, Ruby, and Javascript

πŸ‘‰ Easy…




Top comments (1)

Collapse
 
sajjad2700 profile image
sajjadahmadi4

thanks for the article ❀
really needed this.