Bash It Like a Pro: Must-Know Bash Commands for Every Programmer

Bash It Like a Pro: Must-Know Bash Commands for Every Programmer

Countless superhero comics feature a common trope where the plain-looking guy, such as Peter Parker in Spiderman, Barry Allen in Flash, Bruce Banner in Hulk, and many others, happens to be the strongest character in the story, which is something I particularly enjoy.

Spider-Man - Wikipedia

Source: Wikipedia

In the Tech Industry, we have a superhero of our own. He goes by many names, some call him The Terminal some call him Bash others call him The Shell. He is almighty, bugs and errors fear him, and just his name is enough to send chills down their spines.

So wouldn't it be cool if you knew how to use it?

Why should I learn Bash Commands

  1. It makes you super cool and awesome. That would be enough to make me learn it.

  2. It makes life much easier. It might not seem so right now, but once you get used to the terminal there is no going back to the GUI (Graphical user interface).

  3. If you code, you will encounter the terminal in some or the other form, so learn it now!

  4. It's fast. Let's say that there is a folder containing 10000 files of different types (ppt, mp4, png, jpeg etc.) and I tell you to delete all the files with the ppt extension. Good luck doing that with a GUI. You can do that in the terminal with just three words.

Getting Ready

For those using a Linux or Mac computer, the terminal is likely pre-installed and can be easily located through a search. Windows users, on the other hand, can install Linux on Windows with WSL to have access to the terminal.

Linux File System

Before you start playing with the terminal, you need to know a little bit about the Linux File System.

linux-filesystem.png

Source: Linux File System Hierarchy

You see your whole computer is divided into directories, you can think of them as folders that contain more files and folders.

The "/" is called the root directory (The words "directory" and "folder" are interchangeable), it is the starting directory as you can see from the above diagram, which contains everything on your system.

The Terminal

So, let's get started.

Your terminal might look a little different than mine, but that's completely fine all the commands will still run the same.

Wait Wait Wait!!

What are you doing are you just going to read this article? No No No we can't have that open up the terminal and follow along, I assure you it will be a lot of fun.

So what is sushant@archlinux ~

sushant is the username in your case it would be your name.

and archlinux is the hostname (the name of your computer)

so sushant@archlinux is trying to tell us that, sushant (the user) is using archlinux (the computer). Now that might seem a little silly to you. Why do I need to know this? It's easy for you to tell which computer you are on, but in most practical cases where the terminal is used, that's not the case. Someone might be trying to access a remote server via the internet or multiple people might be using the same server, in those situations, it is nice to know those details.

what is the tilde "~" for? That's what our commands will reveal so let's get started.

Commands

Use the clear command to clear your screen if it gets too messy.

Use the tab key to auto fill the command.

Press Ctrl + Z or Ctrl + C or q, if you get stuck anywhere and can't come out.

pwd (print working directory)

Output the current working directory

Do you recall me mentioning how your computer is organized into directories and follows a certain structure? It's helpful to know where you currently stand in this structure, which is where the pwd command comes in. This command informs you of your present working directory or current location.

The starting / is the root directory, followed by the home directory which is followed by the sushant directory (In your case it might be your name).

That's your starting point whenever you open the terminal you will find yourself here.

ls (list)

list directory contents

It tells you what contents the directory holds.

What if I want to know the contents of the Documents folder? for that, you can do ls Documents/ and it will tell you about the contents of the Documents folder.

My brother is very sneaky, so to trouble me he hides my files. What he doesn't know is, that the terminal is very powerful.

To see all my hidden files, I can do ls -a (where -a stands for all)

NOTE : See all those files that just showed up, all of them have one thing in common. They all start with a dot (.), in Linux, any folder whose name starts with a dot is hidden.

My terminal is color coated the bold blue color represents the Directory/Folder and the light white color represents files.

cd (change directory)

changes your location to a different directory

To move around, we use the cd command. To go inside of the Downloads folder all you have to do is cd Downloads.

See how that tilde (~) changes to ~/Downloads to show us our current location.

So it's trying to tell us that, we are in the Downloads folder, but where is the Downloads folder? It's in the /home/sushant/ directory, which means that the (~) is nothing but /home/sushant/.

That's correct ~ is a shorthand that we use to represent /home/username/.

Going back to the previous directory:

To go back to a directory you can do cd ..

.. -> represents the previous directory

. -> represents the current directory

That's why when I do cd . , I don't go anywhere but rather remain in the same directory.

--Aside: Absolute vs Relative Path

A relative path indicates the position of a file in relation to the current directory, while an absolute path specifies the location of the file starting from the root directory.

Going to the Downloads directory using absolute path:

Going to the Downloads directory using relative path:

pushd and popd

pushd - push the directory to the directory stack

popd - move through directory stack

To demonstrate how the pushd command works, I have created some folders in my ~/Documents/Hashnode/ Directory.

home -> Documents -> Hashnode -> File1 -> FIle2 -> File3 -> File4

Now let's say that I am doing some work in this directory and I want to go to the Downloads folder to check something. You can surely do cd ~/Downloads, but then you will lose your current directory location and will have to do cd Documents/Hashnode/File1/File2/File3/File4/, to reach your current location.

So we need to create a checkpoint, like how we create in games.

we do pushd ~/Downloads/

Now we can do whatever we want in the Downloads directory, and to go back to our checkpoint we do popd .

whatis and man (manual)

let's say that you forget what a specific command does. You can do whatis commandsName and it will give you a small description of what the command does.

NOTE : Some commands will show :nothing appropriate when you type the above command, that's because they are pretty straightforward.

We all have that one friend who goes like "mannnnnnnnnn you know what a bad day I had today" and then goes on to explain his whole day.

We have one such friend in the terminal as well known as man (manual). This command is very useful, its documentation at your fingertips.

man commandName will give you a long description of whichever command you want.

man ls

mkdir

make directories

It lets you create directories.

for creating multiple directories at once you can do mkdir FileName1 FileName2

-- Aside: Spaces

let's say that I want to create a folder named King Kong that's a single word but if I do, mkdir King Kong

it will create two folders instead of one, that's because it does not understand that King Kong is a single word.

Ways to overcome this:

  1. Putting file name in single quotes (' ')

    The quotes won't be visible when you see the file in the GUI, in the terminal they are being shown to tell us the user, that King Kong is a single word.

  2. Using ( \ )

    To create a file with the name I am great you can use :mkdir I\ am\ great/

touch

the touch command lets you create files.*

NOTE : * The job of the touch command is not exactly to create files, but rather to update the last modified time on them. Go to any folder and right-click on it, and go to properties you will see something like Modified there, which tells you when was your file last modified. The touch command lets you update that time. If you do touch fileName it will update the Modified time. What if that file doesn't exist? In that case, it will create that file and that's how we were using it, to create files.

cat (concatenate)

The cat has multiple uses, so let's dive in:

Seeing the contents of a file: if you do cat fileName, it will show you the contents of that file.

Writing something into a file: if you do cat >> fileName, it will give you a blinking cursor to write stuff in at the end of the specified file.

(Initially file2 had nothing in it, so the cat command gave us no output)

Joining the output of files together: It does not modify the files, but rather just lets you see their output together.

Joining files: If you do cat file1 >> file2, it will append (add (something) to the end of a written document. ) the contents of file1 to file2

if you don't pass in any files and do cat >> file2 it will be case 2 (Writing something into a file).

cp (copy)

copy files and directories

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY

cp file1 file2 will copy the contents of file1 into file2.

If you copy a file to a file that does not exist, it will create that file.

mv (move)

move (rename) files

If cp is like copy then mv is like cut.

In my Downloads I have a Dummy file and I am moving it to my Practice Folder.

mv command can also be used for renaming files.

rm (remove)

⚠️ While using the rm command, be a little careful. Create dummy files to play with the rm command because unlike deleting files in the GUI. There will be no recycle bin for the files deleted with the rm command they will just disappear.

rm file1 will delete the specified file.

for removing directories you have to do rm -r directoryName

Some simpler commands

cal : shows you the calendar

file : tells you the type of file

whoami : prints effective user name

history : shows you all your previously used commands

(press q to quit)

And That's All !!!!

You did it, feel proud of yourself. You just did what most people never do, that is learning the terminal commands, so Congratulations!

Now go rest up drink some tea or coffee, and don't forget to use these commands or they will just evaporate from your brain.

Leave your thoughts down below, would love to hear back from you folks !!

Some awesome Resources:

The Linux Command Handbook – Learn Linux Commands for Beginners : Awesome people over at freeCodeCamp created this awesome handbook, which has a bunch of commands and short explanations do check it out.

Beginner's Guide to the Bash Terminal : If you like the video format more, you can check out this awesome video by Joe Collins.