1. Trang chủ
  2. » Công Nghệ Thông Tin

Directory and File Commands

27 251 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Directory and file commands
Thể loại Module
Định dạng
Số trang 27
Dung lượng 84,82 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Directory and File Commands 6 Objectives Upon completion of this module, you should be able to  Control screen output using control characters  Determine a file’s type with the file co

Trang 1

Directory and File Commands

6

Objectives

Upon completion of this module, you should be able to

 Control screen output using control characters

 Determine a file’s type with the file command

 Display the contents of text files using the cat, more, head, and tail commands

 Determine word, line, and character count using the wc command

 Compare the contents of text files using diff and cmp

 Create empty files or update access time of existing files using the touch command

 Create and remove directories using mkdir and rmdir

 Manage files and directories using the mv, cp, and rm commands

 Save the output from a command into a file

 Pass output from one command to another using a pipe

 Use the tee command within a pipeline to create text within a file

Discussion – What tasks do you need to complete to manage your files and

directories?

- Create , edit, delete, copy, a file

- Identity size of file

- View content of a file

- Compare content of two files

Control Characters

Control characters are used to perform specific tasks such as stopping andstarting screen output When displayed on the screen, the Control key isrepresented by the caret symbol (^) To enter a sequence of controlcharacters, hold down the Control key and press the appropriate character onthe keyboard

The Control-s and Control-q characters were originally needed by teletype operators and are rarely used today.

The following control characters can be used:

Trang 2

Table 6-1 Control Characters

Control Characters Purpose

Control-s Stops screen output

Control-q Resumes screen output

Control-c Interrupts current activity

Control-d Indicates end-of-file or exit

Control-u Erases the command line

Control-w Erases the last word on the line

Control-l Clear screen

Shift-Page Up Scroll up one page

Shift-Page Down Scroll down one page

The actual character in the shell appears as ^C , even though you press the

Control key and the c key at the same time.

Determining File Type

There are many types of files found on a Linux system The type of file can bedetermined by using the file command This information can be importantwhen a user is attempting to open or read a file Determining the file typecan help a user decide which program or command to use to open the file.The output from this command will most often be one of the following:

Text – Examples include ASCII text, English text, commands text, and

executable shell scripts The text file type also includes executableshell scripts This type of file can be read using the cat or morecommands, which are discussed in this module, and edited using vi oranother editor

The file command determines file type by referencing the first two bytes of the

file See the contents of /etc/magic

and extensible linking format (ELF) code files and other dynamicallylinked executables This file type indicates that the file is a command orprogram The strings command, shown on the next page, will printout readable characters in this type of file (The

output produced by strings is easily interpreted by someone with aprogramming background The command is introduced here solely as amethod for demonstrating the printable characters of an executablefile.)

Trang 3

Data – Data files are those which are created by an application running

on the system In some cases the type of file is indicated; for example,FrameMaker document When the application in which this file wascreated cannot be determined by the file command, the output willsimply indicate data file The only way to read a data file is todetermine which application created it and open the document withthat application If you have many applications on your system, thiscan be a time consuming process

For information on other file types, see the man pages

Command Format

file filename( s)

Example Text File

$ file dante

dante: English text

Example Data File

$ cd /home/user2/dir1/coffees

$ file beans

beans: Frame Maker Document

FrameMaker must be used to read the beans file

Example Executable File

$ file /usr/bin/cat

/usr/bin/cat: ELF 32-bit MSB executable SPARC

Version 1, dynamically linked,stripped

The strings command must be used to read /usr/bin/cat For thisexample, use strings followed by the file name as the commandformat

Trang 4

Using the cat Command

The cat (concatenate) command displays the contents of a text file onthe screen It is often used to display short text files; because catflashes through the entire file rapidly without pausing, it is unsuitablefor files longer than one screen in length The cat command is moreoften used to join two or more files into one large file

Mention “Alighieri” and few may know about whom you

are talking Say “Dante,” instead, and the whole world

knows whom you mean For Dante Alighieri, like Raphael

Using the cat Command to join two files into one

$ cat filename1 filename2 > file3

This example joins filename1 and filename2 files into filename3 file.

Note – If filename3 is existed, this command will overwrite it You can

use the same type to join more files

Trang 5

Using the more Command

Use the more command to display the contents of a text file to thescreen one screen at a time If the information in a file is longer thanone screen, the following message appears at the bottom of thescreen: More (n%)

where n is the percentage of the file already displayed.

The on-line manual pages use the more utility for display purposes, sothe scrolling keys in the following table are the same ones you used todisplay man pages

Note – Using cat or more to read executable or binary files can cause

a terminal or window to hang

Command Format

more filename(s)

At the More prompt, you can use the following keys to control thescrolling capabilities:

Table 6-2 Scrolling Keys

Scrolling Keys Purpose

Spacebar Scroll to the next screen

Return Scroll one line at a time

b Move back one screen

f Move forward one screen

h Display a Help menu of more features

q Quit and return to the shell prompt

/string Search forward for string

n Find next occurrence of string

Using the head Command

Use the head command to display the first n lines of one or more files.The first 10 lines are displayed by default if the – n option is omitted

Trang 6

Using the tail Command

Use the tail command to display the last n lines of a file The last 10lines are displayed by default if the -n option is omitted

Trang 7

Displaying Lines From a Specific Point in the File

Using the wc Command

Use the wc command to display a line, word, or character count of afile This command is useful when trying to determine characteristics of

a file Whereas the size can be determined in bytes by using the ls -lcommand, much more information is obtained with wc

Locating Text Differences With the cmp Command

Comparing files to determine differences between them can be doneusing the cmp command This command produces no output if the filesare exactly the same, or it provides the byte and line numbers at whichthe first difference occurred if there are discrepancies between thefiles

Trang 8

1 Do a long listing and compare the file sizes.

-rw-r r 1 user2 edu 3528 July 6 16:19 /home/user2/cshrc-rw-r r- 1 user2 edu 3528 July 6 16:19 /home/user2/.cshrc

2 From the long listing, identify the date the files were last changed Ifthis date signifies that the files are the same, use the cmp command

$ cmp ~/cshrc ~/.cshrc

cshrc /home/user2/cshrc differ: char 1638, line 42

This output identifies the first occurrence of a difference between thetwo files The difference occurred at the 1,638th character position andwas found on line 42

You can now manually locate any further discrepancies and takewhatever action you deem necessary

Note – The cmp command is quite useful when comparing binary files,but less useful for text files For more information on this command,see the man pages

Using the cmp -l to see the differences in details

offset character in file2 (in decimal)

character in file1 (in decimal)

Using the diff Command

Trang 9

Another command used for finding differences between files is the diffcommand The output of this command will display line-by-linedifferences between two text files.

Command Format

diff [option] file1 file2

Options

-i Ignores the case of letters; for example, A is equal to a

-c Produces a listing of differences with three lines of context With

this option, output format is modified slightly: output begins withidentification of the files involved and their creation dates.Following a

line of a dozen *s, the line numbers of file1 that are to bedisplayed are listed The actual lines from file1 are thendisplayed, appended by a – for those lines that are different fromfile2 The same display follows as it applies to file2, with a +appended for those lines that are different from file1

Using diff to Compare Files

$ diff -c fruit fruit2

*** fruit Fri May 8 11:32:49 1998

- fruit2 Fri May 8 14:55:21 1998

Trang 10

File names may contain one or more extensions, usually appended to file by an application Extensions are usually one to three characters

that are appended to the end of a file name and are preceded by aperiod (.) You may choose to use this convention when naming files,but it is not a necessary part of a file name

 Directory names generally do not contain extensions, but there are norules against it

Note – To help distinguish between files and directories, some site

administrators prefer their users to start filenames with lowercase letters anddirectory names with capital letters

Creating Files

You can create new, empty files using the touch command

Trang 11

mailbox project research

Note – The touch command creates an empty file if the file name specified does not exist Otherwise, the access/modification time of theexisting file is updated

Creating and Appending Using the tee

Command

Suppose you want to create a file which contains the names and phone

numbers of your co-workers You can use the tee command as follows:

Each line is entered by pressing the Return key The line of text is duplicated

in the shell, showing you it has been written to the output file Use Control-d

to end the input of content

The -a option for the tee command allows you to append to a file’s current

contents without overwriting the current contents

Creating Directories

Trang 12

Use the mkdir command to create directories Directories can be created using either an absolute or a relative pathname You can specify more than one directory name on the same line to create more than one new directory.

Reports/ dir1/ dir4/ file3 fruit2 tutor.vi*

dante dir2/ file1 file4 games/

dante_1 dir3/ file2 fruit practice/

You must have the appropriate permissions to create a directory

(Permissions are covered later in the course.) If you do not have the correct permissions on a file or directory, you will receive an error message similar to this one:

Trang 13

dir1 mailbox project research

practice/dir1:

adminpractice/dir1/admin:

Copying Files

Use the cp command to copy files

Command Format

cp [-i] source_file destination_file

cp [-i] source_file(s) destination_directory

Copying a File to Another Within a Directory

The following example demonstrates how to copy one file to a new file

in the same directory:

Reports dir1 dir4 file2 fruit practice

dante dir2 feathers file3 fruit2 tutor.vi

dante_1 dir3 file1 file4 games

$ cp feathers feathers_6

Copying Multiple Files

The following example demonstrates how to copy multiple files into a directory other than the current directory:

$ pwd

/home/user2

$ ls dir1

coffees fruit trees

$ cp feathers feathers_6 dir1

$ ls dir1

coffees feathers feathers_6 fruit trees

Trang 14

Preventing Overwriting an Existing File When

Command Format

cp -r[i] source_directory(s) destination_directory

Without the -r option, the files and subdirectories contained within a directory are not copied When used with the -i option, cp prompts for verification before overwriting an existing file

Copying the Contents of a Directory to a New

Directory

The following example demonstrates how to copy a directory and all of its files to a new directory in the current directory and the error that results from not using the -r option:

Trang 15

The following example demonstrates how to copy a directory to

another directory that is not in the current directory:

Moving and Renaming Files

Use the mv command to move or rename files and directories

Command Format

mv [-i] source target_file

mv [-i] source target_directory

You can use the mv -i option, which prompts for confirmationwhenever the move would overwrite an existing target A y answermeans that the move should proceed Any other answer prevents mvfrom overwriting the target

Renaming Files in the Current Directory

The following example demonstrates renaming a file in the current directory:

Moving Files to Another Directory

The following example demonstrates moving a file into another

directory:

Trang 16

Reports dir2 file1 fruit2 tutor.vi

brands dir3 file2 games

dante dir4 file3 maildir

dante_1 feathers file4 practice

dir1 feathers_6 fruit ski.places

$ mv maildir monthly

$ ls

Reports dir2 file1 fruit2 tutor.vi

brands dir3 file2 games

dante dir4 file3 monthly

dante_1 feathers file4 practice

dir1 feathers_6 fruit ski.places

$

Moving a Directory and Its Contents

The following example demonstrates using the mv command to move adirectory and its contents into another directory:

Trang 17

Renaming in a Non-Current Directory

The following example demonstrates how to use the mv command to rename a file in a non-current directory:

dir1 mailbox projection

Use the rm -i command as a precaution The -i option prompts you for confirmation before removing any file

Trang 18

Reports dir2 file1 fruit2 tutor.vi

brands dir3 file2 games

dante dir4 file3 monthly

dante_1 feathers file4 newdir

dir1 feathers_6 fruit ski.places

Use rm -ir to interactively remove directories

$ mkdir -p ~/practice/dir1

$ ls ~/practice

dir1

$ rm -ir

rm: examine files in directory practice (yes/no)? y

rm: examine files in directory practice/dir1

(yes/no)? y

rm: remove practice/dir1: (yes/no)? y

rm: remove practice: (yes/no)? y

Redirection

Trang 19

All central processing unit (CPU) operations have input and/or output (I/

O) The keyboard, for example, provides standard input while the monitor displays standard output and standard error.

These are defined as follows:

Input – Sending data to a command

Output – Receiving data from a command

Error - Errors generated by a command

Standard input Command Standard output

The Linux computing environment enables command I/O to becontrolled using redirection This is useful when attempting to save theoutput of a command to a file for later viewing, sending the contents of

a file as an email message, or redirecting error messages to a file foruse when debugging a program

The format for the redirection of standard input, standard output, andstandard error is:

command > file or command >> file

command < file

command 2> file

Angle Brackets ( >, <, 2> )

Use the right-angle bracket (>) to redirect the output of a command to

a file rather than to the screen

Use the left-angle bracket (<) to redirect the input to a command from

a file rather than from the keyboard

Ngày đăng: 02/10/2013, 09:20

TỪ KHÓA LIÊN QUAN

w