User:Spoonbender/Temp/Files.doc

From WikiEducator
Jump to: navigation, search

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


/opt This contains applications and products use to install new products or local applications.



/usr/bin This contains system commands accessible by the users.



/usr/sbin This contains system commands accessible by the users.
/usr/lib This contains archive and shared libraries used for applications.



/usr/share This contains the manual and other vendor independent files.



/usr/share/man This contains files associated with the on-line man documentation.



/usr/local/bin This is where locally developed programs and utilities stored.



/usr/contrib/bin This is where we stored public developed programs and utilities.



 

Dynamic Files


/home This contains one subdirectory for each user account on the system.



/etc This contains system configuration files.



/tmp This is used as temporary space for operating system working files.



/dev This is where special files or device files stored.



/mnt This is used to mount files.



/var/mail This serves as a mailbox for the incoming mail for every user.



/var/tmp This serves as a temporary space for users.
===

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 Displays the directory name of the present working directory.



ls View the files and directories under the present working directory.



cd Used to change your current working directory to another directory.



mkdir Makes directory.



rmdir Removes directory.

 

pwd: Your Present Working Directory

[[Image:]]

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

[[Image:]]

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:


-a Lists all files including the hidden files or files preceded by a dot (.).



-d Displays the characteristic of directories and is usually used with an option –l.



-l Displays a long listing attributes of each file including type, permission mode, number of links, owner, group, size (in bytes), modification date and the file name.



-F To distinguish files from directories. Files are appended by * and directories are appended by /.



-R Recursively displays the files in a given directory and in all subdirectories.

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

[[Image:]]

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.


-p This will create directory if the directory does not exist.



-m mode File permissions are set to mode after creating the directory.

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

[[Image:]]


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&$%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.

Type Regular file or special file



Permissions Access definition for the file



Links Number of files names associated with a single collection of data



Owner User identification of file owner



Group Group identification for file access



Size Number of bytes file contains



Timestamp Data file last modified



Name Maximum of 14 characters


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:


- Regular file



d Directory



l Symbolically linked file



n Network special file



c Character device file



b Block device file


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 > 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] 


 
-l Units of lines.



-b Units of blocks.



-c Units of bytes.



-r - Stands for Reverse.

- Copies lines from the specified starting point in the file in reverse order. The default for r is to print the entire file in reverse order.



-f - Stands for Follow.

- If the input-file is not a pipe, the program will not terminate after the line of the input-file has been copied, but will enter an endless loop, wherein it sleeps for a second and then attempts to read and copy further records from the input-file. Thus it may be used to monitor the growth of a file that is being written by some other process.



-c number -The number option-argument must be a decimal integer whose sign affects the location in the file, measured in bytes, to begin the copying: 


 + Copying starts relative to the beginning of the file.
- Copying starts relative to the end of  the file. 
none Copying starts relative to the end of the file.

The origin for counting is 1; that is, -c+1 represents the first byte of the file, -c-1 the last.



-n number - Equivalent to -c number, except the starting location in the file is measured in lines instead of bytes. The origin for counting is 1; that is, -n+1 represents the first line of the file, -n-1 the last.



File Name of the file you wish to display.


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:


-number The numbers of the file you want to display.
-n number The number of the file you want to display.
Filename The file that you want to display the x amount of lines of.


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.