| technology | system admin - Erik Mikac
6 Bash Commands You Can't Live Without
For the uninitiated, the bash terminal can seem intimidating. While the world at large has moved on to Graphical User Interface (GUI) systems, there are still plenty of uses for the Bash terminal. Bash can be used to grant permissions, organize directories, and serve as a primary means of navigating a computer. For instance, most of the time virtual machines in AWS, Google Cloud, and Microsoft Azure are spun up without a working GUI. In these instances, you simply have to lean on your Bash scripting experience to get anywhere at all.
With all that being said, it is critical to have a working understanding of Bash before taking the Linux+ certification test or working in a career that even touches on Linux. Before we start looking at different Bash commands. let's first look at what exactly Bash is.
What is Bash?
Bash is a Unix shell and command processor created in 1989. Bash stands for Bourne Again Shell, and is a nod to the Bourne Shell, which is the Unix shell it sought to replace. A Unix Shell is a command prompt that provides low-level access to a Unix operating system.
So to break it down, there are many different command languages that can be used as a Linux Shell. Bash is by far the most common, but Apple for instance pre-installs zsh onto each new Mac. And for the purpose of this tutorial, we will be using, zsh, the Mac Unix Shell.
To top it off, they all look pretty much the same: a black screen with the file path on the left, a command line on the left, and a $ sign (or some other symbol) to demarcate the two. Now that we have an understanding of what Bash is, let's look at some different commands to navigate a Linux operating system and manipulate the files therein.
What is ls?
There is no bash command more fundamental than ls. The name is short for list and does exactly what you would expect: display a list of files and directories. Ls works swimmingly coupled with another command called cd, i.e Change Directory. Ls will display a list directories like so:
Notice to the left of the % is test-dir—that's the directory we are in. Below that are all the files and directories located in test-dir. If we want to dig deeper into this directory, simply use the cd command on yet-another-directory to arrive at your destination.
PRO TIP: When typing out the name of a directory to cd into, simply hit the tab key and it will auto complete!
As if what we covered were not exciting enough, there is even more that ls can do for us. So let's look at some of the command-line options we can attach to ls to make it more useful. First let's briefly define what a Command Line Option even is, and especially with regard to ls.
What is a Command Line Option?
A command line option is a modifier attached to the end of a linux command to provide enhanced functionality. For example, on ls, adding -alt to the end of the command is especially useful.
As you can see, using ls -alt looks like ls on steroids. Let's break down what it does.
Ls — displays a list of files and directories
-a — displays "hidden" folders that begin with a period. For example, .bash.
-l — output the files in Long Format. Long format displays the file mode, number of links, owner name, group name, number of bytes in the file, the date with an abbreviated month, and the file name. In this instance, the file mode is the list of folks who can access the file or directory. The format is a little esoteric, but all it essentially means is who or what groups have read, write, or execute privileges on a file.
-t — Sorts the files by time of modification. So the most recently modified will be at the top of the list. Otherwise, linux sorts by dictionary order. That means it only considers blank and alphanumeric characters.
Now that we've warmed up to linux command with ls, let's take a look at grep. It's quite possibly one of the most useful Linux commands.
What is Grep?
Grep is a utility that allows a user to search a text file based on a specific pattern. Often these patterns are Regular Expressions, but they can also be regular words. A Regular Expression is a standardized pattern of characters that allow a computer to search for given texts that match the given pattern. It's perfectly okay if you're still a little confused by grep. Let's take a look at a couple examples to provide its usefulness.
For these examples, I downloaded Crime and Punishment By Fyodor Dostoevsky from www.gutenberg.org. Here is a link to the exact text. Thank you Project Gutenberg!
With the power of grep we are able to easily traverse this vast tome and look for specific patterns of text. In the below example, grep is searching for the word "Crime", in the book Crime and Punishment.
In this example we executed the following command: grep —color=always Crime Crime\ And\ Punishment.txt. Color=always highlighted the words to make them easier to read.
It looks like it shows up five times…probably not as often as you'd have expected. However, if we searched for crime — lowercase — it would show up far more often.
PRO TIP: If you mess up writing a command, just press the up-arrow on your keyboard to get back to the previous command. Less writing = more productivity.j
Since "Crime" with a lowercase occurs so often in the book, let's use the -c command-line option for grep. Instead of displaying the results, it will simply count them and return the results. Here is an example of that:
As you can see, lowercased crime shows up ten times as often as uppercased crime.
So far we've discussed how to display files and directories, and parse them with grep. But what if we wanted to create a directory. That can be done with mkdir, so let's jump right in.
What is Mkdir?
Mkdir allows a user to create a directory. It's about as simple for that, however there are a couple options that can be used to make life easier. Remember when we used ls -alt to view files with the permissions displayed all the way to the left? Well, with mkdir -m, those permissions, or modes, can be set upon creation.
For instance, if we wanted to create a directory where everyone could read, write, and execute the information, it would be written like so: mkdir -m777 some -directory. Example below.
Notice I used mkdir -m777 to create a directory called rwx-dir. (Read, Write, Execute Directory.) 777 in this instance it means that all users have the ability to read, write, and excuse the file or directory. This is made explicitly by displaying rwxrwxrwx on the directory.
Linux file permissions is a fairly in depth coverage, and everything you would ever need to know about it is covered here. The next command we are about to look at is equally useful: mv.
What is Mv?
The Linux command mv allows the user to do two things: rename a file or directory, or move a file or directory to another location. We'll discuss both examples, starting with the renaming of a file.
PRO TIP: If you want to quickly create a file in linux, simply type touch <your-new-file-name>
Let's unpack these commands a little. First, I used touch to create a file called "a-file". Then I used ls to verify it was created. Next, I used our new command mv to rename "a-file" to "a-renamed-file." So to use mv, type mv <the-thing-you-want-renamed> <its new name>
Lastly, I used ls again to prove the name changed.
As discussed earlier, mv can be used to move a file or directory. Let's take a look at an example of that.
First we start by calling ls to see what is in the current CBT-NUGGETS directory.
There are three directories: bash-commands, mv-examples, and rm-examples. However to make it more organized, it would make sense to move the two example directories in the bash-commands directory.
To do that, we use the mv command. That example may be interesting because we use a wildcard. (a.k.a a Kleene Star) A wildcard will look for any character match, as long as it ends with the word "examples". So in this case, it will move both our example directories into bash-commands. Bash-commands is the second operand, i.e the location we're moving the directories to.
Lastly, to prove the directories have moved we cd into bash-commands and call the ls command to show the directories moved.
PRO TIP: To display the current directory, type pwd and hit enter. To move up just one directory level, type cd .. and hit enter. (To be clear, that is TWO dots after cd.)
What is Rm?
Last but not least is the rm command. The rm command removes any file that proceeds the rm command. This is a fairly straightforward (and slightly dangerous) command, but let's still go over a quick example.
The first thing I tend to do when I'm about to execute a Linux command is called pwd just to get my bearings, so to speak. Then, I create an example file by calling touch. Next I call rm my-file to remove the file. The Shell then prompted me and I promptly typed "y" for yes. Lastly I call ls to verify the file has been removed.
There is a good possibility that you will not be prompted when calling rm. If you would like to be prompted, then type this command into your Shell: alias rm='rm -i'. Alias command allows us to pre-define a certain option we would like to use with rm. In this instance, -i means that the command prompt will require the user to press "y" to make sure they really want to delete the file.
PRO TIP: To see all of the available options for any command, type man <the-command>. Man is shot for "manual".
We covered A LOT of Linux ground, and if you actually made it this far, you should feel proud! Linux Shells are a remarkably powerful tool. Having a thorough understanding of all the commands will give you a solid foundation for continued growth in the IT world.
The more IT concepts you learn, the more you will begin to realize that Linux controls everything. So if you can learn to control Linux, then you can steer your destiny in any direction you choose!