P. 1


|Views: 2|Likes:
Published by ScribberTiger

More info:

Published by: ScribberTiger on Jul 03, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as ODP, PDF, TXT or read online from Scribd
See more
See less





Intro to Unix/Linux

Fall 2010 ~ Eric Meyer

Lecture 2 Working with Files and Directories

 Discuss and explain the UNIX file system

 Define a UNIX file system partition

 Use the mount command to mount a file system

 Discuss relative and absolute path addressing


Objectives (cont.)
 Diagram the UNIX file system hierarchy

 Navigate the file system

 Create new directories to store files

 Copy files from one directory to another

 Set file permissions for other user access to directory and



 Read Chapter Two For a good reference read this


Files – A well defined repository of information • Program or component of a program • Arbitrary text • An executable (binary text) – Special files called directories contain or point to other files Structure of Directories – Hierarchically structured like an inverted tree – / is the starting directory or “root” Pathnames – Locating a file by following a sequence of directories until you reach the file – / is also the separator between the directories and final file


Understanding the UNIX File System
 A file is the basic component for data storage
 UNIX considers everything it interacts with a file

 A file system is UNIX’s way of organizing files on mass

storage (disk) devices
 A physical file system is a section of the hard disk that

has been formatted to hold files

 The file system is organized in a hierarchical structure

similar to an inverted tree

Example set of directories and files













Understanding the Standard Tree Structure
 The structure starts at the root level
 Root is the name of the file at this basic level and it is

denoted by the slash character (/)

 A directory is a file that can contain other files and


 A subdirectory is a directory within a directory
 The subdirectory is considered the child of the parent



Using UNIX Partitions
 The section of the disk that holds a file system is called a


 When installing UNIX, one of the first tasks is deciding

how to partition a storage device, or hard disk

 Hard disks may have many partitions

 UNIX partitions are given names
 LINUX uses hda1 and hda2  Or sda1 and sda2


Using UNIX Partitions
 Storage devices are called peripheral devices

 Peripheral devices connect to the computer through

electronic interfaces
 IDE - Integrated Drive Electronics  SCSI - Small Computer System Interface



More on pathnames

Absolute pathnames start at root – /home/albert/misc/Starbug – /bin Relative pathnames start at current directory – Suppose current directory is “home”, then: – joseph/Hologram would refer to the same file as: – /home/joseph/Hologram Special symbols for current directory and parent – .. is parent directory – . is current directory Referencing user directories – ~albert is the absolute pathname to the user directory “albert” (in the directory “home” (also sometimes called “users” or “u”) – ~/ is shorthand for the absolute path to your own user directory

Exploring the Root File System
 UNIX must mount a file system before any programs can

access files on it

 To mount a file system is to connect it to the directory tree


 The root file system is mounted by the kernel when the

system starts


Exploring the Root File System
 The root directory contains sub-directories that contain files:
 /bin contains binaries, or executables needed to start the

system and perform system tasks
 /boot contains files needed by the bootstrap loader as

well as kernel images
 /dev contains system device reference files



Exploring the Root File System
 Root subdirectories continued:
 /etc contains configuration files that the system uses

when the computer starts the shared library images system administrator

 /lib contains kernel modules, security information, and  /mnt contains mount points for temporary mounts by the  /proc is a virtual file system allocated in memory only


Exploring the Root File System
 Root subdirectories continued:
 /root is the home directory of the root user, or the

system administrator

 /sbin contains essential network programs used only by

the system administrator processing cycles

 /tmp is a temporary place to store data during  /var contains subdirectories which have sizes that often

change, such as error logs


Using the Mount Command
 Users can access mounted file systems which they have

permission to access

 Additional file systems can be mounted at any time using

the mount command

 To ensure system security, only the root user uses the

mount command


Understanding Paths and Pathnames
 To specify a file or directory, use its pathname, which follows

the branches of the file system to the desired file

 A forward slash (/) separates each directory name  The UNIX command prompt may indicate your location

within the file system name

 Use the UNIX pwd command to display the current path


UNIX Commands

Commands typically refer to a Unix program located, for example, in /usr/bin – Structure of a command is typically: commandname [flags] [parameters] Flags – Commands may accept one or more flags – Flags start with “-” and are separated from other flags and parameters by one or more spaces – Individual flags may be combined Parameters – Parameters are often filenames


ls –a –l ls –al cp /home/albert/cprogram sources cp ../testfiles/part1 . wc –c –l thatfile


Some file commands
ls cat more touch cp mv rm list files view file contents view file contents (pause each screen) creates file / updates time stamp copy file to a new file move file to a new directory, rename file delete file


Some directory commands
pwd mkdir rmdir cd display absolute pathname to current directory create directory remove directory navigate directories


Navigating the File System
 To navigate the UNIX directory structure, use the cd (change

directory) command

 UNIX refers to a path as either:
 Absolute - begins at the root level and lists all

subdirectories to the destination file
 Relative - begins at your current working directory and

proceeds from there


Listing Directory Contents

The ls (list) command displays a directory’s contents, including files and subdirectories


Listing Directory Contents

The system normally uses hidden files to keep configuration information and for other purposes


Characters in filenames

File names can contain any characters except “/”, but it is recommended that you use upper or lower case letters, numbers, and the characters “-” “.” For example although a file name could contain a space or spaces: confusing name commands using this would not work correctly unless you tell the shell to not break an argument at the spaces by quoting the filename. rm “confusing name”



an asterisk “*” matches any number of characters in a filename – con* will match con, condor, constant.exe – *.c will match all files that end in .c – rm * will remove all the files in a directory a “?” matches any single character in a filename – b?t will match bit, bot, bat. It will not match bt or boot square brackets “[]” will match any one of the characters in the brackets. A hyphen “-” can be used to match any of a range of consecutive characters. – [bhr]at will match bat, hat and rat – chap[5-8].c will match chap5.c, chap6.c, chap7.c and 29 chap8.c

Some useful commands
• •

pwd – Prints the absolute pathname of the current directory ls –al – The ls command lists the files in a directory. The a flag displays all the files. The l flag gives detailed information. touch – The touch commands creates an empty file or updates the timestamp of an existing file cp – The copy command copies the contents of a file or directory to another file or directory (two parameters). The i flag asks before it replaces an existing file; the r flag recursively copies all subdirectories also.


Useful commands continued

– – –

The move command renames a file (it takes two arguments) mv oldfilename newfilename If the second argument is a directory it moves it to that directory Counts the characters, lines, or words in a file wc –w essay

– –

cd Changes the current directory to another one – cd assignment1 – cd .. – cd ../assignment1 passwd – Run this to change your password


Managing Directories and Files
 mkdir (make directory) command
 Create a new directory

 rmdir (make directory) command
 Delete an empty directory

 cp (copy) command
 Copy files from one director to another

 rm (remove) command
 Delete files


ls Example Output

Current permissions can be viewed using ls -l – First line is the number of disk blocks ( 1 block is 512 bytes) taken up by all the files

[sudhir@www scop3348]$ ls -al total 596 drwxr-xr-x 3 sudhir fac 4096 drwxr-xr-x 11 sudhir fac 4096 -rw-r--r-1 sudhir fac 4631 Assignment1.txt drwxr-xr-x 3 sudhir fac 4096 -rw-r--r-1 sudhir fac 51693 -rw-r--r-1 sudhir fac 247017 -rw-r--r-1 sudhir fac 92123 -rw-r--r-1 sudhir fac 175410 [sudhir@www scop3348]$

Jan 22 17:38 . Jan 3 18:30 .. Jan 18 16:10 Jan Jan Jan Jan Jan 9 22 18 16 22 17:07 17:35 10:51 09:05 17:24 index_files index.html Lecture1.pdf Lecture2.pdf Lecture3.pdf

Columns in the Display

• •

First entry in a line is the mode – The first character is d for directory, else - for a normal file – The remain 9 characters in groups of 3 are r, w, x permissions for user, group and other respectively (indicates not having that permission) Second entry indicates number of links to the file (usually 1) Third entry is the user id of owner and the fourth entry is the group id Fifth entry is the number of bytes of the file Sixth entry is the date the file was last modified


File Permissions

3 types of processes can access a file – user or owner: process spawned by user who created file
– –

group: process spawned by members of the same group other: process spawned by anyone else

Permission types – read: access file or list directory – write: write to / remove file (directory) – execute: run file as a program or enter directory


Setting File Permissions


Setting File Permissions

File Permissions
r w x r x r x Owner has read Owner has write Owner has execute Group has read Group does not have write Group has execute Others have read Others do not have write Others have execute


Setting File Permissions for Security
 chmod command
 To set file permissions  Settings are read (r), write (w), execute (x)  The three types of users are owners, groups, and others

 Setting permissions to directories
 Use the execute (x) to grant access


Changing Permissions

Using the chmod command to set permissions

Numeric (using octal) • Directly set the permissions for u, g, o using each 3 bit value as an octal value • chmod 754 lecture1.pdf will set to 111 101 100 or rwx r-x r-• chmod 700 lecture1.pdf will set to 111 000 000 or rwx --- --• chmod 644 lecture1.pdf will set to 110 100 100 or rw- r-- r--


Changing Permissions (cont)

Symbolic • Format: chmod [who] [operation] [permissions] <filename> • who is one or more of u, g, o • operation is + (add), - (remove), = (set) • Permissions are one or more of r, w, x

Examples chmod go-rwx myfile.doc chmod g+w myfile.doc chmod u=rwx,g=rx,o=r myfile.doc




Chapter Summary
 In UNIX, a file is the basic component for data storage and

UNIX considers everything a file

 A file system is the UNIX system’s way of organizing files

on mass storage devices and each file is referenced using a correct and unique pathname

 The standard tree structure starts with the root (/)


 The section of the mass storage device (or disk) that holds

a file system is a partition


Chapter Summary
 A path serves as as a map to access any file on the


 You may customize your command prompt to display the

current working directory name, the current date and time, and several other items

 The ls command displays the names of files and

directories contained in a directory

 Wildcard characters (i.e. *, ?) can be used in a command

such as ls and take the place of other characters in a file name

Chapter Summary
 Use the mkdir command to create a new directory, as long

as you own the parent

 Use the chmod command to set permissions such as read

(r), write (w), execute (x) for files that you own

 Use the cp command to copy a source file to a destination

file and directory


You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->