Know About Bash Command in Linux
Looking for a comprehensive list of Bash commands in Linux? Look no further! This guide covers all the major Bash commands, including ls, cd, mkdir, rm, grep, sed, awk, and more.
This article on the bash command in Linux focuses on the bash shell, one such well-liked shell. The majority of Linux distributions and Apple’s macOS use it as their default shell.
Introduction
There are two different user interface types that computers offer:
1. Graphical User Interface (GUI), and
2. Text-based interfaces (through the command line or terminal)
You can explore the operating system and file system using these interfaces. A command line interpreter, often known as a shell, is used in text-based interfaces to translate the user’s commands.
Best-suited Linux courses for you
Learn Linux with these high-rated online courses
Bash
With the help of commands, users can specify what they wish to perform using Bash. When you open the command line (in Linux and macOS), bash displays a prompt like the one below before asking for the commands.
Code Syntax
user@localhost:~$
Normally, this prompt would list the system’s hostname and the current username. By default, the user’s root (or home) directory will always be the terminal’s current directory.
This prompt will appear once again after a command has completed running to indicate that the shell is now prepared to run another command. Following the prompt, the commands you enter will be shown.
Bash Commands
Understanding bash’s various commands are essential to using it. Bash is incredibly strong when used in conjunction with a variety of settings and these commands. The majority of basic bash commands deal with system navigation. Take a look at the sample below, where the home directory appears as follows.
echo
The most fundamental command in Bash is echo, which outputs its inputs on standard output (on the terminal).
Syntax
echo[option]<text>
Code Snippet
user@localhost:~$ echo "Hello World!"Hello World!user@localhost:~$ echo Hello World!Hello World!
It should be noted that the text that follows the term echo does not require quotation marks. It is advised that you do this, though.
When you wish to display the value of system variables, you can also utilize echo. To do this, you must place a dollar symbol ($) before the name of the variable. For instance, the following command can be used to check the value of the current shell.
Code Snippet
user@localhost:~$ echo $SHELL/bin/bash
pwd
Print Working Directory is a command that goes by the abbreviation pwd. Your current working directory is shown as output.
Syntax
pwd[option]
Code Snippet
user@localhost:~$ pwd/home/user
ls
ls in commands is short for list. It provides a list of a directory’s contents. It displays the contents of the current working directory by default.
Syntax
ls[option]<location>
Code Snippet
user@localhost:~$ lsDocuments Pictures Videos
Unlike pwd or echo, which are typically used without them, ls is primarily used with options (even though options are available for them). The location of the directory can be supplied as an additional optional parameter to ls, which is specified after any options.
Let’s see how to use ls with the -a option. Using this option, we can see hidden files. The files in the code block below that have a dot before them do not appear. Unless you allow the option to view them, these hidden files are not visible on the GUI. In the below example, demo.txt is a hidden file, which we can access by adding -a to ls command.
Code Snippet
user@localhost:~$ ls -aDocuments Pictures Videos demo.txt
cd
The bash command in Linux to change directories is cd. With this command, we can switch the current directory to a different directory by including a path. This command just modifies the directory; it does not produce an output. Later, you may check to see if the current directory has changed by using ls to view the directory contents.
Syntax
cd[option]<directory>
Code Snippet
user@localhost:~$ cd Documentsuser@localhost:~$ lsfile1.txtuser@localhost:~$ cd ..user@localhost:~$ lsDocuments Pictures Videos
In the above-mentioned illustration, we see that the shorthand for returning to the parent directory. This you can apply multiple times in a single route to move up the parent directory hierarchy. Of course, you may also supply the directory’s complete path.
Bash Commands for File Modifications
Working with files entails making any necessary modifications. The fundamental operations when working with files are renaming, transferring, creating, and deleting new files. In this part, we’ll look at these activities using bash.
mkdir
mkdir is short for Make Directory. With the given name, it is for creating a new directory.
Syntax
mkdir[option]<directory name>
Code Snippet
user@localhost:~$ mkdir BashTutorialuser@localhost:~$ lsBashTutorial Documents Pictures Videos
rmdir
The command rmdir stands for Remove Directory. It deletes the chosen directory. The directory to remove must be empty for there to be no errors in the terminal.
Syntax
rmdir[option]<directory name>
Code Snippet
user@localhost:~$ rmdir BashTutorialuser@localhost:~$ lsDocuments Pictures Videos
touch
The command touch makes new, empty files. This command doesn’t contribute anything to the file; it only generates a blank one.
Syntax
touch[option]<filename>
Code Snippet
user@localhost:~$ touch Newfileuser@localhost:~$ lsDocuments Newfile Pictures Videos
rm
The command for deleting files is rm. Similar to rmdir, this action will not reverse. So take caution.
Syntax
rm[option]<filename>
Code Snippet
user@localhost:~$ rm Newfileuser@localhost:~$ lsDocuments Pictures Videos
rm has a useful option called -r. Because it recursively deletes all of the files and subdirectories inside a directory, adding this option enables us to easily remove non-empty directories.
user@localhost:~$ rmdir Videosrmdir: failed to remove 'Videos': Directory not emptyuser@localhost:~$ rm -r Videosuser@localhost:~$ lsDocuments Pictures
Conclusion
The blog explained the syntax and code snippets of bash commands in Linux. The basic bash commands include echo, pwd, ls, and different bash commands for file modifications, including mkdir, touch, and rm. Remember that bash commands are case-sensitive.
If you plan to learn more, check out the whole list of Linux courses.
Contributed by Megha Chadha
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio