An Overview of Bash(Bourne Again Shell) Command Line
Tags: bash, terminal, linux, macos
Published: 2020-09-12 15:10:47

<h1><b>Let's Discuss</b></h1>
<p>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.</p>
<p><b>Note</b> : 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.</p>
<br>
<h1><strong>How to open command line(For Linux and MacOS)</strong></h1>
<br>
<h2>For Linux Users</h2>
<br>
<p>You can use  <b>Ctrl + Alt + T</b> to directly open terminal/Command Line.</p>
<img src="https://www.lifewire.com/thmb/9EbRGXEPSxQzsUv2bEv6E2oeMa4=/1267x670/filters:no_upscale():max_bytes(150000):strip_icc()/ubuntu-terminal-hotkey-34041f7fd039468dabf56a54fa801664.jpg">
<p>It, will look something like this depending on your Linux Distribution(In Screenshot Ubuntu(Linux distribution) is used).</p>
<p><b>Note : </b> In taskbar pane you will see an icon something like the one shown in the following image.</p>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/GNOME_Terminal_icon_2019.svg/1200px-GNOME_Terminal_icon_2019.svg.png">
<br>
<h2>For MacOS Users</h2>
<br>
<ol>
<li>Press <strong>Command + Spacebar</strong>, It will open your <b>Spotlight</b>.</li>
<li> Search for <b>Terminal</b> and Press <b>Enter/Return</b>.</li>
<li>It will open a terminal window like the one shown in following image.</li>
</ol>
<img src="https://cdn.macpaw.com/uploads/images/Screen%20Shot%202018-10-17%20at%2018.14.23.png">
<p>The app in <b>Dock</b> will look something like the one shown below.</p>
<img src="https://media.idownloadblog.com/wp-content/uploads/2016/02/terminal-app-icon-OS-X.png">
<p>So, now we are done with setting up part and now we will move on to learning the commands</p>
<h1><b> Jumping off to commands in BASH</b></h1>

<section>
<h2>1. <b>ls</b> - List Directory</h2>
<p><b>ls</b> is one of the most used commands in bash command line.It helps us to list files of directory which we want to show.</p>
<br>
<ol>
<li><b>Syntax</b> : <code>ls [option(s)] [directory/directories(seperated by space)]</code></li>
<li>Command Options : <ul>
<li><code>-a</code> : all. Lists all the files in the directory, including the hidden files (.filename).</li>
<li><code>-l</code> :  long. Lists details about contents, including permissions (modes), owner, group, size, creation date.</code></li>
</ul>
</section>

<section>
<h2>2. <b>echo</b> : Prints text to command line</h2>
<p><b>echo</b> is used to output text to screen and is typically used to print variables status or shell status to screen or a computer file.</p>
<ul>
<li><b>Syntax</b> : <code>echo[options(s)] [string(s)]</code></li>
<li>Command Options : 
<ul>
<li><code>-n</code> : Used to suppress trailing newline.</li>
<li><code>-e</code> : Used to enable interpretation of backslash-escaped character like '<code>\n</code>'.</li>
</section>

<section>
<h2>3. <b>mkdir</b> : Create a new directory</h2>
<p><code>mkdir</code> is important one if you are wondering how the people in old time used to create directories without <b> Graphical User Interface(GUI)</b>. So, here is the command used to create new directories.</p>
<ul>
<li><b>Syntax</b> : mkdir [option(s)] [directory_name]</li>
<li>Command Options: 
<ul>
<li><code>-p</code> : This option is used to create all directories at once including parent and child directories.
Ex : <code>li -p a/b/c</code> will create folder named <b>a</b> at present directory and <b>b</b> inside it and <b>c</b> inside <b>b.</b></li>
<li><code>-m</code> : Used to create directory along with which we can specify modes we want to give to that directory. Ex : <code>mkdir -m a=rwx new</code> means that create a folder named <b>new</b> at present directory and give it read, write and execute permissions.</li>
</section>

<section>
<h2>4. <b>touch</b> : Creates a file</h2>
<p>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 <b>"myFile"</b>. So, you will write <code>touch myFile.txt</code> in  command line. Also, it can be used in cases where we want to overwrite data or change timestamp of a file.</p>
<ul>
<li><b>Syntax</b> : <code>touch [option(s)] [filename(s)]</code></li>
<li>Command Options :
<ul>
<li><code>-a</code> : Change the access time only. Ex : <code>touch -a newFile.txt</code>. This will change the last access time of that file to current time.</li>
<li><code>-m</code> : Change the modification time only. Ex : <code>touch -m newFile.txt</code>. This will update the creation time of the file that means it will change the time the file was created to the current one.</li>
<li><code>-c</code> : If the file do not exist, do not create it. Ex : <code> touch -c textFile.txt</code>. This says that if the file named textFile.txt is present then overwrite it otherwise do not create a new one.</li>
<li><code>-r</code> : Use the modification and access times only. Ex : <code>touch -r myFile.txt newFile.txt</code>. This says change the timestamp of newFile.txt with that of myFile.txt.</li>
<li><code>-t</code> : Creates the file using a specified time. Ex : <code>touch -t YYMMDDHHMM.SS newFile.txt</code>.</li>
</section>

<section>
<h2>5. <b>pwd</b> : Print working directory</h2>
<p>Here comes the another important command in bash that you will often use while working with directories that is <code>pwd</code> command. As, the name suggests it prints the current directory you are in.</p>
<ul>
<li><b>Syntax</b> : <code>pwd</code></li>
<li>Command Options : Command options aren't used with pwd.</li>
</section>

<section>
<h2>6. <b>cd</b> : Change Directory</h2>
<p>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 <code>cd</code>. As the name suggests it is used to move between directories</p>
<ul>
<li><b>Syntax</b> : <code>cd [directory_name(can include subclasses seperated by '/')]</code></li>
<li>Command Options : cd typically do not use options.</li>
</section>

<section>
<h2>7. <b>mv</b> : Move or rename Directory</h2>
<p><code>mv</code> command is another life saver while working with directories. Sometimes we want to rename a file then <code>mv</code> 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.</p>
<ul>
<li> Syntax for <b>renaming</b> : <code>touch [old_name] [new_name]</code>. Here old name means the name before renaming and new_name means the name you want your <b>file/directory</b> to change to.</li>

<li> Syntax for <b>moving</b> : <code>touch [directory_you_want_to_move] [destination_directory]</code>.</li>

<li>Command Options : 
<ul>
<li><code>-i</code>(Interactive) :  It ask the user for confirmation before moving a file that would overwrite an existing file, you have to press <b>y</b> 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.</li>

<li> <code>-b</code>(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.</li>
</ul>
</ul>
</section>

<section>
<h2>8. <b>cp</b> : Copy Files and Directory</h2>
<p><code>cp</code> command is another life saver while working with directories. Sometimes we want to make a copy of file/directory then <code>cp</code> can be used. This command makes a copy of file(s)/directory(s)</p>
<ul>
<li><b>Syntax</b> : <code>cp Source_file Destination_file/directory</code></li>
<p>In case Destination file/directory is not present it will create a new one else it will overwrite it.</p>
<li>Command Options :
<ul>
<li><code>-i</code>(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.</li>
<li><code>-b</code>(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.</li>
</ul>
</ul
</section>

<section>
<h2>9. <b>rmdir</b> : Remove Directory</h2>
<p>Like <code>mkdir</code> is used to create directory(s),<code>rmdir</code> is used to remove directory(s).</p>
<p><strong>Note</strong> : <code>rmdir</code> is used to only delete empty diretory(s). But, don't worry we have an alternate to remove non-empty directory(s).</p>
<ul>
<li<b>Syntax</b> : <code>rmdir [directory_name(s)]</code></li>
<li>Command Options : <code>-p</code> - remove the child directory if empty otherwise gives error and then also deletes the parent directory.</li>
<p>Ex : <code>rmdir -p a/b</code></p>
<p>It will remove <b>b</b> if it is empty and then removes <b>a</b>.</p>
</section>

<section>
<h2>10. <b>rm</b> : Remove Here</h2>
<p>Here is the command which i was talking about earlier(<code>rmdir</code>). <code>rm</code> command is used to remove references to files, symbolic links and others. It is generally not used with <b>directories</b>, but in case of non-empty directories we use this command.</p>
<ul>
<li><b>Syntax</b> : <code>rm [option(s)] [file_name]</code></li>
<li>Command Options : 
<ul>
<li><code>-i</code> (Interactive Deletion) : Like in case of <code>cp</code> and <code>mv</code>, when we use this option it asks for confirmation before removing each file. Press <b>y</b> for yes and <b>n</b> for no.</li>
<li><code>-f</code> (Force Deletion) : If our files are write protected then we use <code>-f</code> option to bypass security check and forcefully delete the file.</li>
<li><code>-r</code> (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 <b><q>How to delete non-empty directory</q></b>.</li>
</ul>
</ul>
</section>

<section>
<h2>11. <b>cat</b> : Read a file, create a file, and concatenate files</h2>
<p><code>cat</code> command is used to display, combine copies and create new file(s).</p>
<ul>
<li><b>Syntax</b> : <code>cat [option(s)] [file_name]</code><h2></li>
<li>Command Options : 
<ul>
<li><code>-n</code> : This options display the line number along with the contents of file.</li>
<p>Ex : <code>touch -n new.txt</code>. Here new.txt contains </p>
<pre>
My name is Neeraj
My hobbies include listening to music
I love coding
</pre>
<p>So, this command <code>touch -n new.txt</code> will show the output as</p>
<pre>
1 My name is Neeraj
2 My hobbies include listening to music
3 I love coding
</pre>
<p><strong>Now try running this without option <code>-n</code> and you will see that it will now show content only and not the line number.</p>
</ul>
</ul>
</section>

<section>
<h2>12. <b>exit</b> : Exit out a directory</h2>
<p><code>exit</code> command will terminate the shell and stop all the running tasks and if you are in remote SSH shell, it will log you out.</p>
<ul>
<li><b>Syntax</b> : <code>exit</code></li>
<li>Command Options : n/a</li>
</ul>
</section>

<section>
<h2>13. <b>clear</b> : Clears the terminal window</h2>
<p><code>clear</code> 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 <b>up arrow button</b>.</p>
<ul>
<li><b>Syntax</b> : <code>clear</code></li>
<li>Command Options : n/a</li>
</ul>
</section>

<section>
<h2>14. <b>history</b> : List your most recent commands</h2>
<p><code>history</code> command is used to display all the previous commands used in terminal</p>
<ul>
<li><b>Syntax</b> : <code>history [option]</code></li>
<li>Command Options :
<ul>
<li><code>-d</code> : This option can be used if you want to clear a particular command from history</li>
<p>Syntax : <code>history -d event_number</code>. Event number can be found using <code>history</code> command.</p>
<li><code>-c</code> : This command option can be used to clear all history.</li>
<p> Syntax : <code>history -c</code></li>
</ul>
</ul>
/section>

<section>
<h2>15. <b>chmod</b> : Sets the file permissions flag on a file or folder</h2>
<p>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 <b>(0-7)</b> to change the permissions. For example, <code>chmod 777 my_file</code> 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.</p>
<ul>
<li><b>Syntax</b> : <code>chmod 777 new.txt</code></li>
<li>Command Options : <code>-v</code> (verbose) : Shows changed objects. In above command if you add <code>-v</code> option then it will output new.txt and will give access to everyone.</li>
</ul>
</section>

<p>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.</p>