So you have heard about Bash before but all those tech terms and tutorials aimed for terminal savvy people confuse you, huh? You keep getting lost on Wikipedia articles or have no idea where to start your journey? This article the solution to all your problems!
1. What is Bash?
Let’s start with what a shell is. You know how you can give your operating system orders to do this and that, and it will do so? Well the communication between your operating system and you is made possible by a shell. A shell is a user interface that allows you to, without getting lost in all that code and confusing programs, access to the services provided by your operating system.
A shell can be in two forms; a command line interface (CLI) or a graphical user interface (GUI). As the name suggests, a graphical user interface will give you a graphical panel to access your operating system (OS). You can think of this as the old fashioned way we use windows, for example, when we would like to create a folder we can simply right click on the desktop. Then we will be greeted by a box full of actions you can pick from, which is a graphical interface. If you click on the New, there will be another box with options listed. You can choose New Folder, and through this graphical interface windows will do the action of creating a new folder for you. A command line interface, on the other hand, will have you write a line of command and follow that command as you hit enter. In this type of shell you will be greeted by a simple black screen. There, you can use the pre-determined keywords to write a command of “create a new folder” and when you hit enter, the folder will be created.
You will find that almost all operating systems use both graphical and command line type of shells, giving the user the freedom to choose. However, as you learn more about the command line shell, you will find that it’s simpler to use than the graphical one -and looks sexier! There are alternative command line shells you can set up on your OS, but most will come with a built in one. For Windows, it’s Windows PowerShell and for Unix, it’s Bash.
Bash is, just like PowerShell, the name for both a shell and a command language used for that shell. It’s the default for most Linux distributions, most releases of Apple’s macOS, and you will find that there is even a version available for Windows 10.
Notice: One of the biggest advantages Bash has over PowerShell, other than having a wider use, is that unlike Bash PowerShell does not come with an SSH command by default. SSH stands for Secure Shell, and is a program that is used to connect to remote server or machine securely. A SSH command is used to start the SSH program, and on PowerShell you would have to use a third party program called PuTTY for that task while in Bash it will start automatically so no other work will be needed.
2. Basic Commands in Bash
Using a command line shell can be a bit scary for that unlike a graphical user interface shell, there is nothing on the screen. Each line of task you will write before you hit enter on the shell is called a command. In order to use a command line shell like Bash, you are expected know exactly what command to write on the screen. How you write a command will differ based on what language you use, because the keywords you use to perform the same task will differ on each language. In this section you will find some of the basic commands and keywords used to write those commands in Bash language.
When you open the terminal, the very first thing you are going to see is this and a blinking cursor next to it. This line here shows you which machine you are working with. It may not be very necessary when working with one computer alone, but when multiple machines and terminals are open (remember how we were able to do this via SSH as mentioned above), this will prevent confusion.
The dollar sign next to the name is there to tell you that you are not running the terminal as an admin at the moment. Which means, you will not be able to run some commands without a permission from the admin.
Now I want you to imagine you created a folder through Bash here. Where do you think the actual folder will go? Whatever directory the Bash terminal is currently working under, is the answer! Have no idea which directory you are working under? Simple! If you are under Home directory, which is where Bash will put you in automatically, the name section will look as listed above. However, if you are let’s say under Documents directory, it will look something like this:
Notice how the tilde (~) is missing? That tilde simply represented the Home directory and now that you are under Documents directory it changed.
So you are apparently under a folder named x, but have no idea where this x folder is? Run this command! It will print out the complete path of the directory you are currently working under.
This command, short for “list storage”, will give you a list of everything under the current directory you are currently in.
But what about if you are under the documents directory and you would like to see what is under the images directory? Do you have to go through the graphical interface and click on the images directory? Nonsense. You simply change this command as seen below:
Notice: “ls -a” will give you all that is under the directory you are currently in, including the hidden files and directories.
Notice: “ls -l” will give you a long list of details about whatever you have inside the directory you are currently under.
This command will help you change the directory you are currently in. You can simply type the path of the directory you want to work under as the following example:
Notice: When you have a directory inside another one as shown in the example, you can think of it as boxes inside other boxes. If you want to go back to the Documents directory, meaning if you want to go to an outer box or one level up, you can simply use three dots as the example below:
Tip: If you want to go to the home directory, you can simply use the tilde instead of writing a whole path, as shown below:
Short for push directory, this command is used to jump into whichever directory you would like to work on. Take note of the push keyword if you are familiar with stack. You can run this command by writing the name of the directory you would like to jump into next to it as shown below;
Once you run this command, the directory you went to and the directory you left will be printed on the screen as well. This will come in handy when you want to use the popd command.
This command will take you the previous directory you were working on.
Linux will automatically know the type of every file in a directory when you list them with ls, but you won’t, right? Let’s say that you have a file named file1 and you would like to know whether this is a text file, a jpeg file, or even a directory. You can simply type file followed by the name of the file, and hit enter to reveal the secret.
When you need to edit a certain file but have no idea where it is, this command comes in handy. You can simply type the name of the file next to it and hit enter. And good news, even if you can’t remember the full name of the file, you can still type in as much as you remember and it will still be there to help you.
Notice: This command will basically work on a database to find things. And if it doesn’t work on your cmd chances are you need to update the system. You can simply do that by typing the command below and hitting enter;
You have read above about how a command might not be installed, right? In order to find out if and where a command is installed, you can simply type the command next to this keyword and hit enter.
Bash will keep track of every command you type on the terminal. You can reach the previous commands you have typed by simply using the tab key on your keyboard, but this will help you go one command back at a time. So if you would like to see, let’s say, the last 100 commands you have written; you can simply type this command and hit enter.
Pop quiz, what does ls do? Don’t bother switching from your terminal to your web browser and googling it! You can simply type whatis ls and hit enter to reveal the answer.
You know that there was a command that revealed what day it is, but just can’t remember what it was? Simply use this command with a relevant keyword, like apropos time or apropos date and pick from the list.
Short for make directory, you can type a name for the directory you want to create and hit enter to create it. If you would like to make 3 directories, type the names of each with a space in between and that’s it.
This command can be used to both create a new file, or update the last edit time of an existing file.
Short for copy, this command is used to create a copy of an existing file. Use this command with the format below;
cp <the address and the name of the file you would like to copy> <the address you would like your copy to be placed in and the name you would like to give your copy>
Note that if you don’t specify the address you would like to place your copy into, this command will create the copy inside the directory you are working under at the moment.
So we have created a copy of a file, but then messed up the original file, what’s next? This command allows us to write over one file with another one. So if you type mv file1 file2, whatever was inside the file2 will be replaced by whatever was inside the file1. When you run this command you will see that file2 is deleted, but all of it’s content is now inside file1. Short for move, this command will simply move the content.
Short for remove, this command will give you the power of removing huge amounts of data. But be careful! Once you remove something on terminal with this command, it won’t go to the trash can as it would when you remove something on a GUI. You won’t be able to retrieve the removed item from the hard drive. This may, though, come in handy when security is an issue with the files you delete.
Remove command can be used with a star (*) to remove multiple things. If you type rm *, for example, it will delete everything within the directory you are currently in. Or if you type rm f*, it will delete all the files that start with an f within the directory you are currently in.
Notice: rm can only be used to delete files. If you would like to delete a directory, you would need to use this command as below;
rm -r directoryName
There is also a rmdir command for this use, however, you will find that this only works on empty directories.
Short for concatenate, this command can be used for two tasks: view a text file or add text inside a text file.
If you simply use it with the format cat fileName, you will get the content of the fileName file printed on the screen. If you do cat >> fileName, whatever you type afterwards (‘till you press ctrl + d, that is) will be added inside the fileName file.
Being a copycat, this command can also be used to copy the content of one file at the end of another. So if you do cat file1 file2, you will see the content of file2 pasted underneath the content of file1.
If you would like to view a file page by page, use this command with the filename. You can travel through pages with the space bar and exit the file by hitting the q key on your keyboard.
This command works just like more, but ironically, it’s more advanced. With less you can also use arrow keys to go down and up one line at a time, search within the file and all. Remember, less is more!
Nano is a text editor, one of many, for Linux. It’s very simple to use and most widely available in Linux distributions therefore it’s very popular. If you type the file name next to it and hit enter, it will open the file on terminal and allow you to edit it. If you would like to create a new text file simply type nano and hit enter. After you type down the content, as you hit exit, the terminal will ask you whether to save the newly created file or not. There, you can put a name on it and save it.
Want to run two commands within one line? Simply separate them with this little icon and you got it.
Short for super user do, this command will give you admin privileges. Keep in mind that it will ask you for your password first. And in order to exit this mode, type exit and run it.
Use this command to switch between different users on the same machine. Keep in mind that if you are not on sudo mode, you will need the password of that certain user in order to log in. From this point on you can access and change all their files. This command uses the format below;
su — userName
This command will show you the names of all that has accessed your system, whether from the same machine or remotely. It may come in handy if you suspect someone’s spying on you!
This command will give you a bunch of information about your account.
Linux will give 3 kinds of permissions to work on for each file: read (r), write (w), execute (x). Now these can be determined automatically, or you can decide which user will have which permission using this command. Keep in mind that this command is short for change mode, so that it will be easider to remember.
If you type ls -l, you will see which user has which permissions already. If you would like to change things, use the formats below;
chmod +x file1 Makes file1 executable by all
chmod 700 file1 Makes the file readable, writeable and executable only by the original user who created it
chmod 744 file1 Makes the file readable, writeable and executable by all
chmod 644 file1 Makes the file readable, writeable and executable by the original creator, but ony readable by others
3. Other Controls
Sometimes, a process you run on terminal might enter a loop or be endless or simply take too long. In such cases, you might want to kill (stop and exit) the process by hitting ctrl + c.
If a program or app you run on your machine freezes or causes an error, though, the trick above won’t work. In order to kill that app or program you can use the terminal with a killall command. For example, type killall firefox and you will see firefox and all programs that use/run on firefox shut down.
In order to close the terminal, you can either type exit and run that command or hit ctrl + d.
Bash is a shell and a terminal that is available on almost all Linux distributions, MacOs, and now even Windows. Once you are familiar with it, it will be so much easier to control your OS through your terminal rather than dealing with a GUI. So dig in and have fun!