10 Basic Bash Commands that Everyone Should know

Read and explore the most common and most useful bash commands here.

Did you know that the full form of BASH is Bourne Again SHell. In this post we’ll learn about bash which is a command line interface (CLI) and is the most widely used shell. This is mostly because most of the developers use LINUX. Now let’s hop right into learning the most common , the most basic yet the most useful bash commands.

  • ls — list the directory contents

ls is the most common command among the common commands. Many of the times, you would be working in a directory and you’ll need to know what files are located there. The ls command allows you to do the same. There are many options for this command, the most common being:

  • ls -l : Long listing of all the files and their size in the current directory.
  • ls -a : List all the files in the current directory including hidden files too.

SYNTAX : ls [OPTIONS] [FILE(S)]

Examples:

  • echo — prints text on terminal window

echo prints text to the terminal window and is typically used in shell scripts and batch files to output status text to the screen or a computer file. It is also used to show the values of environment variables, which tell the shell how to behave when the user gives a certain command. The useful options here are:

  • echo -n "sometext" : It is used to remove the ‘\n’ (newline character) from the output.
  • echo -e "sometext" : It acts as interpretation of escaped characters that are backslashed.

SYNTAX: echo [OPTIONS] [STRING(S)]

Examples:

  • touch — Creates a file

touch is normally used to create new files but it can also be used to modify the timestamps on files and directories. The useful options here are:

  • touch -a filename : It sets the current time and date on a file and creates an new empty file with the name if a file with that name does not exist.
  • touch -t YYMMDDHHMM.SS filename : It sets the access and modification date and time as the given date and time.
  • touch -r file1 file2 : It copies the file modification time of file1 to file2.

SYNTAX: touch [OPTIONS] [FILE_NAME(S)]

Example:

  • mkdir — Create a new directory

mkdir is a command used to create directories. It can be used to create and number of directories simultaneously with ease. The options here are:

  • mkdir -v example :It displays a message for every directory created.
  • mkdir -p a/b/c : It create parent directories as necessary. If the directories exist, no error is specified.
  • mkdir -m a=rwx example : It is used to set the file modes, i.e. permissions, etc. for the created directories.

SYNTAX: mkdir [OPTIONS] [DIRECTORY/DIRECTORIES]

Example:

  • grep — search

grep command is used to search text for patterns specified by the user. There are often scenarios where you’ll be tasked to find a particular string or pattern within a file, but you don’t know where to start looking, that is where grep is extremely useful. The useful options here are:

  • grep -i "TeSt" file : It enables to search for a string case insensitively in the given file.
  • grep -c "Test" file : It displays the count of the pattern in the given file.
  • grep -w "Test" file : It matches only the whole words of the pattern in the file.
  • grep -l "Test" * : It displays the file that contains the given pattern.

SYNTAX: grep [OPTION(S)] pattern [FILE(S)]

Example:

  • | — Pipe

| command takes the standard output of one command and passes it as the input to another. There are no options available for this command. There are no options.

SYNTAX: command1 | command2 | ...

Example:

  • man — Print manual or get help for a command

man is a command to get manual and is very useful when we need to figure out what a command does. The useful options are:

  • man -f command_name : It gives the section in which the given command is present.
  • man -a command_name : It helps us to display all the available intro manual pages in succession.
  • man -k command_name : It searches the given command as a regular expression in all the manuals and it returns the manual pages with the section number in which it is found.
  • man -w command_name : It returns the location in which the manual page of a given command is present.

SYNTAX: man [OPTIONS] [COMMAND NAME]

Example:

Man command Options
  • pwd — Print working directory

pwd is used to print the current working directory. There are no options for this command.

SYNTAX: pwd

Example:

PWD example
  • less — view the contents of a text file

less command is used to view files without opening an editor. It’s faster to use, and there’s no chance of you inadvertently modifying the file. The most common options are:

  • less -N filename : This command shows the number of the lines present in the file.
  • less -g filename : This command highlights the string which was found by last search command.
  • less -s filename : This command causes consecutive blank lines to be squeezed into a single blank line.

SYNTAX: less [OPTIONS] [FILENAME]

Example:

  • alias — create custom commands

alias command is for creating an abbreviation, or a means to avoid typing a long command sequence.They can save a great deal of typing at the command line so you can avoid having to remember complex combinations of commands and options. But make sure not to overwrite any keywords.

SYNTAX: alias alias_name="command"

Example:

Alias

Hope you got to know some new commands or learnt something about an old one. Kudos!!. That’s it for this post. Peace out.

Python | Data Science | Machine Learning enusiast