Mark As Completed Discussion

In this lesson, we will learn about bash and its basic commands, with a focus on the following key points:

  1. Getting an introduction to bash and its commands.
  2. Understanding how to perform operations using the command line.

Computers provide two kinds of interfaces to the user:

  1. Graphical User Interface (GUI), and
  2. text-based interfaces (through command line or terminal)

These interfaces allow you to navigate through the operating system and file system. In text-based interfaces, a command line interpreter or shell is used to interpret the given commands by the user. This tutorial focuses on one such popular shell known as bash. It is the default shell of most Linux distributions and Apple's macOS.

Bash

Bash allows users to provide commands through which the user can specify what they want to do. Before providing the commands, bash displays a prompt, as you open the command line (in Linux and macOS), such as the one below.

SNIPPET
1user@localhost:~$

This prompt would typically state the current username with the hostname of the system. By default, the current directory of the terminal will be the user's root (or home) directory.

Bash

After a command has finished executing, this prompt will be displayed again indicating that the shell is now ready to execute another command. These commands that you provide will be displayed after the prompt.

Bash Basic Commands

Using bash is all about knowing about its different commands. Using a combination of these commands with various options makes bash extremely powerful.

Basic bash commands are mostly related to system navigation. Consider an example where the home directory looks something like the following. We will use this example in the explanation of various commands below.

Bash Basic Commands

echo

echo is the most basic command in bash, which displays its arguments on standard output (on the terminal).

Syntax: echo[option]<text>

SNIPPET
1user@localhost:~$ echo "Hello World!"
2Hello World!
3user@localhost:~$ echo Hello World!
4Hello World!

Note that it is not necessary to enclose the text after the keyword echo in quotation symbols. However, it is recommended that you do so.

Another use of echo is when you want to display the value of system variables. For this, you need to use a dollar sign ($) before the variable name. For example, we can check the value of the current shell using the following command.

SNIPPET
1user@localhost:~$ echo $SHELL
2/bin/bash

pwd

pwd is a command that stands for Print Working Directory. It displays your current working directory as output.

Syntax: pwd[option]

SNIPPET
1user@localhost:~$ pwd
2/home/user

ls

ls is a command short for list. It lists down the contents of a directory. By default, it lists the contents of the current working directory. From the example introduced at the start of the section, if we list down the contents of our current directory we get,

Syntax: ls[option]<location>

SNIPPET
1user@localhost:~$ ls
2Documents Pictures Videos

ls is mostly used with options unlike pwd or echo which are mostly used without them (even though options are available for them). ls can also take an optional argument (specified after options, if any), which can be the location of the directory.

Let's look at the usage of ls using the -a option. This option enables us to view hidden files. In the code block below, the files with a preceding dot ., are hidden files. These hidden files are not visible on GUI unless you enable the option to view them.

SNIPPET
1user@localhost:~$ ls -a
2Documents Pictures .temp_file Videos .webpage

cd

cd is a command to change directory. By providing a path along with this command, we can change the current directory to a different directory. This command does not give an output and only changes the directory. You can later use ls to display the directory contents to check that the current directory has been changed.

Syntax: cd[option]<directory>

SNIPPET
1user@localhost:~$ cd Documents
2user@localhost:~$ ls
3file1.txt 
4user@localhost:~$ cd ..
5user@localhost:~$ ls
6Documents Pictures Videos

In the above example, we used a shortcut .. to change back to the parent directory. This can be used several times in a single path to go up the hierarchy of parent directories. Of course, you can provide the entire path to the directory as well.

Try this exercise. Click the correct answer from the options.

The command cd changes ____.

Click the option that best answers the question.

  • home directory
  • working directory
  • root directory
  • None of the above

Are you sure you're getting this? Is this statement true or false?

The command echo can take options.

Press true if you believe the statement is correct, or false otherwise.

Bash Commands for File Modifications

Working with files means modifying them in any capacity. Renaming, moving, creating, and deleting new files are the basic operations when working with files. Let's have a look at these operations using bash in this section.

mkdir

mkdir is a command short for Make Directory. It creates a new directory with the specified name.

Syntax: mkdir[option]<directory name>

SNIPPET
1user@localhost:~$ mkdir BashTutorial
2user@localhost:~$ ls
3BashTutorial Documents Pictures Videos

mkdir

rmdir

rmdir is a command for Remove Directory. It removes the specified directory. It should be noted that the directory to be removed must be empty or the terminal will throw an error.

Syntax: rmdir[option]<directory name>

SNIPPET
1user@localhost:~$ rmdir BashTutorial
2user@localhost:~$ ls
3Documents Pictures Videos

rmdir

Another important thing to note is that when working with the command line, there is no undo option. Once you remove a directory, you won't be able to restore it. So be careful with what you delete!

touch

touch is a command used for creating blank files. This command only creates a blank file and does not add anything to it.

Syntax: touch[option]<filename>

SNIPPET
1user@localhost:~$ touch Newfile
2user@localhost:~$ ls
3Documents Newfile Pictures Videos

rm

rm is the command for removing files. Similar to rmdir, this action cannot be undone so careful use is recommended.

Syntax: rm[option]<filename>

SNIPPET
1user@localhost:~$ rm Newfile
2user@localhost:~$ ls
3Documents Pictures Videos

A helpful option available for rm is -r. Adding this option allows us to remove non-empty directories with ease, as it recursively goes through all the files and sub-directories inside a directory and deletes them.

SNIPPET
1user@localhost:~$ rmdir Videos
2rmdir: failed to remove 'Videos': Directory not empty
3user@localhost:~$ rm -r Videos
4user@localhost:~$ ls
5Documents Pictures 

mv

mv is a command used to move a file or a directory. We can specify this using the syntax below, where source and destination are paths to the file or directory that needs to be moved.

Syntax: mv[option]<source><destination>

SNIPPET
1user@localhost:~$ ls
2Documents Pictures Videos example1
3user@localhost:~$ mkdir NewFolder
4user@localhost:~$ mv example1 NewFolder/
5user@localhost:~$ ls
6Documents NewFolder Pictures Videos
7user@localhost:~$ cd NewFolder
8user@localhost:~$ ls
9example1

mv is a helpful command that can also be used for renaming files. If we specify the destination path the same as the source path, but with a different name, then we can rename the file.

SNIPPET
1user@localhost:~$ ls
2Documents Pictures example1
3user@localhost:~$ mv example1 linux_example
4user@localhost:~$ ls
5Documents Pictures linux_example

Try this exercise. Fill in the missing part by typing it in.

_ command creates new files and _ command creates new directories.

Write the missing line below.

Try this exercise. Is this statement true or false?

Will the following command rename a file?

SNIPPET
1user@localhost: ls
2pic1 pic2
3user@localhost: mv pic1 pic2/

Press true if you believe the statement is correct, or false otherwise.

Learn More

Note: For most of the commands discussed above, you can provide options to customize bash commands. To find out the list of options available for each command, --help option can be used with the command (for example, to get the list of options for the command ls, ls --help is used).

We've made it! To dive deeper into Linux and Bash commands, check out our guide to advanced commands for more.

One Pager Cheat Sheet

  • In this lesson, we will learn about bash and its basic commands, with a focus on understanding how to perform operations using the command line.
  • Bash enables users to provide commands after a prompt, which then executes these commands and displays the prompt again when done.
  • With basic Bash commands you can navigate through a system to effectively manage files and directories.
  • The echo command displays its arguments on the standard output on the terminal, with an optional option, and can also be used to output the values of system variables when preceded with a dollar sign ($).
  • The command pwd is used to Print Working Directory, displaying the current directory in the output.
  • ls is a command used to list down the contents of a directory, often with options or a specific location specified.
  • We can use the cd command to change directory both with a specific path and by going up the hierarchy of parent directories with a shortcut.
  • The echo command can be modified using options, flags and/or switches, such as -n, to alter its default behavior of ending the output with a newline character.
  • Through the use of bash commands, files can be renamed, moved, created, and deleted.
  • The mkdir command allows users to Make Directory with the specified name, using the syntax mkdir[option]<directory name>.
  • Using rmdir with the syntax: rmdir[option]<directory name> removes a specified and empty directory, however, there is no undo option so users should exercise caution.
  • The touch command is used to create blank files with the syntax touch[option]<filename>.
  • The command rm is used to remove files, with the addition of the -r option allowing the recursive removal of non-empty directories.
  • The mv command can be used to move or rename a file or directory.
  • The touch command is used to create empty files and update file timestamps, while the mkDIR command is used to create new directories that don't already exist.
  • This command does not rename the file but instead moves it using the mv command to a different directory.
  • To customize Linux and Bash commands, use--helpoption with a command and for more advanced commands and tips, refer to our guide to advanced commands.