How-To Use the Linux Terminal

Modern Linux distros feature such powerful graphical user interfaces that most users won’t need to touch the terminal. But some of us wish to make the most of our systems and don’t mind getting our fingers dirty to do it. That’s why you’re using Linux in the first place isn’t it?

If you’re comming from a Windows background you’ve probably seen something called the “Command Line” or command prompt; perhaps you’ve even used it. There are just some things that are easier or simpler to do using the command prompt on Windows; powerful system features and capabilites can be levered through it. But the Windows command line is a pale shadow compared to the Linux terminal.

To use the Linux terminal, usually an interface called bash, is to reach into the core and touch the beating heart of your system. Everything, and I mean everything, about your system can be accessed, scripted, and automated through the terminal. It is also, because of its power and flexability, a convenient tool to use for some tasks that I go over in this guide.

For example, instead of opening up Synaptic, searching for libCurl, selecting it, installing it, and exiting Synaptic again I can simply type sudo apt-get install libcurl into my terminal and let my system do all the work while I watch. But the average Windows user, pasty-pale and weak from non-exertion, gets cross-eyed, red in the face, and speechless when confronted by the arcane inner workings of their system. Let me guide you through it, grasshopper.

Each command going into the terminal is either a command to execute a program or an argument/parameter to that program. A simple concept, but one which is not always immediately understood. Let’s walk through that last example.

sudo

This the command to tell the system to run the next command as though you were root. Use it carefully, and only in situations that you absolutely must have root access for.

apt-get

Here we’re telling the system to run a program called apt-get. It’s the package manager on your system; use it to install, remove, and update software.

install

This is not a program invokation. It’s actually an argument to apt-get. Now, given what we just learned about apt-get, can you guess what this is going to do?

libcurl

Once again, this is an argument being passed to apt-get. It’s the thing we want apt-get to install on our system.

So what we’re doing is telling the system to give us root priveledges, and, as root, run the program apt-get. Then we’re telling apt-get to install a software package called libcurl. It’s so much simpler and easier than going through Synaptic’s GUI isn’t it?

But what if you have other things you want to do with the terminal? What if, say, you’ve broken something in your system, you can’t log into a graphical desktop, and you want to get your files to a safe place before re-installing your system to try things again?

Now you’ll need the functionality of some basic system tools. Even more basic and fundamental than the above example. Check this out:

cd = change the current directory to a new one. For example, cd .. will move up one directory level. cd ~/Desktop will change into your “Desktop” directory. Remember, commands in the Linux terminal are case-sensitive.

ls = list the contents of the current directory. This is often aliased to dir on modern Linux systems. I have dir aliased to be ‘ls -al’ myself: alias dir=’ls -al’

cp = copy files from one directory to another. The first directory given is the source directory, the second is the destination. If your directories have spaces in them then surround them with double quotes to keep the bash from getting confused.

mv = the same as cp, except that it moves the files rather than copy them. It’s like the the cut/paste of the command line world, only it does things all in one step.

chmod = changes the permissions of a file. We’ll get to this later.

chown = changes the ownership of a file. This is very powerful if used in conjunction to the sudo command. More on this when we get to chmod.

mount = mount a directory, hard drive, or hard drive partition, to a given directory. This one can get tricky, but it’s extremely important when the access and safety of your files is concerned.

Now let’s pretend that we’re in that scenario where you’ve just buggered your system by tinkering with things, you can’t figure out what you did wrong, and you have even less idea of how to fix it. You’ve decided that you just want to re-install the OS and start things over from scratch. Now here’s the catch: let’s say that whatever it was you did wrong has screwed up the GUI. You can log into your system, but it won’t start the desktop and all you get is a terminal. This situation is important because it’s one you may encounter; it’s a situation I was in years ago myself.

The first thing to do is get oriented, like checking the wallpaper on your desktop just to touch base. Let’s hit dir to see where we are.

$dir

Desktop Downloads Documents Music Pictures Videos

You can see what your current working directory is to the left of the “$”, but it’s always nice to actually see the contents of where you are. Anyway, let’s say that the only things we want to keep are in the Desktop. Now we know that “~/Desktop” is the directory full of stuff to save.

Let’s find a safe place to put everything so that it survives when we format this partition. Well, if you’re dual booting, you’ll have a Windows partition won’t you? Why not just move everything over there and then wipe the Linux partition?

The Windows partition is mounted to “/media” by default under a folder with the name of the volume. It’s usually something like “My Computer”. We can check this by using ls or dir

$dir /media

cdrom cdrom0 EIHORT

Oh, that’s right, I named that thing “EIHORT” on my computer. Glad I checked huh? If there’s somewhere specific I want to put everything then I can navigate around on EIHORT just as though it were a normal directory, which it is. Let’s put everything I want to save in “/media/EIHORT/saves”:

$cp ~/desktop /media/EIHORT/saves

ls: cannot access desktop: No such file or directory

What? I typed in “desktop”, and I know that’s in the current working directory, what’s up? Unlike Windows filenames are case-sensitive in bash. That means you can have “desktop” and “Desktop” in the same directory and they won’t conflict. Let’s try again:

$cp ~/Desktop /media/EIHORT/saves

ls: cannot access /media/EIHORT/saves: No such file or directory

Now what’s the problem? I know EIHORT is there, and I know I typed in “Desktop” correctly. Did you make sure that EIHORT was mounted? Partitions, on some distros, are not set up to be mounted automatically. Let’s do this manually just as a learning experience.

Let’s say that there’s only one physical hard drive in my computer. That hard drive is a SATA hard drive and it’s plugged into the first SATA port on my mother board. That means that the hard drive is a special device under the /dev directory called /dev/sda and each partition is a number. So /dev/sda1 is the first partition, sda2, sda3 and so on are later. Now, I can mount these partitions and see what’s in them:

$sudo mount /dev/sda1 /media/EIHORT

$dir /media/EIHORT

ATI Documents and Settings MSDOS.SYS NVIDIA AUTOEXEC.BAT Program Files

Good, I found my Windows partition. But I don’t see a “saves” directory. We’ll have to make one won’t we? You might have to use sudo to make things work since we used sudo to mount the partition your system may consider /media/EIHORT to be owned by root.

$sudo mkdir /media/EIHORT/saves

Now, finally, we’re ready to go:

$sudo cp ~/Desktop /media/EIHORT/saves

Depending on how much you have to copy over this may take a while. Don’t expect to get your terminal back until everything is copied over.

Now you know just enough about bash on Linux to be dangerous. The purpose of all this was to expose you to the Linux terminal, make you feel all hackery, get you used to the workflow, and illustrate how to think about solving these sorts of problems and error messages.

But if you actually want to use the bash you’ll need to get a little more in-depth practice. Maybe a lot more in-depth practice. You can get this by googling “Linux bash” and reading, reading, reading. If all you need is to check out how to do this or that real quick then you might try some of these links below.

Links:

A helpful listing of bash commands.

The Wikipedia article on the bash.

An advanced shell scripting guide for you adventuresome types.


About this entry