![]()
Linux for Newbies: Part 4 -- Navigating in Linux
by Gene Wilburn
(The Computer Paper, November 1999. Copyright © Wilburn Communications Ltd. All rights reserved)
Last month we installed Linux and learned how to login as root and shut down the system. Essential, but not too exciting. This month we'll start navigating the system. Knowing how to get around inside Linux is the first step towards Linux mastery.
The Linux filesystem is a hierarchy--an inverted tree structure. The top level is called root and is indicated by a single forward slash ('/'). Directories branch off from root and each directory level is preceded by a forward slash. Hence the basic, top-level directory structure looks like:
/ /bin /boot /dev /etc /home /liband so forth. There are no drive designators such as C: or D: as in DOS. Everything is accessed via one single tree with many directories and subdirectories. Additional drives are simply mounted into the tree structure. We'll see examples of this in a later column.
Before you actually start navigating the tree, it's time to create a user account for yourself. For a number of compelling reasons it's a critically good idea not to use your superuser (root) account except for special and administrative purposes. You can do severe, inadvertent damage if you make a mistake logged in as root. As a normal user, you limit your potential destructiveness to a much smaller area.
Boot up Linux and login as root with your root password:
Login: root Password: yoursecretpasswordNow you can create user accounts. Create at least one for yourself (up to eight characters). In my case, my login name is genew so I would set myself up as a user by typing the following:
adduser genew passwd genewThis automatically creates my user directory as /home/genew and it sets my password. You can create an account with separate password for each member of your family or your workgroup. By default Linux users have a home directory created under /home. The exception is the superuser account, whose home directory is /root. After you have created a normal user account for yourself, exit from your superuser account (by typing 'exit') and re-login under your user name, e.g.:
exit Login: genew Password: mysecretpasswordWhere Am I?
It's easy to get lost in the large tree structure of the filesystem, but it's also easy to find out where you are and move to where you want to be.
When you first log in, you're always in your home directory--your personal workspace. To verify this, type 'pwd' at the $ prompt:
$ pwd /home/genewIn Linux, as in Unix, you can always go home by typing 'cd', the Change Directory command. When issued without a qualifier, it means "go home". This is a slightly different behaviour than the cd command in DOS, where 'cd' without a qualifier is equivalent to the Linux 'pwd'.
Other than that, the Linux cd command works in a manner very similar to the way it does in DOS except that the slashes are the right way around--backslashes are a DOS crudity caused by historical impurities in DOS design.
Hence you can go to the root of the filesystem tree by typing 'cd /'. You can go directly to a subdirectory by typing in the full path, e.g., 'cd /usr/local/bin'. Relative movement is built in. To go up one directory simply type 'cd ..' just as you would in DOS. If you're already in /usr/local, you can go to /usr/local/bin by typing 'cd bin' without the full pathname.
Try these cd commands on your system. You can verify each result by typing 'pwd' (which stands for Print Working Directory).
What's in here?
The basic Linux command for looking around at files and directories is 'ls', which means List. Its counterpart in DOS is 'dir'. The ls command has several flags, or qualifiers, you can use to alter the amount and type of information displayed. Here are some Linux/DOS equivalences:
DOS Linux Meaning dir/w ls Simple listing dir ls -l Long (full) listing dir/ah ls -la Long + hidden filesThe Linux ls command has some additional flags that are very handy (e.g. try out 'ls -m'). You can read up on the options by typing 'man ls'. The 'man' command reads manual pages--the core built-in documentation for Linux commands. Man pages are not tutorial in nature but they provide maximum info in the smallest space. They will seem arcane at first, but remember: "man is your friend." Use it often. You type 'q' to exit a man page.
Now practice moving around your filesystem and typing 'ls' with variant flags, in various directories. Don't worry if you don't understand what all the files are. What you want to do at this stage is to gain a sense of comfort in moving around the system and looking at things. Take a look at the furnishings using 'ls' and 'ls -la'. Except for some cryptic looking hidden files (files that begin with a period, e.g. '.bashrc'), it's a pretty bare place. You need to create your own subdirectories (or folders as they're called in Windows and Macintosh).
The command for doing this is 'mkdir', which works in a manner very similar to the DOS 'mkdir' and the DOS variant 'md' commands. Let's say you want a subdirectory in your home directory to store some documentation files you want to write:
cd mkdir docsLinux and DOS are fundamentally different in one area. Linux is a case-sensitive operating system. Hence 'docs', 'Docs', and 'DOCS' are three completely different names in Linux and you have to type the right one. This takes a little adjusting to if you come from a DOS, OS/2, or Mac background. The Linux convention is to stick with lower case for most directory names and filenames. It's also easier to type.
If you want to remove a directory, and it's still empty, you use the 'rmdir' command, e.g. 'rmdir docs'. This corresponds to the DOS 'rmdir' and 'rd' commands. It's no coincidence that DOS mirrors these Unix commands. DOS 2.0 borrowed heavily from Unix, but the forward slash was already being used so DOS inserted the awkward backslash in its place.
You can create several subdirectories in a single command:
mkdir docs/linux ; mkdir docs/linux/tipsOn the Linux command line, you can issue multiple commands by inserting a semicolon between them.
Who's in command?
It's time to talk a little about this command line we've been using. The little '$' prompt you see (or '#' if you're logged in as root) is part of the "shell" you're running--the Linux command interpreter. There are several shells available, but the one that is used by default in Linux is called Bash. It is derived from the original Unix Bourne Shell (a person's name) with added elements taken from the Unix C Shell and Korn Shell (another person's name) plus a few new features. Bash stands for Bourne Again Shell.
The Linux Bash shell provides command-line history. If you tap the Up Arrow on your keyboard, it lists the last 1000 or so commands that you typed. Linux also has a scroll-back buffer that lets you look at things that have scrolled off the screen. Press Shift + PgUp to scroll back a page at a time, Shift + PgDn to scroll forward.
If you came to Linux from a DOS or OS/2 background and enjoyed writing batch files, you're going to love Bash. The equivalent of batch files are called shell scripts and the built-in programming elements of Bash provide a full, rich programming environment. We won't delve into shell scripting much in this series, but any good general book on Linux will provide information on how to get started.
Permissions
Being a multiuser operating system, Linux uses a traditional Unix approach to directory and file permissions. Because you're the system administrator of your Linux box, you'll need to know file permission basics.
To get started, go to your home directory and type the following:
$ echo ok > myfile2.txt $ echo moreok > myfile1.txt $ ls -l -rw-rw-r-- 1 genew genew 7 Aug 19 10:53 myfile1.txt -rw-rw-r-- 1 genew genew 3 Aug 19 10:53 myfile2.txtThe actual filenames are on the far right, arranged alphabetically. We created these files by redirecting an 'echo' statement into two different filenames. Note that myfile1.txt is 7 bytes long and myfile2 is 3 bytes long. They both display "genew" twice. The first name is the owner of the file and the second name indicates group ownership. By default Red Hat Linux creates a group that matches your login name and everything you create is stamped with both. This is a security measure that we'll discuss in a future column. The "7 Aug 19 10:53" part of myfile1.txt tells me the size of the file, in bytes, and when it was created.
At the beginning of the line we see '-rw-rw-r--', the permissions information. To read this information you need to mentally split it into four fields. Read it this way in your mind '- rw- rw- r--' with spaces between the fields. The first part, '-', simply informs us that this is a file not a directory. Directories begin with 'd'.
The second field represents the owner's rights. There are three basic permissions in Linux: read 'r', write 'w', and execute 'x'. Text files are normally not executable unless they're a programming script so the 'x' field is blank ('-'). A shell script file would have 'rwx' for the owner because it's an executable.
The third field is group permissions and the fourth field is world permissions (i.e. everyone on the system). Hence the user and the group have read/write access to this file and everyone else has read permission.
We'll spend some time on setting permissions in an upcoming lesson, but if you want to jump ahead, what we've covered in this column are fundamental Unix commands. You can read ahead in any Linux or Unix book. There is also a nice section on this material in the Red Hat 6.0 Getting Started Guide, available for viewing at www.redhat.com/support.
Next time: We're going to leave the basics for a column or two so we can concentrate on setting up X Window, the Linux graphical user interface. Then we'll return to the fundamentals and learn more about using and administering the system.
Gene Wilburn (gene@wilburn.ca) is a Toronto-based IT manager, musician and writer who operates a small farm of Linux servers.
-30-