Debian 8: Essential Bash commands

      No Comments on Debian 8: Essential Bash commands

Introduction

This will be a short article on a few essential Bash commands you should know to effectively use Debian 8 after a clean install without a graphical desktop environment. You’re going to learn basic file-system navigation, installing and removing programs and some basic process management. We will also look at a few options to each essential bash command. So lets start.

Bash is short for ‘Bourne Again Shell’ and is a derivative from ‘sh’ or common shell on older Unix systems. Bash is the main program responsible for user interaction between programs that use STDIN/STDOUT for interaction.

Listing files and directories

These are the most essential basic terminal commands you should really know because when working on a bare Linux system you are constantly using these to get around on your file-system. You also should familiarize yourself with coping, moving and tricks like renaming because it makes life a lot more easy in the world of Bash.

First of all, it’s important to know what your location is in your file-system. There is a simple instruction for that, pwd, It’s short for Print Working Directory:

It’s output will look like this:

It’s good practice to just quickly check where you’re at in your file-system before you start to perform other operations. So now you know where you are, now it’s time to learn ls, short for LiSt

You will get an output that’s something like follows

This will print out a list of files and directories in your working directory. There are a couple of neat tricks with this instruction that can make like easier. The following command adds an option and highlights directories by marking them blue. This way it’s a little easier to distinguish between files and folders. Just try it and you’ll see.

If you want to know the file sizes you could use another option:

That will return output like this:

However, sizes are returned in bytes, which isn’t really a problem with small files, but when the files get longer it’ll get annoying so we make it human readable like so:

That will return something like this:

Now that’s better! But there’s more. You can use ls to figure out who the owner is of a certain file or folder and what the file attributes are, such as read-only, execute rights and write privileges. Type the following command:

it will output the listing a little differently now because there’s now a lot of information about a fie or directory. It will look something like this:

Last neat little trick I want to show you is this:

It also shows hidden files and directories, it’s output will be a bit longer. I’m not going to show you what programs I use that create hidden folders in my home folder so you’ll have to see for yourself.

Navigating the filesystem

You can navigate through a file-system by changing the working directory. There is only one command for this, cd. Just type this

You will not get any output because using this command without a location is useless. So let’s create a directory first, enter the following command:

There, you’ve learned another command! But let us focus on changing directories first. Use the ls command to get a list to verify the directory has been created:

That will reveal your directory listing as follows:

It seems our folder has been created so lets now move ourselves to that directory using the following command:

You will not get any output from the command line except that now your prompt includes the working directory:

Notice that file and directory names are case sensitive. If the cd command returns an error stating that the file or directory doesn’t exist, you may want to check case sensitivity. Now, assuming you didn’t get an error you should verify that the working directory has changed. Use the pwd command for that.

Congratulations you’ve successfully changed the working directory, but how to I get back? That’s easy, type the following:

This should change your working directory back one level higher. This is what the ‘..’ means, it’s essentially the same as “go back one directory”. There is also the ‘.’, which is basically the same as “right here” or “current working directory. This last one is useful in certain copy or move operations I’ll address later.

You’ve just learned moving around the file-system using relative paths, which is the easiest to start with, but a neat thing is using absolute paths. I will cover this in an other article more deeply but for now it’s enough to just try it out a bit. Type this:

This cd command changes the working directory to the root of your file-system, it’s a very important folder but don’t worry for now. This is using absolute paths, its knowing what paths exist and getting there directly without traversing through all the directories and sub-directories first. So now we are at the beginning, now lets get back to our test folder using an absolute path:

Ofcourse, you would have to replace ‘phrozenbit’ with your own username. If no error showed it’s ugly face it’s safe to assume you’ve landed right back in our previously created test folder. It’s no problem if you don’t understand this at first, but just play around with this until you do understand. This is what learning is all about, trying new things.

Copying or moving files around, and deleting files

It’s important to know how to copy or move files around using terminal commands, as it’s a powerful tool. We will do a little experiment that will teach a few more commands dealing with creating files and writing content to it. For this we will need the test directory we’ve created in the previous chapter.

Make sure you’re in your home folder by typing the following neat instruction:

That weird little wave thingy should be above your tab key and you need typ hold shift to get it. That weird wave thingy essentially means “home”, so your instruction actually means “change directory to Home”

Next change to the Test folder we’ve created previously using the mkdir instruction as follows:

Remember, it’s case sensitive. Now we are going to create two more directories, one with the name ‘source’ and the other we will name ‘destination’. Don’t actually type the single quotes of course. Create the first directory:

and then create the second directory:

We will now create a file inside the fodler with the name ‘source’ and copy that to the directory with the name ‘destination’. First we need to move into the directory with the name ‘source’ as follows:

We are going to create an empty file using a tiny program called ‘touch’. It essentially reserves a block on the harddrive and names it, this is your very empty file. Type the following command:

Verify if the file has been created using the ls command. You can also verify if the file is actually empty by using the ‘more’ instruction. It should not output anything but go ahead and type:

Again, it’s case sensitive, get used to this because many file-names are constructed this way. Now we have to write some text to that file. We can use the ‘echo’ instruction for that and we can direct it’s output to a file by constructing a nifty instruction. Type the following command:

It’s getting a little bit more complicated, but this is a very basic method of writing contend to a file. There are of course user friendly file editors you can install that would make things a little more easy. Verify the contents of the file using this instruction:

This will print the contents of the file to your screen. It should read ‘this is a test’.

Now we need to create a copy of this file in our folder with the name ‘destination’, and while we are at it we will rename the file in the destination folder. The format of a copy instruction is “cp source-path destination-path”. Any relative or absolute path with a file is a valid source, and any relative or absolute path with a file is a valid destination. Write this instruction and hit enter.

What we did is we selected ‘TestFile.txt’ to be our relative source path, and we selected ‘../destination/Copy-of-TestFile.txt’ as our relative destination path. What happens with the ‘../destination/Copy-of-TestFile.txt’ part is that the program cp is going one folder back, then enters the destination folder and drops the file in that folder using a different name. You can perform all sorts of neat tricks with cp, you can for example be at some arbitrary location somewhere in your filesystem, copy some file from some source location using an absolute path to some destination using another absolute path. You can also mix up absolute paths and relative paths but this is something for my next article.

We are now going to verify the process of copying our file. Go back one folder using this command:

and then enter the directory with the name ‘destination’ using this command:

Once there just type ‘ls’ and hit enter. You’ll see that, if everything went according to plan, the file ‘Copy-Of-TestFile.txt’ should be there. Again, be aware that it’s case sensitive, I can’t stress the importance of that enough. We also need to verify the contents again, to see if something weird happened to it while copying, you can never be too sure.

This should give the same output as when you verified the source file at it’s source location.

Moving files works exactly the same as copying files when it comes to the source and destination format. Only when you move a file from a source directory to a destination directory, the file is deleted from the source directory. You can do the same steps as outlined above, only replace the ‘cp’ portion with ‘mv’.

Another neat thing to mention is that there isn’t a program for renaming files. One of the first things I learned is that you just have to be smart. There is nothing stopping you from moving a file with the name ‘something.file’ to ‘renamed.file’ within the same directory. The original file is moved to the same folder under another name, and the source file is deleted.

Removing files is relatively easy. There is an instruction named ‘rm’. We are going to remove the copy in our destination folder now to test this.

Easy as one-two-three. Verify to see if it’s really gone by using ‘ls’.

Installing and removing programs

Installing programs works by installing so called packages from a big online software repository. In simple terms this means you’ve got a big library of programs to choose from and install directly using a called apt. You can also remove programs with it, or search the online repository with it.

In this chapter I’ll show you the general methods of installing and removing software. We’ll first look at good practice, then we we’ll install a small program that we need in the next chapter. We’re also going to install a program and then remove it again so you can get a first glance at installing programs.

Before you install software you need to be logged into your system as root. Previous examples show that we’re logged in using a regular system account without root privileges. This is important because if you don’t need administrative permissions for the task you’re about to carry out ( browsing the file-system ) then you should not be logged in as root. You will read this more often in my tutorials as it is an important practice to keep in mind.

Installing software however requires administrative permissions so you need to be logged on as root. There are several methods to do so but I’ll cover that later in another tutorial. For now just use the su command to log on as root, here’s the instruction:

Your regular account will be switched with the root account after typing the root password. You should remember this password from installing Debian to your system. A good indication to look for is the hash-tag in the terminal prompt. I’ll go root and paste the output here.

Some good practice would be to update the local database of packages that are offered by the online mirror, and then upgrade all the previously installed programs before you start to install new software. This sometimes is very important, especially when you’re about to do some more advanced things like compiling programs, drivers or kernels. First you should update your local database using the following command:

You will see apt output some connection statistics like printed below:

My output is a bit long because I’ve got a system that supports both the 32Bit and 64Bit architecture and I’ve enabled the non-free and contrib repositories but your output should look somewhat the same.

Next up in line is upgrading your installed packages with newer versions. This is what is called an upgrade, it removes the old and installs the new. On other operating systems most of the installed programs are usually patched wich I personally find silly for installed programs. Here’s the instruction:

Apt will now list a couple of packages that have newer versions available in the online repository and asks if you want to proceed, answer by typing the ‘y’ button followed with Enter. Apt will connect to the online repositories and starts to download the newer versions. Depending of the scale of the upgrade this may take a few minutes up to 30 minutes or even more. The output of this can be quite long so I will not paste it into this tutorial, you’ll get the idea. It’s advised not to do anything else on the computer until the upgrade is complete, especially on a system with encrypted storage facilities.

Now that we are done updating the local package database and upgrading the installed packages we can search for a program. We need something that shows some system statistics like CPU usage and Memory usage, and a process list, so lets search for that using apt. It’s a matter of finding a keyword for a program, such as process-viewer, to see if there is a package that matches the keyword. Use the following command:

This will most likely give a list with a few programs that matches the keyword. It depends on your systems language, my system is installed with the Dutch language so I can’t exactly verify that for you, but you’ll probably see a package listed with the name ‘htop’. This is an example of my similar Dutch instruction and output, yours should be about the same.

There should be an entry in your list with the name htop. This is an interactive process viewer, that also shows some CPU and RAM statistics. In my opinion this is the best tool there is to keep track of system resources and for keeping an eye on processes to make sure they won’t misbehave and crash your Debian system. Use the following command to install this tiny program.

Apt will start to download the htop package and install it for you. This can be done very quickly because it’s a really tiny program. Just wait for the terminal prompt to return.

To start the program, just type ‘htop’ and pressing the Enter key. Htop starts, and a list of processes show up. You don’t want to mess around to much with the processes as chances are you aren’t running that many processes at the moment. This list can get pretty extensive when you’ve installed a desktop environment and use a couple of programs on it.

You can navigate the process with the up and down arrow keys on your keyboard, and you can do whatever you want with the process. Most of the time you would be killing off misbehaving processes that are consuming to much system resources. If you ever find a process using up to many RAM or CPU just select it, press the F9 button and send the SIGKILL signal to that process, Linux will kill the process unconditionally freeing up the resources it was using. Don’t be afraid to play around in this program to get to know it but be careful not to kill any vital processes. Don’t worry to much though, because there will not be any permanent damage to the system, in this case there’s nothing a reboot can’t solve.

Finally, if you ever need to remove Htop from your system, all you need to do is use the following command:

Don’t be fooled by this simplicity though, there are many programs that have a package dependence tree. If you use apt to remove a package, you’ll only remove that package. There are ways to ensure complete package dependence trees as well as configuration files using apt and some options but i will not cover those in this tutorial as you need to know what you’re doing in most cases. You will in fact brick your system if you uses options and switches you don’t know how to use. Root on a system and APT gives you great power, but that comes with the big responsibility of exactly know what you’re doing.

Note

This will be it for now. In an upcoming article I’ll write more about the file-system explaining how it works, what the purpose of certain files and directories are and I’ll show some tricks with coping and moving files around. Thanks you very much for reading and happy hacking around on Debian Gnu/Linux.

 

 

 

Leave a Reply