Mostafa Z. Ali Mostafa Z. Ali mzali@just.edu.jo 1 1 The Linux - - PowerPoint PPT Presentation

mostafa z ali mostafa z ali
SMART_READER_LITE
LIVE PREVIEW

Mostafa Z. Ali Mostafa Z. Ali mzali@just.edu.jo 1 1 The Linux - - PowerPoint PPT Presentation

Fall 2009 Lecture 6 Operating Systems: Configuration & Use CIS345 The Linux Filesystem Mostafa Z. Ali Mostafa Z. Ali mzali@just.edu.jo 1 1 The Linux FileSystem A filesystem is a set of data structures that usually resides on part


slide-1
SLIDE 1

1‐1

Operating Systems: Configuration & Use

CIS345

Mostafa Z. Ali Mostafa Z. Ali

mzali@just.edu.jo

Fall 2009

The Linux Filesystem

Lecture 6

slide-2
SLIDE 2

The Linux FileSystem

  • A filesystem is a set of data structures that

usually resides on part of a disk and that holds directories of files.

  • Filesystems store user and system data that

are the basis of users’ work on the system and the system’s existence

slide-3
SLIDE 3

The Hierarchical Filesystem

  • A hierarchical structure

frequently takes the shape of a pyramid

  • Ex: One example of this type
  • f structure is found by

tracing a family’s lineage: A couple has a child, who may in turn have several children, each of whom may have more children. This hierarchical structure is called a family tree

slide-4
SLIDE 4
  • Directory tree: Like the family tree it

resembles, the Linux filesystem is called a tree.

  • This structure allows you to organize files so

you can easily find any particular one. On a standard Linux system, each user starts with

  • ne directory, to which the user can add

subdirectories to any desired level

slide-5
SLIDE 5

Directory Files and Ordinary Files

  • Ordinary files, or simply files, appear at the

ends of paths that cannot support other paths.

  • Directory files, also referred to as directories
  • r folders, are the points that other paths can

branch off from

  • Pathname: is a series of names that trace a

path along branches from one file to another

slide-6
SLIDE 6

Filenames

  • The maximum length of a filename varies with the type of

filesystem

  • Linux supports several types of filesystems
  • Most of today’s filesystems allow you to create files with

names up to 255 characters long

  • While you can use almost any character in a filename, you

will avoid confusion if you choose characters from the following list

  • Uppercase letters (A–Z)
  • Lowercase letters (a–z)
  • Numbers (0–9)
  • Underscore (_)
  • Period (.)
  • Comma (,)
slide-7
SLIDE 7

Filename length

  • When you share your files with users on other systems,

you may need to make long filenames differ within the first few characters.

  • Systems running DOS or older versions of Windows

have an 8‐character filename body length and a 3‐ character filename extension length limit.

  • Some UNIX systems have a 14‐character limit and older

Macintosh systems have a 31‐character limit.

  • The disadvantage of short filenames is that they are

typically less descriptive than long filenames.

slide-8
SLIDE 8
  • Linux is case sensitive.
  • It is a poor idea to use SPACEs within filenames because a

SPACE is a special character, you must quote it on a command line.

  • Use periods or underscores instead of SPACEs:

joe.05.04.26, new_stuff.

  • If you are working with a filename that includes a SPACE,

such as a file from another operating system, you must quote the SPACE on the command line by preceding it with a backslash or by placing quotation marks on either side of the filename.

$ lpr my\ file $ lpr "my file"

slide-9
SLIDE 9

Filename Extensions

  • A filename extension is the part of the filename following an embedded

period

  • filename extensions help describe the contents of the file, but in some

cases, however, filename extensions are optional

  • Use extensions freely to make filenames easy to understand. If you like,

you can use several periods within the same filename—for example, notes.4.10.01 or files.tar.gz.

slide-10
SLIDE 10

Hidden Filenames

  • A filename that begins with a period is called a

hidden filename (or a hidden file or sometimes an invisible file) because ls does not normally display it

– The .plan file is hidden. – Two special hidden entries—a single and double period (. and ..)—appear in every directory

  • The command ls –a displays all filenames,

even hidden ones

slide-11
SLIDE 11

The Working Directory

  • The directory you are associated with is called

the working directory or current directory

  • The pwd (print working directory) utility

displays the pathname of the working directory

slide-12
SLIDE 12

Home Directory

  • When you first log in on a Linux system or

start a terminal emulator window, your working directory is your home directory

  • To display the pathname of your home

directory, use pwd just after you log in

slide-13
SLIDE 13

Startup Files

  • Startup files, which appear in your home

directory, give the shell and other programs information about you and your preferences

  • Frequently one of these files tells the shell

what kind of terminal you are using and executes the stty (set terminal) utility to establish the erase and line kill keys

slide-14
SLIDE 14

Pathnames

  • a pathname shows the pathnames of directories and
  • rdinary files in part of a filesystem hierarchy. An

absolute pathname always starts with a slash (/), the name of the root directory

slide-15
SLIDE 15

Absolute Pathnames

  • An absolute pathname always starts with a slash (/), the

name of the root directory

  • String all the filenames in the path together, separating

each from the next with a slash (/) and preceding the entire group of filenames with a slash (/).

  • This path of filenames is called an absolute pathname

because it locates a file absolutely by tracing a path from the root directory to the file

  • Example: To list all the files in the /etc/apt directory:

$ pwd /home/sam $ ls /etc/apt Apt.conf.d sources.list sources.list.save trusted.gpg Secring.gpg sources.list.d trustdb.gpg trusted.gpg~

slide-16
SLIDE 16

~ (Tilde) in Pathnames

  • In another form of absolute pathname, the shell expands the

characters ~/ (a tilde followed by a slash) at the start of a pathname into the pathname of your home directory. Using this shortcut, you can display your .bashrc startup file with the following command, no matter which directory is your working directory: $ less ~/.bashrc

  • A tilde quickly references paths that start with your or someone

else’s home directory. The shell expands a tilde followed by a username at the beginning of a pathname into the pathname of that user’s home directory.

  • For example, assuming he has permission to do so, Alex can

examine Scott’s .bashrc file with the following command: $ less ~scott/.bashrc

slide-17
SLIDE 17

Relative Pathnames

  • A relative pathname traces a path from the

working directory to a file. The pathname is relative to the working directory

  • Any pathname that does not begin with the

root directory (/) or a tilde (~) is a relative pathname

  • The simplest relative pathname is a simple

filename, which identifies a file in the working directory

slide-18
SLIDE 18

Significance of the Working Directory

  • Files that are children of the working directory can be referenced by

simple filenames.

  • Grandchildren of the working directory can be referenced by short

relative pathnames: two filenames separated by a slash.

  • When you manipulate files in a large directory structure, using short

relative pathnames can save you time and aggravation.

  • If you choose a working directory that contains the files used most
  • ften for a particular task, you need to use fewer long, cumbersome

pathnames.

slide-19
SLIDE 19

Working w ith Directories mkdir: Creates a Directory

  • The mkdir utility creates a directory
slide-20
SLIDE 20
  • The –F option to ls displays

a slash after the name of each directory and an asterisk after each executable file (shell script, utility, or application).

  • When you call it with an

argument that is the name

  • f a directory, ls lists the

contents of that directory.

  • Use the –p (parents) option

to mkdir to create both the literature and promo directories with one command

slide-21
SLIDE 21

cd: Changes to Another Working Directory

  • The cd (change directory) utility makes another

directory the working directory but does not change the contents of the working directory

  • The second cd command in the figure below does

not have an argument so it makes Alex’s home directory the working directory

slide-22
SLIDE 22

The . and .. Directory Entries

  • mkdir automatically puts

two entries in each directory you create:

  • a single period (.) ‐ working

directory and can be used in its place

  • a double period (..) ‐ the

pathname of the parent of the working directory

  • These entries are hidden

because their filenames begin with a period

slide-23
SLIDE 23

rmdir: Deletes a Directory

  • The rmdir (remove directory) utility deletes a directory.
  • You cannot delete the working directory or a directory that

contains files other than the . and .. entries.

  • You do not have to (nor can you) delete the . and .. entries;

rmdir removes them automatically

  • The rm utility has a –r option that recursively deletes files,

including directories, within a directory and also deletes the directory itself

slide-24
SLIDE 24

Using Pathnames

  • you can use touch to create an empty file
  • With /home/alex as the working directory, the

following example uses cp with a relative pathname to copy the file letter to the /home/alex/literature/promo directory

  • The copy of the file has the simple filename

letter.0610

slide-25
SLIDE 25

mv, cp: Move or Copy Files

  • You can use this utility to move files from
  • ne directory to another (change the

pathname of a file) as well as to change a simple filename.

  • If the working directory is /home/alex, Alex

can use the following command to move the files names and temp from the working directory to the literature directory

  • This command changes the absolute

pathnames of the names and temp files from /home/alex/names and /home/alex/temp to /home/alex/literature/names and /home/alex/literature/temp, respectively

slide-26
SLIDE 26

mv: Moves a Directory

  • Just as it moves ordinary files from one directory

to another, so mv can move directories

  • If new‐directory does not exist, the existing‐

directory‐list must contain just one directory name, which mv changes to new‐directory (mv renames the directory). Although you can rename directories using mv, you cannot copy their contents with cp unless you use the –r

  • ption
slide-27
SLIDE 27

Important Standard Directories and Files

  • Check pages 194‐196 (Just for your

knowledge) … you will need this later!

slide-28
SLIDE 28

Access Permissions

  • Ubuntu Linux supports two methods of

controlling who can access a file and how they can access it:

  • traditional Linux access permissions and
  • Access Control Lists
slide-29
SLIDE 29

ls –l: Displays Permissions

  • When you call ls with the

–l option and the name of

  • ne or more ordinary

files, ls displays a line of information about the file

slide-30
SLIDE 30
  • From left to right, the lines that an ls –l command displays

contain the following information

  • The type of file (first character)
  • The file’s access permissions (the next nine characters)
  • The ACL flag (present if the file has an ACL, page 203)
  • The number of links to the file (page 209)
  • The name of the owner of the file (usually the person who created

the file)

  • The name of the group that the file is associated with
  • The size of the file in characters (bytes)
  • The type of file (first column) for letter.0610 is a hyphen (–)

because it is an ordinary file (directory files have a d in this column)

slide-31
SLIDE 31
  • Although execute permission can be allowed

for any file, it does not make sense to assign execute permission to a file that contains a document, such as a letter.

  • The check_spell file is an executable shell

script, so execute permission is appropriate for it. (The owner, group, and others have execute access permission.)

slide-32
SLIDE 32

chmod: Changes Access Permissions

  • When you own a file, you can use the chmod

(change mode) utility to change access permissions for that file

  • A user with root privileges can access any file
  • n the system
slide-33
SLIDE 33

Symbolic Arguments to chmod

  • You also need execute permission to execute a shell script

directly on the command line. In contrast, binary (program) files do not need to be read; they are executed directly

  • In addition to a (all) and o (other), you can use g (group) and u

(user, although user refers to the owner of the file who may or may not be the user of the file at any given time) in the argument to chmod. The acronym UGO (user‐group‐other) can help you remember how permissions are named

$ ls –l letter.0610 ‐rw‐‐‐‐‐‐‐ 1 max pubs 3355 Jun 22 12:44 letter.0610 $ chmod a+rw letter.0610 ‐rw‐rw‐rw‐ 1 max pubs 3355 Jun 22 12:44 letter.0610

slide-34
SLIDE 34

Numeric Arguments to chmod

  • Numeric arguments to chmod (comprise 3 octal digits) can

be used to specify permissions in place of the letters and symbols

  • Digit1: permissions for the owner
  • Digit2: permissions for the group
  • Digit3: permissions for other users
  • A 1 gives the specified user(s) execute permissions, a 2

gives write permissions, and a 4 gives read permissions

  • A fourth leading digit (Digit0) can be used to control setuid

and setgid (discussed in the next section)

  • Construct the digit representing the permissions for the
  • wner, group, or others by ORing (adding) the appropriate

values

slide-35
SLIDE 35

$ chmod 600 letter.0610 $ ls –l letter.0610 ‐rw‐‐‐‐‐‐‐ 1 max pubs 3355 Jun 22 12:44 letter.0610

  • Next 7 (4 + 2 + 1) gives the owner read, write, and

execute permissions. The 5 (4 + 1) gives the group and other users read and write permissions

$ chmod 755 check_spell $ ls ‐l check_spell ‐rwxr‐xr‐x 1 pubs 852 Jul 31 13:47 check_spell

slide-36
SLIDE 36

Mode Meaning 777

Owner, group, and others can read, write, and execute file

755

Owner can read, write, and execute file; group and others can read and execute file

711

Owner can read, write, and execute file; group and others can execute file

644

Owner can read and write file; group and others can read file

640

Owner can read and write file, group can read file, and others cannot access file

slide-37
SLIDE 37

Setuid and Setgid Permissions

  • When you execute a file that has setuid (set user

ID) permission, the process executing the file takes on the privileges of the file’s owner.

  • For example, if you run a setuid program that

removes all files in a directory, you can remove files in any of the file owner’s directories

  • The ls utility shows setuid permission by placing

an s in the owner’s executable position and setgid permission by placing an s in the group’s executable position

slide-38
SLIDE 38
  • When you use four digits to specify permissions, setting Digit0 to

1 sets the sticky bit, setting it to 2 specifies setgid permissions, and setting it to 4 specifies setuid permissions

slide-39
SLIDE 39

Directory Access Permissions

  • Although the three types of users can read

from or write to a directory, the directory cannot be executed.

  • Execute access permission is redefined for a

directory: It means that you can cd into the directory and/or examine files that you have permission to read from in the directory

slide-40
SLIDE 40
  • When you have only execute permission for a directory,

you can use ls to list a file in the directory if you know its name.

  • You cannot use ls without an argument to list the entire

contents of the directory. In the following exchange, Jenny first verifies that she is logged in as herself. Then she checks the permissions on Alex’s info directory.

  • You can view the access permissions associated with a

directory by running ls with the –d (directory) and –l (long)

  • ptions
slide-41
SLIDE 41
  • Because Jenny does not have read permission for the

directory, the ls –l command returns an error

  • When Jenny specifies the names of the files she

wants information about, she is not reading new directory information but rather searching for specific information, which she is allowed to do with execute access to the directory

slide-42
SLIDE 42
  • Now ls –l works just fine without arguments, but she still

cannot read financial (This restriction is an issue of file permissions, not directory permissions.)

Alex gives others read access to his info directory

slide-43
SLIDE 43
  • To change file access permissions you use chmod command. It has ‐R or ‐

recursive option that change files and directories recursively. For example $ chmod ‐R 0755 directory

  • However, if you need to apply conditional file permissions recursively use

combination of find and chmod command.

  • Find all files in /home/user/demo directory

$ find /home/user/demo ‐print

  • Now find all files in /home/user/demo directory with permission 777

$ find /home/user/demo ‐perm 777 ‐print

  • Next you need to apply chmod on all these files using ‐exec option:

$ find /home/user/demo ‐perm 777 ‐print ‐exec chmod 755

slide-44
SLIDE 44

Links

  • A link is a pointer to a file. Every time you

create a file by using vim, touch, cp, or any

  • ther means, you are putting a pointer in a

directory.

  • This pointer associates a filename with a place
  • n the disk
  • You can make it easy for other users to access
  • ne of your files by creating additional links to

the file

slide-45
SLIDE 45
  • To share a file with another user, first give the

user permission to read from and write to the file

  • You may also have to change the access

permissions of the parent directory of the file to give the user read, write, or execute permission

  • Once the permissions are appropriately set, the

user can create a link to the file so that each of you can access the file from your separate directory hierarchies

slide-46
SLIDE 46
  • A link can also be useful to a single user with a large directory hierarchy.

You can create links to cross‐classify files in your directory hierarchy, using different classifications for different tasks

  • A file named to_do might appear in each subdirectory of the correspond

directory—that is, in personal, memos, and business

  • If you find it difficult to keep track of everything you need to do, you can

create a separate directory named to_do in the correspond directory

  • You can then link each subdirectory’s to‐do list into that directory
  • For example, you could link the file named to_do in the memos directory

to a file named memos in the to_do directory

Although it may sound complicated, this technique keeps all your to‐do lists conveniently in one place

slide-47
SLIDE 47

Symbolic Links

  • Linux supports symbolic links, also called soft links or

symlinks

  • a symbolic link is an indirect pointer to a file (the

directory entry contains the pathname of the pointed‐ to file

  • A symbolic link can point to any file, regardless of

where it is located in the file structure

  • A major advantage of a symbolic link is that it can point

to a nonexistent file. This ability is useful if you need a link to a file that is periodically removed and recreated

  • A symbolic link always points to the newly created file

and does not interfere when you delete the old file

slide-48
SLIDE 48

ln: Creates a Symbolic Link

  • You use ln with the ‐‐symbolic (or –s) option to create a

symbolic link

  • The following example creates a symbolic link /tmp/s3 to

the file sum in Alex’s home directory

  • When you use an ls –l command to look at the symbolic link,

ls displays the name of the link and the name of the file it points to. The first character of the listing is l (for link)

slide-49
SLIDE 49
  • The sizes and times of the last modifications of the two files

are different

  • Symbolic links are literal and are not aware of directories.
  • A link that points to a relative pathname, which includes

simple filenames, assumes that the relative pathname is relative to the directory that the link was created in (not the directory the link was created from).

  • In the following example, the link points to the file named

sum in the /tmp directory. Because no such file exists, cat gives an error message:

slide-50
SLIDE 50

rm: Removes a Link

  • When you remove a file, be sure to remove all

symbolic links to it. Remove a symbolic link in the same way you remove other files: