Untitled Document

Chapter - 4 File Systems & File Handling Commands

In this chapter we will see the UNIX file system and how UNIX handles the files using Commands.UNIX looks everything as a file and it has thousands of files . When you write a program or store some text in file , you add one more file to the system . Files can grow rapidly and if don't organise them propely then it is difficult to handle and locate them . UNIX organises it files in directories and expects us to do that as well .

File system in UNIX is very simple and can be easily managed by the file handling commands . UNIX provides an adequate security mechanism on files so that outside user or members should not tamper the file contents . We will see how to create files , directories , list the filename in its directories and changing file attributes .

The File -

File is nothing but a container or placeholder for storing information .Whenever we create a file in unix and write some text or programm , then the file contains only the contents that we have written to the file and nothing else . A file size is not stored in the file , nor even its name . All the file attributes are stored / kept in a separate area of hard disk , not directly accessible to humans , but only to the kernel . Directory is a place where we keep all our files .

we have categorised files into three types -

  • Ordinary File - Known as regular files , which contains stream of characters
  • Directory File - Directory is nothing but collection of files and other directories , but directory contains names and numbers associated with files .
  • Device Files - All the devices and peripherals are represented by files . To read or write a device , we have to perform operations on these files .

Ordinary ( Regular file) -

An ordinary file or regular file is the most common file type . All programs we write belongs to this type . An Ordinary file is further classified as -

  • Text file
  • Binary File

A text file contains only printable characters, the c program , shell or progam scripts are the text file . a text file is file contains lines of characters where each line is terminated by new line character known as linefeed (LF) .(when we press enter this character is automatically append to the line ) we can see this character by using od command.

A Binary file contains both printable and non printable characters.Most of UNIX commands are binary files .Picture, Sound and Video files are binary files . Displaying content of such files using cat command produce unreadable output and may harm your terminal.

Directory File -

A directory contains no data , but it keeps some details of the files and sub directories that it holds . A directory file contains an entery for every file and subdirectory that it houses .Whenever a user creates a file or a sub directory in a directory following two things are created in hosting direcory -

  • The file name
  • A unique identification number for the file or directory ( Called inode number)

If a directory Scripts conatins an entry for file prog.ksh , then we usualy say that the directory Scripts contains the file prog.ksh . You must not interpret the statement litreally .Always remember that a directory cotains the filename not the file contents

whenever user does any changes in directory like moving or copying files the kernel automatically updates the directory with inode numbers.We will see more about the inode number in our upcoming discussions .

What Are the Standard File naming Convention -

In Unix/Linux file name can be 255 characters long and files may or may not have extension .Unix/linux does not imposes any rules for file creation but its better to use proper names to files - It is recommended that only following character to be used in files . Alphabetic characters and numerals .

The period . hypen - and underscore _

Unix/Linux imposes no rules on file extension e.g a ksh or sh file doesn't need to have .sh or.ksh extension but it helps in identification for file type. Unix is a case sensitive so files Mangep and mangep are 2 different files.

You can use following symbols, all these symbols are valid in filenames , you can use them in your filename . But i would suggest not to go with such complex filenames .

{} [] @ # $ % * + - ^ -
Do not start your file name with - as it may act like an argumnet to a command when you run any command with this file and system may give you soem error

Home Directory -

When you login to your system , Unix/Linux automatically places you in a directory called home directory .It is created by the system when your account is opened. If you login to the system using the name as mangesh , then you will land up in a directory that have the pathname as /home/mangesh ( or something else) . You can change your home directory when you like .

Shell has an built in variable called $HOME (all are capital letters)- which knows your home directory.

echo $HOME

The above path is an absolute pathname , which simply a sequence of a directory names separated by slashes .An absolute pathname shows a file's location with reference to the top i.e root. These slashes act as delimiters to file and directory names , except that the first slash is a synonym for root . The directory mangesh is placed two levels below root.

We can refer to the file abc present in our home directory by running command -

$HOME/abc

There is one more convenient way by which we can go to our home directory using ~ symbol. So $HOME/abc can be written as ~/abc. The symbol ~ can be use to navigate to any user's home directory not just your own . We can even go to the others home directory using ~ symbol. If user singh has file tests in his home directory then mangesh can access

~singh/test - will redirect me to user Singh's home directory .
cd ~ followed by / will refer to your own home directory and when followed by ~<name> will refers to home directory given in name (username).

Home Directory is determined by system administrator at the time of opening user account and its pathname is stored in /etc/passwd

pwd : Know Your Current Directory -

while roaming through different directories sometimes we need to know our current working directory ,to get this run pwd command and it will dispaly the absolute path name of your current directory where you are working now .

pwd

cd : Changing Directory -

During our work we need to move to different directories . To do this we need to run cd command followed by the absolute path name . Currently you are located in your home directory and now you want to go to directory logs .

cd logs

The command cd logs means : " Change your subdirectory to logs under the current directory" . When you invoke this commands UNIX understands that you are under the path where logs subdirectory exists .We can even use the absolute path name for the same effect .

cd /home/mangesh/logs
when cd command is invoked without arguments it will redirect you to your home directory .

mkdir : Creating Directories

Directories can be created with mkdir command . Run the mkdir command followed by directory names of the directories to be created .Drectories will be created under your current working directory.Directory test can be created under your current directory like this :

mkdir test

To create multiple directories in single stroke run - three sub directories will be created under your current working directory

mkdir test logs compare

UNIX goes one step further - following command will create a directory tree - mangesh mangesh/my_scripts mangesh/output - the command will first create mangesh then it will create the other 2 sub -directories under it (Please note the sequence of directory names given in command)

mkdir mangesh mangesh/my_scripts mangesh/output

The order of speciying arguments is important , you obviously can't create a sub directory before creation of its parent directory .e.g you can't run

mkdir mangesh/my_scripts mangesh/output mangesh

Note - Even system fails to create the two sub directories my_scripts and output , it has still created the mangesh directory .

sometimes system fails to create a directory

mkdir test

The reason could be any of the following-

  • The directory name is already exist .
  • There may be a file with same name.
  • user does not have permission to create the directory - (we will see the file and directory permission in our upcoming chapter )

rmdir : removing directories -

We can remove the directories by using rmdir command . To remove a directory , you simply need to run this -

rmdir test [ constraint is directory should be empty]

The only constraint is that -Directory should be empty .We can remove single as well as multiple directories using rmdir .While removing multiple directories we need to follow sequence reverse of mkdir .

rmdir mangesh/my_scripts mangesh/output mangesh

To remove directories using rmdir , here are some rules that we need to follow -

  • To remove a directory you should be placed one directory above of the removing directory
  • you can't remove a directory using rmdir unless it is empty .
  • rmdir and mkdir commands work only in directories owned by the user .- . However users are normally won't be able to create for remove files and directories from othes users Directories (we will see the file and directory permission and ownership in our upcoming chapter )
A sub directory can't be removed with rmdir unless it is empty and one is positioned in its parent directory or above it .But we can remove directories without using rmdir ( we will see in upcoing chapter)

Absolute Path name -

If a User wants to see the content of the file abc , then the usual way to see the contents of file is to run cat command . Run cat abc.txt and content will be displayed on your terminal The command will execute successfully only if user is present in the directory where abc.txt is present

You are in your home directory /usr/mangesh and want to see the content of the file present at /usr/mangesh/programs/test/abc.txt - to see this file you need to go to the location and run cat command . The above path is nothing but an absolute path , seperated by / . Directory test is 4 level below the root directory and file abc.txt is present under test directory , such a path name is called as absolute path

We almost use Absolute path name for reading or executing a scripts , as the files are located in different location so combination of cd command followed by absolute pathname helps us to roam around different locations .

Note - No two files in unix will have same absolute path . i.e. two abc.txt files cannot be present in one location or in a directory but the same file can be in different location

The command we run on Unix/Linus sytem there absolute path is store in PATH variable .So whenever we run any command unix/linux start looking for that command using PATH variable . But if you want to execute any script / code or want open any file you need to go to the absolute path of the file . If you frequently need your script or file then its best to store the directory path in path variable

Relative Path Names -

In our previous example to navigate the /usr/mangesh/programs/test/abc.txt location we have specified the pathname seperated with / Navigation often becomes easier by using a common ancestor(here usr/mangesh) as reference .UNIX offers a shortcut - the relative pathname - that uses either the current or parent directory as reference and specifies the path relative to it . A relative path name uses one of these cryptic symbols .

  • . (a single dot ) - represents the current directory
  • .. (double dots) - This represents the parent directory

Assume that you are placed in /usr/mangesh/programs/test/ and you want to one level up i.e to directory programs , then you can use .. argument along with cd command .

cd ..

You can combine any number of such sets of .. separated by / . For instance if you want to move to /usr/mangesh/ , then simply run

cd ../..

Now lets run to the solitary dot that referes to the current directory . Any command which uses the current directory as argument cal also work with a single dot . This means that cp command which also uses a directory as the last argument can be used with a dot.

cp ../singh /.txt .

The command will copy the .txt file to the current directory . This dot is also implicitly included whenever we use filename as argument , rather than a pathname .For instance cd progs is same as cd./progs

Some Important Unix Directories -

Directory Description
/bin and /usr/bin These are the directories where all the commonly used Unix commands are found path variable alwyas shows these directories
/sbin/ and /usr/bin If there is a command that you can't execute but the system administartor can,then it would probably in one of these directories .You won't be able to execute the system administrator commands
/etc This directory contains the configuration files of the system.You can change a very important aspect of sytem functioning by editing a text file in this directory .your login name and password are stored in files /etc/passwd and etc/shadow
/dev This Directory contains all device files .These files don't occupy space on disk . There could be more subdirectories like pts ,dsk and rdsk in this directory .
/lib and usr/lib Contains all library library files in binary form . You'll need to link your C Programs with files in these directories.
/usr/include This Directory contains all device files .These files don't occupy space on disk . There could be more subdirectories like pts ,dsk and rdsk in this directory .
/usr/share/man This is where the man pages are stored.
/tmp The Directory where user is allowed to create temporary files .These files are wiped away regularly by the system
/var The variable part of the file system.Contains all your print jobs and your outgoing and incoming mail
/home On many systems users are housed here.However, your system may use a different location for home directories

Untitled Document Scroll To Top Untitled Document