CS 497C – Introduction to UNIX Lecture 12: - The File System

Preview:

DESCRIPTION

CS 497C – Introduction to UNIX Lecture 12: - The File System. Chin-Chih Chang chang@cs.twsu.edu. Relative Pathnames (. and ..). A relative pathname is a pathname which defines the location of a file with with respect to the current directory. - PowerPoint PPT Presentation

Citation preview

CS 497C – Introduction to UNIXLecture 12: - The File System

Chin-Chih Changchang@cs.twsu.edu

Relative Pathnames (. and ..)

• A relative pathname is a pathname which defines the location of a file with with respect to the current directory.

• It uses the symbols . (a single dot) and .. (two dots) to refer to the current and parent directories, respectively.

• The command cd .. change your directory to the parent directory of the current directory.

Relative Pathnames (. and ..)$ pwd/home/remeo/progs$ cd ../..$ pwd/home• For example, if you are in your home

directory /home/romeo and you want to display the contents of /etc/service, you can use either one of following ways:

Relative Pathnames (. and ..)$ cat /etc/service$ cat ../../etc/service• You’ll sometimes need to precede a

command with ./ (a dot and a /). • Assume you want to use a cat program

written by you in the current directory, you can run your own cat and ignore the one in /bin:

$ ./cat note

mkdir: Making Directories

• Directories are created with the mkdir (make directory) command.

• The command is followed by the names of the directories to be created.

• You can create more than one directory in one command.

mkdir doc src news

mkdir: Making Directories

• Sometimes, the system refuses to create a directory because:– The directory may already exist.– There may be an ordinary file by that name in

the current directory.– The permissions set for the current directory

don’t permit the creation of files and directories by the user.

rmdir: Removing Directories• The rmdir (remove directory) command

removes directories.• You can delete more than one directory in

one command.• They are two important rules when deleting

directories:– You can’t use rmdir to delete a directory unless

it is empty. – You can’t remove a subdirectory unless you are

placed in a directory hierarchically above the one you choose.

cp: Copying Files

• The cp command copies a file or a group of files.

cp chap1 unit1

• If the destination file (unit1) doesn’t exist, a new file will be created. If not, it will be overwriten without any warning from the system.

• If unit1 is a directory, the file will be copied into that directory.

cp: Copying Files

• You can copy multiple files to a directory.

• For instance, to copy the file chap1, chap2, and chap3 to the progs directory, you can use:

cp chap1 chap2 chap3 progs

• The UNIX system uses a set of special characters called metacharacters that you can use for matching more than one file.

cp: Copying Files

• cp is often used with the shorthand notation . (dot) to signify the current directory as the destination.

• For instance, to copy the file .profile from /home/juliet to your current directory, you can use either of the two commands:

cp /home/juliet/.profile .profile

cp /home/juliet/.profile .

cp: Copying Files• You can use the * as a shorthand for

multiple filenames sharing a command string.

• For example, you can copy chap01, chap02, and chap3 in this way:

copy chap* progs• The –i (interactive) option warns the user

before overwritting the destination file. • The –r (recursive) option makes it possible

to copy an entire directory.

rm: Deleting Files

• The rm command removes files and makes space available on disk.

• It normally operates silently and should be used with caution. It can delete more than one file with a single instruction:

rm chap01 chap02 chap03

rm chap*

rm progs/chap01 progs/chap02

rm: Deleting Files• Unless used with –r option, rm won’t remove

a directory.

• You may need to delete all files of a directory, as part of a cleaning-up operation:

$ rm *

• The –i (interactive) option makes the command ask the user for confirmation before removing each file.

rm: Deleting Files• With the –r option, rm performs a tree walk

– a thorough recursive search for all subdirectories and files within these subdirectories.

• Using the rm –r * will delete all files in the current directory and all subdirectories and their files.

• rm won’t delete any file if it’s write-protected.

• The –f (force) option overrides this protection also.

Recommended