Unix Tutorial Part 1

In the previous post, we have seen the architecture of UNIX. In this post, we will learn about the directories.

A directory is used to store file names and related information.All files, whether ordinary, special, or directory, are contained in directories.UNIX uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree. We will discuss the following points as mentioned below.

  • Listing files and directories
  • Making Directories
  • Changing to a different Directory
  • The directories . and ..
  • Pathnames
  • More about home directories and pathnames

1.1 Listing Files and Directories:

When you first login, you will yourself in the Home Directory. Your home directory has the name same as your user name and all your personal files and subdirectories are saved here.

ls(list): To find out what is your home Directory , you need to type

% ls

This command (ls) lists the contents of your current working directory.

There are chances that no files will be visible in your home directory, in that case UNIX prompt will be returned.Alternatively, there may already be some files inserted by the System Administrator when your account was created.

ls does now list all the files present in your home directory, but only those whose names are not started with a dot(.).Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. These are hidden, so these can not be changed unless you are very much familiar with UNIX or you are an authorized person.

To list all files in your home directory including those whose names begin with a dot, type

%ls -a 


1.2 Making Directories:

mkdir(make directory): 

You can make a subdirectory in your home directory to hold the files you will be creating. To make a subdirectory called unixdir in your current working directory type.

% mkdir unixdir

To verify the directory you have created, type

% ls


1.3 Changing to a different directory:

cd (change Directory):

The command cd directoryname means change the current working directory to directoryname. The current working directory is considered as the directory you are in, i.e. your current position in the file-system tree.

To change to the directory you have just made, type

% cd unixdir

The above command will change to the directory ‘unixdir‘.Type ls to see the contents (which should be empty).

Exercise: Make another directory inside the unixdir named as mybackup


1.4 The directories . and ..

 Assume that we are in unixdir directory, then type

% ls -a

You will see in the directory unixdir (or in any other directory), there are 2 special directories called dot(.) and dot dot (..)

The current Directory: 

In Unix, dot(.) means the current directory. So, typing

% cd .

means stay where you are (i.e. in the unixdir directory)

Note: There is a space between cd and the dot(.)

The Parent Directory:

(..) means the parent of the current directory, so typing

% cd ..

will take you one directory up the hierarchy (back to your home directory).

Note: typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.


1.5 Pathnames

pwd (Printing Working Directory):

Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type

% pwd

The full pathname will look something like this –

/home/its/myd/shekhar

which means that shekhar (your home directory) is in the sub-directory myd (the group directory),which in turn is located in the its sub-directory, which is in the home sub-directory, which is in the top-level root directory called ” / ” .

 Exercise:

Use the commands cd, ls and pwd to explore the file system.

(Remember, if you get lost, type cd by itself to return to your home-directory)


 

1.6 More about home directories and pathnames

Understanding pathnames

First type cd to get back to your home-directory, then type

% ls unixdir

to list the conents of your unixdir directory.

Now type

% ls mybackup

You will get a message like this –

mybackup: No such file or directory

The reason is, mybackup is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your mybackup directory, you must type

% ls unixdir/mybackup


~ (your home directory)

Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing

% ls ~/unixdir

will list the contents of your unixdir directory, no matter where you currently are in the file system.

Exercise:

What do you think

% ls ~

would list?

What do you think

% ls ~/..

would list?


Command Meaning
ls list files and directories
ls -a list all files and directories
mkdir make a directory
cd directory change to named directory
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
pwd display the path of the current directory
Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...