print  [print page]

UNIX and LINUX For Beginners

 

Command Line Primer, Part One

 

INTRODUCTION

 

You are about to enter a wonderful, new world. While novices will choose to use Windows and the Linux GUI, power users since time immemorial have chosen the way of power: the command line.

 

The command line is simple, yet is all you will ever really need. There are only three rules to remember before proceeeding:

 

 

With that said, let's begin.

 

THE LINUX AND UNIX FILESYSTEM

 

Unlike Windows with it’s C: drive and other drive letters, Unix and Linux use filesystems with a descending directory structure. The top of the system in windows uses the C:\ drive, but in Unix it is simply:

 

/

 

This is called the root directory, and all other directories fall beneath it:

 

/

/usr/local/bin

/var/spool/cron

/home

/opt/oracle/

/mnt/cdrom

 

Furthermore, volumes like disk drives and CD-ROMs do not get a drive letter like in windows, instead they are mounted on subdirectories and can seen with the 'mount' command:

 

# mount

/dev/hda1 on / type ext3 (rw)

/dev/hdb1 on /data type ext3 (rw)

/dev/scd0 on /mnt/cdrom type udf (ro)

 

According to the above hard drive a partition 1 is mounted on the root filesystem, while a second drive, hard drive b is mounted on a directory called /data. Meanwhile, the CD-ROM is mounted on /mnt/cdrom.

 

There are some important directories of the filesystem that must be present and are historically dedicated partitions:

 

 

While these directories historically were dedicated disks or partitions (for load balancing and to protect the all important root filesystem), the advent of journalled file-systems have made this unnecessary. Therefore, on more modern systems the default layout may be a single disk with one partition mounted as root, which contains the above as regular directories.

 

 

RUNNING COMMANDS

 

Every system has one all powerful user: the root user. User root is analagous to the Windows Administrator (or is it the other way around?) and has complete power of the system. When you run a command on the system it runs with the same permissions as the user that runs it.

 

Running commands on a Unix or Linux system is done by simply typing the command, followed by optional arguments separated by spaces:

 

Syntax: command arg1 arg2 arg3

 

# ls

 

# ls -lsrt

 

# ls -l -s -r -t

 

# ls --size

 

In the above example, we run the simple command ls, which will list a directory's contents. In the second example, we run the ls command with the argument -lsrt which gives a long sorted by reversed time. This is the same as running the third command ls -l -s -r -t. The last example is a Linux syntax that uses a word argument --size. The double dash -- tells Linux that the argument is a whole word, rather than -s -i-z and -e.

 

As a side note, the command line numbers each word in the command line for internal processing which can be used in scripting, which you don't need to worry about now. For instance, the command is $0 while each argument is $1, $2, and so forth.

 

VARIABLES

 

An important part of UNIX and Linux are variables. There are a lot of built in variables such as $USER which stores your username. You can view the system variables by typing the command 'set'.

 

You can create custom variables to use in your commands. For example:

 

# MYDIR=/usr/local/bin

 

# cd $MYDIR

 

# pwd

/usr/local/bin

 

However, the variable is only available within the current shell, it will not apply to any new ones you create within the shell or commands you run. For instance, to run Oracle scripts you may need to manually set the USER variable to 'oracle'. But if you run the script, you will get the error 'must be oracle to run this script'. To fix this, you would do:

 

# export USER=oracle

# ./MySqlScript.pl

 

The 'export' command sets variables and applies it to all child processes.

 

THE SYSTEM PATH

 

When you run a command, Unix or Linux looks in the system path to find the command file to run. For instance, if you run the command 'ls' Unix will look in all its PATH directories and find the 'ls' command at /bin/ls.

 

You can view your path by typing the command:

 

# echo $PATH

/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin

 

This means that when type a command, it will look in the following directories (in order) and run the first match it finds:

 

/usr/local/bin

/usr/bin

/bin

/usr/X11R6/bin

 

You can find out where a command is located within the path by typing:

 

# which ls

/bin/ls

 

You can't do that with Windows!

 

But what if the command isn't in your path? For instance, let's say you want to run a custom command called mycmd in the directory /var/tmp. Assuming that you are in the directory /var/tmp, you could do one of the following:

 

# /var/tmp/mycmd

 

or

 

# ./mycmd

 

The first example tells Unix the full path to command, thus overriding the system path. But what does the last one do? That also gives Unix the full path, just using special reserved characters. In Unix '..' means the parent directory, while '.' means the directory you are in. So since you are already in /var/tmp, both commands are actually the same!

 

BEGINNER COMMANDS

 

Okay, now let's start learning some commands:

 

 

 

LEARNING COMMANDS

 

So, how do you learn new commands? That is fairly easy in Unix! Unix comes with built in manual pages, or 'man' pages. You have everything at your fingertips in Unix to learn as you go!

 

For instance, let's say we want to learn a bit more about the ps command to figure out what the command ps -ef above actually does. You would simply type the following:

 

# man ps

 

And voila! You get a man page that you can browse through, using the space bar to advance through each page. Using the man page we learn that -ef gives us a full process listing with expanded output.

 

But what if you dont know what command to run to do a certain function? No problem, because all the man pages are indexed into a built in whatis database. The whatis database allows you to look up a command in three useful ways:

 

# whatis ls

ps (1) - report process status

 

# apropos partition

fdisk (8) - Partition table manipulator for Linux

jfs_mkfs (8) - create a JFS formatted partition

jfs_mkfs [mkfs] (8) - create a JFS formatted partition

lvmdiskscan (8) - scan for all disks / multiple devices / partitions available

mpartition (1) - partition an MSDOS hard disk

partprobe (8) - inform the OS of partition table changes

pvcreate (8) - initialize a disk or partition for use by LVM

sfdisk (8) - Partition table manipulator for Linux

 

# man -k partition

fdisk (8) - Partition table manipulator for Linux

jfs_mkfs (8) - create a JFS formatted partition

jfs_mkfs [mkfs] (8) - create a JFS formatted partition

lvmdiskscan (8) - scan for all disks / multiple devices / partitions available

mpartition (1) - partition an MSDOS hard disk

partprobe (8) - inform the OS of partition table changes

pvcreate (8) - initialize a disk or partition for use by LVM

sfdisk (8) - Partition table manipulator for Linux

 

Above we have three examples. In the first, we use the command whatis to look up the definition of the ps command in the whatis database. In the last two, we use apropos and man -k to look up all commands that have the word 'partitioni' in their definition. As you can see, apropos and man -k do the same thing. I prefer apropos, but it doesn't work on HP-UX, so you must use the man -k version.

 

If the whatis database doesn't exist, you can create it using either the makewhatis or catman commands. The one you use depends on the version of Linux or Unix you are using:

 

 

Red Hat:

# /usr/sbin/makewhatis

 

Solaris:

# /usr/lib/makewhatis

 

HP-UX:

# catman -w

 

 

OWNERSHIP AND PERMISSIONS

 

Ownership and permissions are a very important part of Unix and Linux. The ownership and permissions are displayed using ls -l:

 

# ls -l

	-rw-r--r--  root sys  1071479 Mar 30 myData
	drwxr-xr-x  root sys     4096 Aug 14 appData

 

[permissions] [owner] [group] [size] [modified] [file]

 

The first column contains the permissions and is ten characters (this is also commonly referred to as a mode). The first character is a special setting, and will be d for directories, t for stick bit directories that run programs in protected memory, and s for suid programs (programs that can run with elevated permissions). Otherwise, it will be a simple - (no setting) for normal files.

 

The next nine characters are groups of three characters that show the permissions for owner, group, and all others. For example, characters 2-4 show the permissions for the owner in the following order: read, write, and execute. So a file with permissions -rwx------ (700 mode) can be read, written, and executed by the owner. A file with permissions -rwxr-x--- (750 mode) gives full control to the owner and can be read and executed by members of the specified group.

 

To better understand modes, think of each owner, group, and other permission set as a sum value of seven. Read ownership is worth four points, write permission is worth two points, and execute is one point. No permissions is zero points. All combinations is unique, as shown below:

 

 

Therefore, octal mode 775 would be equivalent to -rwxrwxr-x.

 

Continuing with our example above you can see in column two and three that the owner is root and belongs to the group sys. The fourth column is the file size, column 5 is the modified date/time, while the final column is the name of the file or directory.

 

You can change permissions (assuming that you have write access to the file) using the chmod command. The chmod command can use easy syntax or octal mode syntax:

 

# chmod u+rwx,g+rwx,a+rx filename

 

would be equivalent to:

 

# chmod 775 filename

 

However, the second example is preferred because it is more exact. It explicitly sets the file permissions as described in the octal mode designation 775. The first example only adds permissions, so if 'all others' already have write access they will continue to have it if you use the simple syntax. Using octal mode effectively resets permissions.


Go to Part 2