Tải bản đầy đủ (.pdf) (51 trang)

Tài Liệu - Võ Tấn Dũng (votandung) Unit3 Linux ITC

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.93 MB, 51 trang )

Information Technology College of HoChiMinh city
Faculty of Information Technology

Course: Fundamentals of Linux OS

Unit 3

File and Directory
Commands
Lecturer: Võ Tấn Dũng

/>

Objectives
• 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 by the
touch command. Use the tee command to create text within a file.
• Create and remove directories using mkdir and rmdir.
• Manage files and directories using the mv,cp, and rm commands.
• Pass output from one command to another using a pipe

VÕ TẤN DŨNG


Displaying Files
Using these commands:
cat, more, head, tail, wc



VÕ TẤN DŨNG


Using the cat command
• The cat (concatenate) command displays the contents of a
text file on the screen. It is often used to display short text
files; because cat flashes through the entire file rapidly
without pausing, it is unsuitable for files longer than one
screen in length. The cat command is more often used to
join two or more files into one large file.

Command Format
cat filename(s)
Example:

$ cat dante
The Life and Times of Dante
by Dante Pocai
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
.
.
.
VÕ TẤN DŨNG


Using the cat command (cont.)
• Using the cat Command to join two files into one.


$ cat filename1 filename2 > file3
This example joins filename1 and filename2 files into
filename3 file.

VÕ TẤN DŨNG


Using the more command
• Use the more command to display the contents of a text file to the
screen one screen at a time. If the information in a file is longer than
one screen, the following message appears at the bottom of the
screen: --More--(n%) . Where n is the percentage of the file already
displayed.

Command Format
more filename(s)
• At the --More-- prompt, you can use the following keys to control the
scrolling capabilities:

VÕ TẤN DŨNG


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.

Command Format
head [ -n ] filename(s)

In this example, the head -6 command displays the
first six lines of the /usr/dict/words file.
$ head -6 /usr/dict/words
10th
1st
2nd
3rd
4th
5th
$
VÕ TẤN DŨNG


Using the tail command
Use the tail command to display the last n lines of a file.
The last 10 lines are displayed by default if the -n option is
omitted.
Command Format
tail [ -n ] filename(s)
tail [ +n ] filename(s)
In this example, the tail -5 command displays the last five
lines of the /usr/dict/words file.
$ tail -5 /usr/dict/words
In this example, the tail +23 command displays from line
23th through the end of the /usr/dict/words file.
$ tail +23 /usr/dict/words
VÕ TẤN DŨNG


Using the wc command

Use the wc command to display a line, word, or character count
of a file.

Command Format
wc [ options] filename(s)

$ wc dante
33 223 1320 dante
$ wc -l dante
33 dante
VÕ TẤN DŨNG


Comparing Files
Using these commands:
cmp, diff

VÕ TẤN DŨNG


Locating text differences
with the cmp command
- Comparing files to determine differences between them
can be done using the cmp command.
- This command produces no output if the files are exactly
the same
- Or it provides the byte and line numbers at which the first
difference occurred if there are discrepancies between
the files.


Command Format
cmp file1 file2

VÕ TẤN DŨNG


Example of cmp command
• Using the cmp command to compare files that appear to be the same.
You would:

1. Do a long listing and compare the file sizes.

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

• This output identifies the first occurrence of a difference between the
two files. The difference occurred at the 1,638th character position
and was found on line 42.

VÕ TẤN DŨNG


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

Command Format
diff [option] file1 file2


VÕ TẤN DŨNG


Example of diff to compare files
$ diff -c fruit fruit2
*** fruit Fri May 8 11:32:49 1998
--- fruit2 Fri May 8 14:55:21 1998
************
***2, 8****
orange
apple
banana
-pear
-mango
tomato
pomegranate
---2, 8---orange
apple
banana
tomato
+guava
+mango
pomegranate

$ cat fruit
lemon
orange
apple
banana
pear

mango
tomato
pomegranate

$ cat fruit2
lemon
orange
apple
banana
tomato
guava
mango
pomegranate

VÕ TẤN DŨNG


Creating files and directories
Using these commands:
touch, tee, mkdir

VÕ TẤN DŨNG


Creating empty files
using touch command
• You can create new, empty files using the touch command.

Command Format
touch filename(s)

Example:

Creating Empty Files
$ cd ~/practice
$ touch mailbox project research
$ ls
mailbox project research

VÕ TẤN DŨNG


Creating and appending using
the tee command
• When you create a new file by the tee command, the
system searches for the file name you typed in the
command line. If the file exists, the system overwrites its
contents with the new information. If the file does not exist,
it is created and the content is written into it.
• After input the content, use Control-d to end the input of
content.

Example:

$ tee phone_list.txt
Bill O. - 808-555-9876
Bill O. - 808-555-9876
Fred P. - 808-555-6543
Fred P. - 808-555-6543
^D


The -a option for the tee command allows you to append
to a file’s current contents without overwriting the current
contents.
VÕ TẤN DŨNG


Use the mkdir command
to create directory
Example:

Command Format
mkdir [-p] directory_name(s)

$ pwd
/home/user2
$ mkdir Reports
$ ls -dl Reports
drwxr-xr-x 2 user2 staff 512 Jan 28 16:24 Reports
$ mkdir Reports/Weekly
$ cd Reports/Weekly
$ ls Reports
$ mkdir dir1 dir2 dir3
Weekly
$ ls -F

dir1/ dir2/ dir3/
$ mkdir ~/games
$ cd
$ ls -F
Reports/ dir1/ dir4/ file3 fruit2 tutor.vi*

dante dir2/ file1 file4 games/
dante_1 dir3/ file2 fruit practice/
VÕ TẤN DŨNG


Use the mkdir command
to create directory (cont.)
• You must have the appropriate permissions to create a
directory. If you do not have the correct permissions on a
file or directory, you will receive an error message similar to
this one:
$ mkdir /home/Olympic
mkdir: Failed to make directory "/home/Olympic";
Permission denied

• You can create multiple levels of directories at one time by
using the -p option. $ cd
$ mkdir -p practice/dir1/admin
$ ls -R practice
practice:
dir1 mailbox project research
practice/dir1:
admin
practice/dir1/admin:
VÕ TẤN DŨNG


Copy files and directories
Using these commands:
cp, cp -r


VÕ TẤN DŨNG


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:

Example:

$ cd
$ pwd
/home/user2
$ cp file3 feathers
$ ls
Reports dir1 dir4 file2 fruit practice
dante dir2 feathers file3 fruit2 tutor.vi
dante_1 dir3 file1 file4 games
$ cp feathers feathers_6
VÕ TẤN DŨNG


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


Example:
$ pwd
/home/user2
$ ls dir1
coffees fruit trees
$ cp feathers feathers_6 dir1
$ ls dir1
coffees feathers feathers_6 fruit
trees

VÕ TẤN DŨNG


Copying directories
• Use the cp -r (recursive) command to copy a directory and
its contents to another directory. If the directory does not
exist, it is created by the cp command.

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.

VÕ TẤN DŨNG


Preventing overwriting
an existing file when copying
• Use cp -i as a security measure. The -i option prompts you
if you are about to overwrite an existing file or directories.

• When used with the -i option, cp prompts for verification
before overwriting an existing file or directories.

Example:
$ cp -i feathers file3
cp: overwrite file3 (yes/no)? n
$

VÕ TẤN DŨNG


Copying the content 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:

Example:

$ cd
$ pwd
/home/user2
$ ls dir3
planets
$ cp dir3 ski.places
cp: dir3: is a directory
$ cp -r dir3 ski.places
$ ls ski.places
planets

VÕ TẤN DŨNG


×