View page as slide show

Command Line Basics

Introduction

Many people are afraid of the command line because they believe it's very difficult. However, the command line need not be difficult, and even a very basic knowledge of the command line can really help you in a pinch.

This tutorial is targeted at those who have been around Linux for a bit but don't know anything about the command line.

Prerequisites

You should know:

  • what a file is and what a directory is.
  • what / is, and the difference between it, /root and the root user.
  • what your /home is.
  • what a path is and what it looks like.
  • the difference between a relative and absolute path 1).
  • at least on a basic level the layout of a Linux file system (/mnt, /dev, /usr, etc.).

Introducing The Commands

To get started, click on the terminal icon either on the desktop or on the taskbar. It should open a terminal which looks something like this:

Now let me explain what you're seeing here. You should be seeing a left bracket, your username, an @ sign, the name of your computer (often “localhost”), a space, a tilde (may be something else, don't panic), a right bracket, and a dollar sign. This is the command line (AKA terminal) of Linux. You can type text in the command line and hit Enter to run it.

Ready to get started? Okay, let's get going!

pwd

The first command to learn is pwd. It stands for “Present Working Directory”. When you type it in, it will tell you what directory you're currently in. Go ahead and type it in:

pwd

It should display the path of the directory you're in.

This command is often paired with cd.

cd

This command stands for “Change Directory”. You should tell it where you want to go, obviously.

If you type in just cd without telling it where to go, then it'll take you back to your /home directory. If pwd tells you you're somewhere other than your /home directory at the moment, please run cd to return to your /home.

Now try running this command:

cd Desktop

You should notice something with the command line when you change directories. This won't happen with every Linux system, but it happens in PCLinuxOS and TinyMe. Instead of that tilde, you'll see the name of the directory you're in.

Now you're sitting in the Desktop directory. Any file or directory placed here will show up on your desktop. Let's take a look and see what files are in this directory.

ls

Now run this command:

ls

When you run ls, you will see a list of all files and directories which reside in the same directory you are in. In this case, you will see a lot of files which end in .desktop.

Now that you know how to change to a different directory and list its contents, let's try creating a directory.

mkdir

mkdir is short for “Make Directory”, and that, I feel, should be pretty self-explanatory at this point. Go ahead and give it a try:

mkdir test

creates a directory called “test” inside /home/[you]/Desktop.

Now let's say I need to create a directory, but that directory itself will be in a directory which doesn't exist. Believe it or not, this happens all the time. What I want you to do is create a directory called test2 and inside test2 create a directory called inside_test2. For me, that means it would look like:

/home/k/Desktop/test2/inside_test2

I've already given you the tools to do this. Go ahead and create the directories, then look at the answer below.

There are one of two possibilities you could have used to accomplish this:

mkdir test2
cd test2
mkdir inside_test2
mkdir test2
mkdir test2/inside_test2

If you used either option, you did well. Now you can learn how to do this with one stroke. Append the -p option to mkdir:

mkdir -p test2/inside_test2

And mkdir will create both directories at once.

rm

rm “ReMoves” a file or directory. To remove a file without being asked for confirmation, append the -f (force) option:

rm -f

If you want to remove a directory, including everything underneath that directory without being asked for confirmation on every file, run:

rm -rf path/to/directory

So now that you know how to remove a directory, remove test2 from your Desktop directory.

WARNING! Consider rm to be heavily armed and dangerous! ;-) rm does not move things to the trash! Once a file is removed with rm, it is gone! There is no undelete!

su & exit

su is a very important and dangerous command. When you run a program either from the command line or from the system menu, you are running it as a regular user. Regular users have limitations on what they can do. There is one administrative user, root, which can do anything on the system.

su stands for “Switch User”. When you run su by itself, you elevate yourself to the status of the administrator, root. Note that when you're “in root mode”, you can do a lot of things, even delete every file on the system. While you should be careful in root mode, sometimes it's necessary to run stuff as root. su is the best way to run programs or manipulate files as an administrative user (much better than logging in as root!). Go ahead and run it:

su

Now note that you'll be asked for a password. When you type in the password, the terminal will not echo anything back. You will not see an asterisk for each letter you type in– you'll see nothing. Many people think that it isn't accepting the password. It is, believe me. Type in your password and hit Enter. Now you're in root mode and you can do whatever you like.

Most of the rest of the tutorial will assume you're running as user (and it's a bad idea to run just any old program as root), so let's learn how to exit root mode.

When you're done doing something as root, run:

exit

If you were to run exit as your regular user, it would close the terminal (or the tab you have open in the terminal).

And now that we're back in regular user mode, I want to show you one more thing about su. What if you want to run a single command as root? It's annoying to have to su, give the password, run the command, then exit when you just want to run one command. When you pass the -c option to su, the computer will run the command after su -c as root, then drop back to your regular user. Let's run a harmless command as root:

su -c pwd

See? It asks for the root password, gives the output of pwd, and drops back to regular user mode. No need to run exit. This is going to come in very handy in a moment.

locate & updatedb

locate is a very useful command. Let's say you can't remember the location of xorg.conf. Run this command:

locate xorg.conf

You will get more than one response from this command. I get:

/etc/X11/xorg.conf
/etc/X11/xorg.conf.backup-x11-driver-finalize
/etc/X11/xorg.conf.old
/usr/share/man/man5/xorg.conf.5.bz2

There's another command which is paired with locate, updatedb. When you run locate, it doesn't actually look at the files on your system and output whatever matches your request. Rather, it looks in a database which has a list of files on your system. This database is not updated every time there's a change on your system (that would be a bad idea). updatedb updates that database. Note that updatedb is run every time you start up your system. If you're one of those who likes to keep your computer on for long periods of time, then you'll probably want to run updatedb once a week or so. If you reboot every day or so, then you'll rarely if ever run updatedb.

Now, because of where updatedb has to go on your system, it must be run in root mode. The best way to run updatedb is like this:

su -c updatedb

Got this all down? :-) Let's move back to locate.

Now let's say that we're searching for an icon. Run:

locate icon

Whoa! That's a lot of output! I'll bet it scrolled by so fast you had no chance of reading all of it. This is where less comes in.

less

As well as teaching you less, this part is going to introduce a new concept, the pipe. The pipe passes the output of one command to another command.

The pipe looks like this: | and is found on most American keyboards by pressing Shift and the key between the Backspace and Enter keys. However, this key may also be found just to the left of the Backspace key (usually when the keyboard has a huge Enter key and a tiny Backspace) or even to the right of the right Shift key.

less takes whatever you are handing to it, puts it in a buffer, and lets you scroll around. Try it:

locate icon | less

In this case, locate icon is producing output, but instead of showing it on the screen, the pipe redirects the output to less.

Press

q

to exit less.

Note that less can always be used by itself to view a text file.

less /etc/X11/xorg.conf

cat

cat is short for “concatenate”, which means to append. 1 + 1 = 2, but 1 cocatenate 1 is 11. Many people say it's wrong to use cat to show a file, but frankly, that's what it's most often used for:

cat /etc/X11/xorg.conf

The difference between cat and less is that cat does not allow you to scroll around in a file and less does not keep the contents of a file on-screen.

Note: Please don't use cat | less [something]. That's just bad practice. less can be used by itself in that case.

1) It's fairly easy to tell the difference. If it starts with a /, then it's an absolute path. Otherwise, it's a relative path.

Discussion

ken bernardken bernard, 2010/03/30 13:24

I am trying to find a HOWTO install the manpages for the man command in TinyMe 2010. I am shocked to find that the man command is unavalable in TinyMe. The included Smart Package Manager does not have a way to install it so it will work.

KDulcimerKDulcimer, 2010/04/05 09:44

Please put help questions on the forum.

Dave RussellDave Russell, 2010/04/08 13:37

Isn't the correct spelling of the append word concatenate?

Enter your comment
WMPWS
 

 
help/commandlinebasics.txt · Last modified: 2010/04/12 13:57 by kdulcimer
Recent changes RSS feed Creative Commons License Valid XHTML 1.0 Valid CSS Driven by DokuWiki