DEV Community

Cover image for An Overview of Bash(Bourne Again Shell) Command Line
Neeraj Gupta for DSC CIET

Posted on • Updated on

An Overview of Bash(Bourne Again Shell) Command Line

Let's Discuss

In Today's life knowing how to communicate with your computers is important. In, Earlier days, when there was no Graphical Interface(GUI), people used to interact with computers through commands or we can say they used to give certain predefined commands to perform certain tasks.But, in today's world everything has shifted to graphic based interfaces.Today's generation has just forgotten how powerful is command line and how much deep access it gives you over your machine.

Note : If you directly start messing with command line without it's basic knowledge you may end up messing up with some files which are not meant to be although command line do give alerts or ask for permissions but you know we don't have that much patience and we simply ignore that.

How to open command line(For Linux and MacOS)

For Linux Users

You can use Ctrl + Alt + T to directly open terminal/Command Line.

It, will look something like this depending on your Linux Distribution(In Screenshot Ubuntu(Linux distribution) is used).

Note : In taskbar pane you will see an icon something like the one shown in the following image.



For MacOS Users

  1. Press Command + Spacebar, It will open your Spotlight.
  2. Search for Terminal and Press Enter/Return.
  3. It will open a terminal window like the one shown in following image.

The app in Dock will look something like the one shown below.

So, now we are done with setting up part and now we will move on to learning the commands

Jumping off to commands in BASH

1. ls - List Directory

ls is one of the most used commands in bash command line.It helps us to list files of directory which we want to show.

  1. Syntax : ls [option(s)] [directory/directories(seperated by space)]
  2. Command Options :
    • -a : all. Lists all the files in the directory, including the hidden files (.filename).
    • -l : long. Lists details about contents, including permissions (modes), owner, group, size, creation date.

2. echo : Prints text to command line

echo is used to output text to screen and is typically used to print variables status or shell status to screen or a computer file.

  • Syntax : echo[options(s)] [string(s)]
  • Command Options :
    • -n : Used to suppress trailing newline.
    • -e : Used to enable interpretation of backslash-escaped character like '\n'.

3. mkdir : Create a new directory

mkdir is important one if you are wondering how the people in old time used to create directories without Graphical User Interface(GUI). So, here is the command used to create new directories.

  • Syntax : mkdir [option(s)] [directory_name]
  • Command Options:
    • -p : This option is used to create all directories at once including parent and child directories. Ex : li -p a/b/c will create folder named a at present directory and b inside it and c inside b.
    • -m : Used to create directory along with which we can specify modes we want to give to that directory. Ex : mkdir -m a=rwx new means that create a folder named new at present directory and give it read, write and execute permissions.

4. touch : Creates a file

Upto now we have seen some interacting commands and now here is another one to create a file of any type. Let's say you want to create a text file with name "myFile". So, you will write touch myFile.txt in command line. Also, it can be used in cases where we want to overwrite data or change timestamp of a file.

  • Syntax : touch [option(s)] [filename(s)]
  • Command Options :
    • -a : Change the access time only. Ex : touch -a newFile.txt. This will change the last access time of that file to current time.
    • -m : Change the modification time only. Ex : touch -m newFile.txt. This will update the creation time of the file that means it will change the time the file was created to the current one.
    • -c : If the file do not exist, do not create it. Ex : touch -c textFile.txt. This says that if the file named textFile.txt is present then overwrite it otherwise do not create a new one.
    • -r : Use the modification and access times only. Ex : touch -r myFile.txt newFile.txt. This says change the timestamp of newFile.txt with that of myFile.txt.
    • -t : Creates the file using a specified time. Ex : touch -t YYMMDDHHMM.SS newFile.txt.

5. pwd : Print working directory

Here comes the another important command in bash that you will often use while working with directories that is pwd command. As, the name suggests it prints the current directory you are in.

  • Syntax : pwd
  • Command Options : Command options aren't used with pwd.

6. cd : Change Directory

Another important command while you are working between directories. If, you wonder how old generation managed to move between directories of their computers without GUI. Here's the solution cd. As the name suggests it is used to move between directories

  • Syntax : cd [directory_name(can include subclasses seperated by '/')]
  • Command Options : cd typically do not use options.

7. mv : Move or rename Directory

mv command is another life saver while working with directories. Sometimes we want to rename a file then mv can be used. This command actually servers two purposes that is moving and renaming that purely depends on syntax you are using. If you are using syntax for renaming it will rename file else move it.

  • Syntax for renaming : touch [old_name] [new_name]. Here old name means the name before renaming and new_name means the name you want your file/directory to change to.
  • Syntax for moving : touch [directory_you_want_to_move] [destination_directory].
  • Command Options :
    • -i(Interactive) : It ask the user for confirmation before moving a file that would overwrite an existing file, you have to press y for confirm moving, any other key leaves the file as it is. This option doesn’t work if the file doesn’t exist, it simply rename it or move it to new location.
    • -b(backup): With this option it is easier to take a backup of an existing file that will be overwritten as a result of the mv command. This will create a backup file with the tilde character(~) appended to it.

8. cp : Copy Files and Directory

cp command is another life saver while working with directories. Sometimes we want to make a copy of file/directory then cp can be used. This command makes a copy of file(s)/directory(s)

  • Syntax : cp Source_file Destination_file/directory
  • In case Destination file/directory is not present it will create a new one else it will overwrite it.

  • Command Options :
    • -i(interactive): i stands for Interactive copying. With this option system first warns the user before overwriting the destination file. cp prompts for a response, if you press y then it overwrites the file and with any other option leave it uncopied.
    • -b(backup): With this option cp command creates the backup of the destination file in the same folder with the different name and in different format.

9. rmdir : Remove Directory

Like mkdir is used to create directory(s),rmdir is used to remove directory(s).

Note : rmdir is used to only delete empty diretory(s). But, don't worry we have an alternate to remove non-empty directory(s).

  • Syntax : rmdir [directory_name(s)]
  • Command Options : -p - remove the child directory if empty otherwise gives error and then also deletes the parent directory.
  • Ex : rmdir -p a/b

    It will remove b if it is empty and then removes a.

10. rm : Remove Here

Here is the command which i was talking about earlier(rmdir). rm command is used to remove references to files, symbolic links and others. It is generally not used with directories, but in case of non-empty directories we use this command.

  • Syntax : rm [option(s)] [file_name]
  • Command Options :
    • -i (Interactive Deletion) : Like in case of cp and mv, when we use this option it asks for confirmation before removing each file. Press y for yes and n for no.
    • -f (Force Deletion) : If our files are write protected then we use -f option to bypass security check and forcefully delete the file.
    • -r (Recursive Deletion) : This option iterates/moves through all the files and delete each file(s) and sub-directory(s) recursively of the parent directory. At each stage it deletes everything it finds. Normally, rm wouldn’t delete the directories but when used with this option, it will delete. So, here is the answer to previous question that is How to delete non-empty directory.

11. cat : Read a file, create a file, and concatenate files

cat command is used to display, combine copies and create new file(s).

  • Syntax : cat [option(s)] [file_name]

  • Command Options :
    • -n : This options display the line number along with the contents of file.
    • Ex : touch -n new.txt. Here new.txt contains

    My name is Neeraj
    My hobbies include listening to music
    I love coding
    

    So, this command touch -n new.txt will show the output as

    1 My name is Neeraj
    2 My hobbies include listening to music
    3 I love coding
    

    Now try running this without option -n and you will see that it will now show content only and not the line number.

12. exit : Exit out a directory

exit command will terminate the shell and stop all the running tasks and if you are in remote SSH shell, it will log you out.

  • Syntax : exit
  • Command Options : n/a

13. clear : Clears the terminal window

clear command will clear your previous used commands and other stuff from terminal window and will give you fresh terminal screen. Although previous used commands will remain in your history and you can access them by click up arrow button.

  • Syntax : clear
  • Command Options : n/a

14. history : List your most recent commands

history command is used to display all the previous commands used in terminal

  • Syntax : history [option]
  • Command Options :
    • -d : This option can be used if you want to clear a particular command from history
    • Syntax : history -d event_number. Event number can be found using history command.

    • -c : This command option can be used to clear all history.
    • Syntax : history -c

/section>

15. chmod : Sets the file permissions flag on a file or folder

There are situations that you’ll come across where you or a colleague will try to upload a file or modify a document and you receive an error because you don’t have access. The quick fix for this is to use chmod. Permissions can be set with either alphanumeric characters (u, g, o) and can be assigned their access with w, r, x. Conversely, you can also use octal numbers (0-7) to change the permissions. For example, chmod 777 my_file will give access to everyone. In previous command first 7 is for owner of file, second 7 is for groups, and third is for all others.

  • Syntax : chmod 777 new.txt
  • Command Options : -v (verbose) : Shows changed objects. In above command if you add -v option then it will output new.txt and will give access to everyone.

That's it for this blog.Thank you for reading this blog. If you found this blog useful so share it others, maybe it help them too. See you all in next one.

Top comments (0)