In part 1 of this series we discussed why we want to learn Bash and in part 2 we walked through what Bash can do. In this post, we will be diving into how to use Bash, including basic commands, leveraging common tools, and how to make Bash your own.
If you have never touched a terminal before, I know it can be daunting and it feels like there are a million commands with a million possible options and some of them are not really readable by humans. Fear not, the underlying concepts are ones you are already familiar with if you use a computer on a semi-regular basis.
Where are we?
The first thing to remember when in a command line is that Bash will try to perform any actions relative to the folder you are in. Knowing where you are is important. To ask Bash to tell you where you are, type 'pwd' (without the ‘ '). This will display the present working directory. For example:
You might be wondering, what is all that stuff before the ‘pwd’ in that picture? That is the default command line prompt, which in a MacOS Terminal tells me the ‘ComputerName:CurrentDirectory YourUserName’. The ‘$’ just tells you where you should type. Many examples of commands you will find online will start with a ‘$’ to inform you where the command you want to copy starts.
Now that I know where I am at, I can ask Bash to list directory contents with the ‘ls’ command.
There are a lot of variations on how you can display this information through the use of options, which can look like this:
Using the option ‘ls -a’ means List Directory Contents including the ‘hidden’ folder which starts with a ‘.’ such as my ‘.git’ folder in this scenario. The items ‘.’ and ‘..’ are actually very handy special characters that are always present in the folder. The ‘.’, pronounced ‘dot’, means ‘this current directory and the ‘..’ or ‘two dots’, means ‘one directory level higher’.
Let’s not worry about all the options quite yet. Let’s stick to basic commands for navigation and later on I will tell you how you can find all the information about all the options you could ever want.
For Bash, moving means change directories, or ‘cd’. We just need to tell Bash where to go. In this case, I am asking Bash to move down into the ‘folder1’ directory.
And remember that 2 dot, ‘..’, from earlier? This is extremely handy for moving back up a level or more. Since every folder has this and it always means the same thing, we can always count on ‘../..’ meaning the directory above the directory above this one. The ‘/’ is a special character that is used in paths.
Interacting with things
In part 2, I talked about how the basic idea is Bash gives you a way to copy, move, create, delete, and even execute the files.
To make a copy of a file, we tell bash to ‘cp’ the file and what to call the new copy.
To move a file, we will tell Bash to ‘mv’ it. Not only can we move the file between folders but we can move the file to the same location with a new name, giving us a very easy way to rename things.
Creating files can be done a few different ways, such as by opening a text editor and saving a new file. But if you just need to create a new document that you will later edit, you can use ‘touch’ command.
To make a new directory, what we commonly call folders, we need to tell bash to ‘mkdir’.
Deleting a file means telling Bash to remove it or ‘rm’ the file.
Folders need a little more than just ‘rm’. We need to tell Bash to remove the folder and everything contained therein, which requires the ‘-r’ flag. I highly recommend also using the ‘-i’ flag to force a prompt for me to confirm that I really want to delete things.
To open any file in the ‘native application’ for that file format, you can just try ‘open’ and the name of the file. For example, ‘open doc1.txt’ in our scenario would open the text file in the default text editor for the system. If you try to open a .csv file, Bash will do its best to open it in a spreadsheet tool of some form. If there is an error on opening, Bash will likely tell you what went wrong.
Once you understand how to navigate and manipulate files with the command line, using the GUI will start seeming slow for many use cases by comparison.
So many options
In the ‘rm’ and ‘ls’ commands I introduced some options, which I needed to perform specific feats. This is where a lot of people start to worry about memorizing a long list of possible options. Outside of memorizing a handful that are very handy, most users, like me, need to look up options when doing more complex things. And one of the best parts about Bash is its documentation.
For any command, you can simply ask bash to let you read the manual by saying ‘man <command>’. For example, I know ‘ls -a’ because ‘man ls’ tells me that is the option for the job:
Every built-in tool has an entry like this. It is built in and works offline. While it might sound like some dry reading, it can actually be a very satisfying way to learn since you can test this out immediately and learn what options are the most practical to you. The deeper you go into specific use cases, ‘man’ will become an incredibly helpful ally.
A whole new world of tools
Now that we know the basics, we can now start utilizing the other tools Bash can offer. There are far more tools than I have time to write about here, but let me touch on a few of my favorites that I use every day. A quick search on any of these along with the keyword Bash will yield a lot of amazing resources.
‘clear’ - this simply clears the terminal screen.
‘history’ - this shows me the list of commands I have recently run.
The up arrow, which populates the prompt with the previous command. This saves me a lot of typing.
The tab key, which autocompletes. Instead of typing out ‘index.html’ in our previous examples, I could type ‘in’ and hit the tab key. Since there is no other file that starts with ‘in’, Bash assumes I want to autocomplete the line with ‘index.html’.
‘grep’ - this stands for ‘global regular expression print’. This is one of the most powerful text search tools humans have ever created. Learning this tool will let you handle ‘big data’ in amazing ways and let you build complex reporting tools from the ground up once you get the hang of it.
‘calc’ - this can turn your terminal into a handy calculator to do simple arithmetic or complex equations with ease.
Getting even more tools
That last tool I mentioned, ‘calc’, is actually not built in for most systems. It needs to be installed by a package manager. If you are on MacOS, you can likely use Homebrew. If you are a Linux user, there are a variety of package managers you can leverage. There probably is a tool for about anything you would want to accomplish.
Once you have the basics down and understand how to navigate Bash, the world of tools opens up even wider. The tools for your favorite CMS, Drush and WP-CLI, and platform-specific tools like Terminus suddenly make much more sense.
I know that is a lot of information in one post. I hope to inspire you to go explore this amazing tool of Bash and mold it to work for you. Next time, in the final part of this series, we can turn our attention to shortcuts, called aliases, and then unleash the real power of Bash, doing a lot of things with one command via a script.