Using the Terminal – Part 2: shell, bash and some basic commands

Using the Terminal – Part 2: shell, bash and some basic commands

This one turned out be be longer than expected but I wanted to get to a point where you could actually do something with the stuff you read here.

In the first part I told you where you find the terminal, what different terminal applications are there as well as a couple of basics that many modern terminal applications share.

A short look under the hood

Before I go on, I want to go a bit more in depth, don’t worry, I won’t go too far. And even if you don’t understand this stuff, you should have at least read the terms once.

I told you in the first part, that all terminals speak the same language. That was not correct because in fact they are under the hood the same. The terminal application is just a way to pass commands to the actual shell, which is bash (that’s for Bourne Again SHell), when you enter a command into the terminal it will be interpreted by bash which than makes the computer do something. So what you actually get is the underlying shell and the terminal application itself and subsequently the features you get are a mixture of both and it is sometimes hard to figure out what belongs where, a lot of functions are already present in the shell that’s why they are shared by all the terminal applications. In one of the next parts I will tell you which files control these settings and how you can make use of them.

The Unix/Linux world knows different shells so terminal commands and features are not the same everywhere but usually in the Linux distributions you might use it is bash. The standard shell (called sh, which is a symlink to the actual shell) in the *buntus is dash (for Debian Almquist SHell, not to be confused with the application launcher of the Unity desktop of the same name). dash is faster but not very user-friendly, so terminals still use bash. You can read more about this in the Ubuntu Wiki
https://wiki.ubuntu.com/DashAsBinSh

This should be enough for this part, I know it is heavy stuff. You can forget everything now but bash and that when people talk about the terminal they often mean the shell. However, there is nothing wrong with saying terminal people will understand you, if not they just want to be a smart-ass ;). And we have to keep in mind that you are a home user and not an IT professional[tm].

Understanding your files and folders a bit

Before we can start with commands, I want – and have to – tell you a little about your file system, the folders, files and their names and how you can deal with them. Also here I don’t want to go too much in depth and keep it on the phenomenon level. There is certainly more you need to know in the long run but these where the first things that I found to be odd or cool or remarkable when I switched to Ubuntu.

Some differences between Windows and Linux

One major difference between Linux and Windows is, that for Linux temp and TEMP are two different folders while on Windows they are the same. Windows doesn’t care about capital letters, Linux does. Also text.txt and text.TXT are two different files.

On Linux you don’t have different drives even though you have them in your Windows system on the same box, there is no c:\ and d:\ on Linux there is just the root directory / (note that the slash has the opposite direction in Linux) and every drive is part of the root directory. Depending on the kind of drive, you find them either in /mnt/ or in /media/, the first being for example harddrives, the latter removeable drives like USB sticks or DVDs.

In Linux you can use virtually every character in a file or folder name. Only the / is reserved, while others are not liked when it comes to commands and you have to escape them with a backslash like the blank/space or you have to write the file or path in quotes. While this is really cool when it comes to Linux file systems, you might get a problem, when you want to copy such files to a Windows file system or store them on an NTFS/FAT drive, so better avoid characters that Windows doesn’t like. You find more on that topic in Wikipedia
http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
It’s a nice overview, that I can’t integrate here in such depth.

Special directory entries

Each directory has two special entries . (one period) and .. (two periods), the first is a link to itself, the latter a link to the parent directory, so if you are in /tmp/, . is /tmp/ itself and .. is /. The . is pretty handy, when you want to run a script, when you are in a directory that contains script.sh, you cannot just type script.sh to run it, you have to type ./script.sh. Usually, these entries are hidden, your file manager will for exmple most likely not show them cause for graphical interfaces they are of limited use.

Wildcards

In the terminal you can also use wildcards such as the asterisk (*) or the question mark (?). You might already know them from Windows cause they are used fairly often. The ? is exactly one character, the * any amount of characters including none.

These two already help you a lot. *.*~ for example matches all backup files, *.jpg all jpg files. But that already leads to a problem, because jpg doesn’t match JPG, but the terminal is pretty smart cause you can do some sort of simple regular expressions. To get all jpg files, no matter how they are written, you can do something like this *.[jJ][pP]*[gG]. Why the asterisk? Because some people use to write jpeg instead of jpg.

There is way more and there are also certain commands that can use the full power of real regular expressions. But I will keep it like this for now, this topic would deserve a tutorial on its own, I just need some of these things later.

Some useful commands

With the first set of commands I want to enable you to use the terminal as a basic file manager: browsing the directories, deleting, copying, renaming and moving files, creating and deleting directories.

cd

That is Change Directory (same as on Windows) and it is used to change the current directory to another and navigate through the file system. Most of the aforementioned stuff is important for cd, if you understood a bit of the file system, you understand the cd command.

You can jump into the home directory using cd ~ or by just cd, cd / jumps to the systems root directory and cd .. jumps one level up in the directory tree to the parent directory. The terminal can also autocomplete by hitting TAB or suggest by hitting tab twice. Instead of jumping from directory to directory you can also add an absolute or relative path (auto completion works there too), so it is no problem to jump from your home directory, directly to /usr/share/applications/ which you could also write it as ../../usr/share/applications/, this is the same path just relative using the parent directory twice. That doesn’t make sense in this case but sometimes such things are really handy when you start some scripting.
Screenshot from 2015-04-05 20:41:44

ls

ls is the command to LiSt the content inside a directory. The default output of ls is very compact. The most useful switch for ls in this regard is -lh (l for long and h for human readable), that creates an output that looks like the details output in your file manager with file sizes that you can actually read.
Screenshot from 2015-04-05 20:35:55

When a folder or file is written with a period in the beginning it is treated as a hidden folder or file in file managers, by default ls will not displaythem as . and .., you can display them using the -a switch. Files that have a trailing tilde are also hidden as backup files in file managers, however, the terminal will display backup files but you can hide them using -B

You can also filter the output of ls, to just display all the jpg files inside a directory you can use ls -lh *.[jJ][pP]*[gG]. Or you can display the content of directories you are not currently in, e.g., the subdirectories of your current directory using ls -lh *.

Here is a short list of some useful switches for ls that you can combine to something you can use to a nice output:

  • l – long form
  • h – human readable
  • a – all
  • A – all but .. and .
  • p – displays trailing slashes on folders
  • B – hides backup files
  • R – list subdirectories recursively

Besides ls the *buntu know a few aliases for ls with certain switches:

  • l for ls -CF
  • la for ls -A
  • ll for ls -alF

Also ls itself is on *buntu already an alias for ls --color=auto else you wouldn’t have colors in the output. I will tell you more about aliases and how you can make your own ones later (maybe in the next part).

From Windows you might know the command dir that exists in *buntu as well and it does the same as ls. In addition there is vdir which is ls -l.

sudo

The aforementioned commands do not change anything. When you want to change something you often need root access cause you can’t do much outside of your home directory without it.

You already know sudo from previous posts. Usually sudo isn’t used as a command by itself, it’s main use for you is to pass commands to root. But you could for example open a root shell using sudo -i as a command that is useful when you have to perform a lot of administrative tasks, you leave that session using exit.
Screenshot from 2015-04-05 20:49:36

You should never use sudo to perform tasks inside your home directory. You should also not run graphical applications using sudo, it is technically possible but not recommended, it is better to use gksudo or pkexec instead, while the latter is the recommended way and the former deprecated but at times still necessary.

What can happen is that files that actually belong to you get owned by root which means that you can neither change nor remove them. So, when using sudo be sure that you have to use sudo.

mkdir

mkdir is meant to MaKe DIRectories. It’s a very basic command that you find in lot’s of tutorials and install instructions. The basic usage is mkdir [directory] where [directory] can be both an absolute or relative path. There is just one pitfall, the parent directory for the directory you want to create must exist, if you want to create a complete path including the parent directories you have to use the switch -p.

cp

cp stand for CoPy. The usage is to copy a source to a target. To copy file.txt from your recent folder to into a subfolder called files you use cp file.txt files/, you can also copy multiple files cp file1.txt file2.txt files/ or use wildcards cp *.txt files/. cp comes with a bunch of options to create backups or synchronize between folders. For manual copying I suggest to use the -i switch, which makes the command interactive, usually it would not ask before it overwrites something.

mv

mv is used to MoVe files. It’s usage is pretty similar to cp the only difference is pretty obvious cp keeps the source, mv doesn’t, kinda self explanatory. If you move a file to a file with another name like mv file1.txt file2.txt you could also do some basic renaming, however, there are better ways to do that.

rm & gvfs-trash

rm is to ReMove or unlink or delete a file. This will not move the file to trash, it will unlink the file from the file system so it simply won’t be there anymore.

The basic usage a command like rm file.txt to remove file.txt. That is pretty easy but also not very powerful. Now let’s say you want to remove all backup files in a directory, that’s also pretty easy cause you can do this rm *.*~, which is a bit more powerful. On the interwebs you can find even more advanced oneliners to remove lot’s of files. that is even cooler but, please, don’t use them yet because as long as you don’t understand what you are doing you can delete a lot of stuff you didn’t want to delete, one false blank can make the difference. In case you do use such commands use at least the -i switch to make the command interactive.

Usually, rm would not delete directories, for directories you have to use th command rmdir but rmdir can’t delete directories that are not empty.To remove directories including their content, you can use rm with the -r switch.

There is a way on *buntu that makes life a bit less dangerous and that is moving files to trash using gvfs-trash. gvfs is for GNOME virtual file system, it’s usage is pretty similar to rm, but there is a way back. Most of the *buntus come with gvfs preinstalled, on others you can simply install the gvfs-bin or the trash-cli package.

Tl;dr

We have learned that under the hood of your terminal lies the shell which is bash. We have learned a bit about how to deal with files and folders, how to use wildcards. We have also learned some basic command like navigating through your directories, listing their content, creating new directories, copying, moving and removing files. That already allows you to understand a lot of things.

This was really a long one, hope it helps understand a bit. Use --help and man on the commands if you need to know more. there are many more things to discover.