User:Spoonbender/Temp/Files.doc

The UNIX File System
Before we discuss the UNIX file system, let us first identify what a file system is. File system is a hierarchical structure of files and directories having its separate root directory. The purpose of this file system is to have an organized and manageable files and directories for the entire UNIX system.

The File system is one of the many good features of UNIX system. This contains tens of thousands of files. If a program is to be made, you will need to add files to a file system and once executed, you will need to compile it and then add the files to the file system. The files may grow rapidly and if you will not organize these files you will not able to access and transfer files from one directory to another. The file system is proved to be useful and it is widely adopted by other systems even Windows operating system.

To manage the data processed by the system, there is what we call files to store those data. And to store these files and subdirectories we have directories. This is how the UNIX file system organizes files. Managing these files is analogous to a filing cabinet. The filing cabinet is the file system, every drawer in a filing cabinet is a directory, folders inside the drawer are subdirectories and reports inside the folders are actually files.

The System Hierarchy
The system hierarchy makes an easy and effective way of organizing files and directories. On top of the system hierarchy is the root directory denoted by a forward slash (/). The UNIX system provides commands to create new directories. There are also commands in moving a report from one folder to another.

Files can be categorized as static files or dynamic files. Static are files that are rarely changed while dynamic are files that are frequently changed.

Static Files

Dynamic Files

===

Path Names
A Path name is a sequence of one or more filenames separated by a delimiter /. This also represents the route from the parent directory to the desired file or directory.

When you log into a system, you will be positioned by the system in a home directory designated to you. This directory is your home directory given by the system administrator.

Path name can either be absolute or relative.

Absolute Path Name


 * Gives the entire designation of the file or directory location.
 * Always begin at the root directory.
 * Always begin with a /.
 * Independent of the current working directory.
 * Unique across the entire directory.

Relative Directory


 * Always begin at working directory.
 * Does not start with the root directory (/).
 * Unique relative to the current location.
 * Often shorter than absolute path name.

The . and .. Directory
The. and .. are automatically created when any directory is created. These are commonly used when designating relative path names. Dot (.) represents your current or previous working directory while dot dot (..) represents the directory above your current working directory.

Basic File System Commands

We shall study here some basic commands for file system. These commands will help us create directory, remove directory etc. The basic commands of file system include the following

pwd: Your Present Working Directory


Every time you are logged in to a system you are positioned in a certain directory by the system. This directory is called working directory.

The pwd command means a present working directory that reports the absolute path name of your current working directory.

The pwd command is also used to verify your current location as you move through the file system.

Syntax:

pwd

Example:

$ pwd

/home/user3

ls: Listing the Contents


The ls command is used to display all the files and directories of your present working directory without any arguments. But if the command is appended by the file names, only the files and directories associated with that file are displayed.

Syntax:

ls [-adlFR] 

ls accepts the use of many options. If this command is executed with options this will display additional information. Some of the options used are the following:

Example:

$ ls

f1owen

$ ls –F

f1*owen/

$ ls –aF

.profilef1*owen/

$ ls owen

f1

$ ls –F /home

.profileuser1/ user2/user3/

cd: Changing your Current Directory



The cd command of UNIX system will allow you to move from one location to another.

Syntax:

cd pathname

where pathname is the absolute path name of the location or directory you want to go. Executing cd command without any argument will take you to your home directory. If you are lost just type cd and the system will bring you back to your home directory.

Example:

$ pwd

/home/user3

$ cd owen

$ pwd

/home/user3/owen

mkdir: Making Your Own Directory
The mkdir command is a command used to create your own directory. Directories that you created will help you manage and organize your files. The. and .. directories are automatically created when you make a directory. Creating directory does not necessarily mean that you are changing your current location to the directory made.

Syntax:

mkdir [-p] dir_pathname (s) 

You can use the following options in creating directory. There are other options that are not discussed in this module, to know those options you can use the man page entry for mkdir command.

Creating subdirectory under a non-existing directory is not allowed, you must create first the directory before the subdirectory unless you use the –p option.

Example:

Based on figure create a directory fruit and subdirectories apple, grapes and peach under your present working directory.

$ pwd

/home/user3

$ mkdir fruit

$ mkdir fruit/apple fruit/grapes fruit/peach

or

$ mkdir –p fruit/apple fruit/grapes fruit/peach

rmdir: Deleting Directories



The rmdir will help you remove unnecessary directories. Be sure that the directory you are removing is empty (excluding the dot and dot dot files). You cannot remove directories between your present working directory and the parent or root directory. Sometimes you cannot remove directory because of permission, you must have the permission first before you can remove a directory.

Syntax:

rmdir dir_pathname(s)

Example:

Remove directory fruit and subdirectories under fruit.

$ pwd

/home/user3

$ rmdir fruit

rmdir: fruit not empty

$ rmdir fruit/apple fruit/grapes fruit/peach fruit

The File
A file is a container for storing information and is just actually a sequence of characters. The Unix system doesn’t put any restriction on the structure of a file.

There are actually three categories of a file, namely:

Ordinary File


 * Also called regular file
 * Represented by programs and is the most common type of file in Unix.

Directory File


 * Contains no data, but houses the names of other files and subdirectories.
 * A directory file contains two fields – the name and the identification number (which is called as inode number).

Device File


 * Represented by devices (printer, CD-ROMs, floppy disk), which provides communication medium so that interaction with the disk file actually results in activation of the physical device.
 * Any output directed to it will be reflected onto the respective physical device associated with the filename.

The Filename
A filename is a name of the file that is known by the operating system consisting of up to 255 characters.

Files may or may not have extensions (used to describe the application), and consist of ASCII characters except the forward slash ‘/’. Some of the valid file names are:

LIST. chapterIndex. Val^id&amp;$%dfggld

Filenames are case sensitive. In framing filenames, use only the following characters:


 * Alphabets
 * Numerals
 * Period
 * Hyphen
 * Underscore

Special characters like <, >, &, ;, ! and | are some few characters that may cause problems.

Unix doesn’t impose any restriction on the extension of files. A file may have as much dots embedded in its name like: x.y.z is a valid filename and can even begin or end with a dot.

The File Characteristic
The Unix file has several characteristics, and these can be displayed using the ls –l command.

A File Name has several specifications, usually contains alphabets (a-z, A-Z), numbers (0-9), dot, dash, and underscore, contains a maximum of 255 characters if long file names are supported, and maximum of 14 characters.

The Unix system supports many types of files and the file type is displayed by using the command ls –l. The common types are:

cat: Concatenating Files
The cat command (concatenation) is used to join and display text files.

Syntax:

cat file

Example:

$ cat love.doc

By using the cat command the contents of the several files can be combined into another file. The command line below shows this function:

$ cat chap1 chap2 chap3 &gt; chap4

The downside of the "cat" command is that the contents of the file may scroll off of the screen. In cases where a file is too large to fit on the screen, it would be better to use the "more" command to display the file.

more: Paging File Output
The more command of Unix is used to look at the contents of a file. It lets the user view text files. It displays files one screenful at a time.

Syntax:

more filename

Example:

$ more lecy.doc

This command displays the /etc/chap1 file one screenful at a time.

$ more /etc/chap1

When more has control of your screen, you can hit the spacebar to display the next screenful of information, use the '/' to search for text, or type 'q' to quit.

tail: Displaying the End of a File
The tail command writes the last lines of a file to the screen.

Syntax:

tail [+ number] [-l] [-b] [-c] [-r] [-f] [-c number | -n number] [file] 

Example:

$ tail –1 love.doc

I love you all.

head: Displaying the Beginning of a File
The head command displays the first ten lines of a file, unless otherwise stated.

Syntax:

head [-number | -n number] filename 

Options:

Example:

$ head –1 love.doc

Love is blind.

cp: Copying Files
The cp command is used to copy a directory, files, or copy files to a directory.

Syntax:

cp [-i] file1 new_file 

cp [-i] file [file…] dest_dir 

cp –r [-i] dir [dir..] dest_dir 

Example:

$ cp -r dir1 dir2

This copies all the contents of dir1 to dir2. The directory is created if it does not exist. If dir2 does exist then dir1 is created as a subdirectory within it.Using the command cp, this command line one can create a new copy of a directory:

$ cp -r predoc document

This copies all the files and subdirectories in the directory prelim into a new directory document.

mv: Moving and Renaming Files
In Unix, the command used to move a file is the mv command. It could also be used to rename a file.

Syntax:

mv [-i] file new_file 

mv [-i] file [ file…] dest_dir 

mv [-i] dir [dir…] dest_dir 

Examples:

To rename, indicate first the name of the file to be changed followed by the new name:

mv chap1 list1

This renames the file chap1 as the file list1.

To move a file to another directory, the method used is the same as in renaming a file. Indicate first the name of the file in the current directory to be moved followed by the new directory.

To move a file to another directory and rename it:

mv chap1 list1/book2

This moves the file chap1 to the filename book2 in the subdirectory list1.

To move and rename a directory:

mv list1 ../virtual/media

This moves the directory list1 from the current directory to the directory ../virtual and renames it as media.

rm: Deleting Files
The rm command of Unix is used to delete files from a directory.

Syntax:

rm [-if] filename [filename…] 

rm –r[-if] dirname 

Examples:

rm chap1

where chap1 can be in the current directory:

A number of files can also be removed at the same time.

rm list1 list2 list3

Remove can also be used without asking for a confirmation. To do this, use the -force option.

By using the rm command together with the –i option this would prompt the user before deleting any of the files specified. -i option stands for inquire.

rm -i list1 list2 list3

Also the rm command can delete all files in the present directory whose filename ends with the specified extension name of a file. The example below shows that the files with an extension of .txt is deleted from the current directory:

rm *.txt

Having this command would mean that all files in the current directory whose filename begins with the characters "list" would be deleted.

rm list*

Using the rm command, a directory together with all of its contents could also be deleted.

rm -r book

ln: Creating Links
The ln command of Unix is used to create links. The command line below shows how to link files in the same directory.

Syntax:

ln file new_file

ln file [file…] dest_dir 

ln chap1 list1

This creates a link list1 to the file chap1 in the current working directory. It is also possible to make several links to a file in different directories:

ln chap.rtf ../helpdata/book1 /public/helpdoc/part1

This links chap.rtf to ../helpdata/book1 and /public/helpdoc/chap1.