Learn Programming

UNIX console

Logging in

Before we can start we need to login to UNIX. You will be asked for your username and password every time you want to use UNIX. Simply type your username and password to login.

Username: johnny
Password:

Your password will not be printed on the screen when you type it in for safety reasons.

Once you have logged in you will see the command prompt which looks like this:

$

It might also look like this:

#

You are now ready to start entering commands.

Basic UNIX commands

The first command we will learn is whoami. This command will print the name of who you are logged in as. Here is an example:

$who am i
johnny

The passwd command is used to change your password. You might have to enter a password that is at least a certain length and that has both letters and numbers in it.

$passwd
New password:
Confirm password:

You can use the pwd command to print the directory you are currently working in. It should print the path of your home directory at the moment.

$pwd
/home/johnny

The ls command lists the files in the current directory.

$ls
documents/
first.ksh
hello.c

A wildcard (*) can be used with ls to print any files starting or ending with certain letters.

$ls *.c
hello.c

To see hidden files and directories you must use ls -a.

$ls -a
.mail/
documents/
first.ksh
hello.c

Anything that follows a command with a hyphen(-) in front of it is called a switch. Switches are a way of making a command do more things or do things in a different way. Entering any command with the --help switch usually gives you a list of switches for that command.

You know that an item on a file list is a directory if it is followed by a forward-slash(/). Some people will find that on the UNIX they are using the directories are not followed by a forward-slash.

The ll command does a long listing of your files. Some people will not be able to use the ll command on their UNIX. If you do not have ll then you must use ls -l.

$ll
OR
$ls -l

drwxr-x--- 1 johnny users Jan 10 12:01 documents/
-rwxr--r-- 1 johnny users Feb 11 13:12 first.ksh
-rw-r--r-- 1 johnny users Mar 12 14:23 hello.c

You can use more than one switch at a time in the following 2 ways:

$ls -l -a
OR
$ls -la

drwxr-xr-x 9 johnny users 4096 Oct 17 20:45 ./
drwxr-xr-x 3 root root 4096 Sep 27 16:43 ../
drwxr-x--- 1 johnny users Dec 25 23:59 .mail/
drwxr-x--- 1 johnny users Jan 10 12:01 documents/
-rwxr--r-- 1 johnny users Feb 11 13:12 first.ksh
-rw-r--r-- 1 johnny users Mar 12 14:23 hello.c

The mkdir command creates a new directory. You must also type the name of the directory you want to create.

$mkdir newdir

You will not get a message telling you that the directory was created. If you do get a message then it will be an error message from typing the command in wrong. Use the ll or ls -l command to see if your new directory has been created.

$ll
OR
$ls -l

drwxr-x--- 1 johnny users Jan 10 12:01 documents/
drwxr-x--- 1 johnny users Apr 1 1:23 newdir/
-rwxr--r-- 1 johnny users Feb 11 13:12 first.ksh
-rw-r--r-- 1 johnny users Mar 12 14:23 hello.c

You can change to another directory using the cd command.

$cd newdir

You can use the pwd command to see that you are now in the new directory.

$pwd
/home/johnny/newdir

You can go up a directory by changing to that directory or by using cd ..

$cd /home/johnny
OR
$cd ..
$pwd

/home/johnny

Now we can delete the directory using the rmdir command.

$rmdir newdir
$ls

documents/
first.ksh
hello.c

You can use the touch command to create a new, empty file.

$touch myfile.txt
$ls

documents/
first.ksh
hello.c
myfile.txt

The rm command is used to delete a file.

$rm myfile.txt
$ls

documents/
first.ksh
hello.c

The mv command is used to move files. Create a file called move.txt and a directory called new/ and then move move.txt to new/ like this:

$touch move.txt
$mkdir new
$mv move.txt new/
$ls new

move.txt

The ls command was used in the above example to print the contents of new/ without changing to the directory.

You can't normally delete a directory without emptying it first. rm is usually used for deleting files and not directories but if you use it with the -rf switches it will delete a directory and all of its contents.

$rm -rf new

mv can also be used to rename files.

$touch file1.txt
$mv file1.txt file2.txt
$ls

documents/
first.ksh
file2.txt
hello.c

The man command is one of the most important commands. You can use the man command to read the manual of any command. Here is and example of getting help for the ls command:

$man ls

This opens up the manual page for ls and tells you everything you want to know about it. When you are finished you must press "q" to go back to the command prompt.

File permissions

File permissions determine if a user is allowed to access a file. We must first look at a long file listing to understand file permissions.

drwxr-x--- 1 johnny users Jan 10 12:01 documents/
-rwxr--r-- 1 johnny users Feb 11 13:12 first.ksh
-rw-r--r-- 1 johnny users Mar 12 14:23 hello.c

The first part of the long listing tells you if the item is a file or a directory and the file permissions. The first "d" in the following example means the item is a directory. A hyphen(-) means that an item is a file.

drwxr-x---

The rest of the first part is made up of 3 sections which are owner, group and others. The letters show what access writes users have for the file.

   u   g   o
d|rwx|r--|r--|

u - Owner
g - Group
o - Others
a - All(ugo)

r - Read
w - Write
x - Execute

We can use the chmod command to change file permissions. The following example gives the group execute rights:

$chmod g+x first.ksh

You can change more than one permsission on a file like this:

$chmod u-rwx first.ksh

Learn UNIX tutorial
UNIX console
vi editor
KornShell scripting