Unix 简明教程

Linux - File Management

What is File Management in Linux?

当我们使用 Linux 时,我们需要许多文本和二进制文件,例如所有 Linux 程序都以二进制文件形式出现,而它们的源代码以文本文件形式出现。作为操作系统用户,我们还会创建许多文件来管理我们的日常活动。用户生成的文件包括 word 文件、excel 文件、power point 演示文稿和许多其他文本文件。

When we work with Linux, we need many text and binary files, for example all the Linux programs come in binary files where as their source code come in text files. As a user of the Operating System, we also create many files to manage our day to day activities. User generated files include words files, excel files, power point presentations and many other text files.

在本章中,我们将详细讨论 Linux/Unix 中的文件管理。Linux 中的所有数据都组织到文件中,所有这些文件都组织到不同的目录中。这些目录组织成称为文件系统的树形结构。

In this chapter, we will discuss in detail about file management in Linux/Unix. All the data in Linux is organized into files and all these files are organized into different directories. These directories are organized into a tree-like structure called the filesystem.

Types of Files in Linux

因此,Linux 中的一切都是 file 。因此,当您使用 Linux 时,无论哪种方式,您都会将大部分时间花在文件处理上。本教程将帮助您了解如何创建和删除文件、复制和重命名文件、创建到它们的链接等。

As such everything in Linux is a file. So when you work with Linux, one way or another, you spend most of your time working with files. This tutorial will help you understand how to create and remove files, copy and rename them, create links to them, etc.

在 Linux 中,有三种基本类型的文件 −

In Linux, there are three basic types of files −

  1. Ordinary Files − An ordinary file is a file on the system that contains data, text, or program instructions. In this tutorial, you look at working with ordinary files.

  2. Directories − Directories store both special and ordinary files. For users familiar with Windows or Mac OS, Unix directories are equivalent to folders.

  3. Special Files − Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.

File Management Commands in Linux

让我们学习最重要的 Linux 命令,列出可用的文件、创建和删除文件、复制和重命名文件、创建到文件的链接等。

Let’s study the most important Linux commands to list available files, create and remove files, copy and rename files, create links to files, etc.

Listing Files

要列出 Linux 系统中当前目录中存储的所有文件和目录,请使用以下命令 −

To list all the files and directories stored in the current directory on a Linux system, use the following command −

$ ls

以下是要在命令中列出示例输出 −

Here is the sample output of the above command −

$ls

bin        hosts  lib     res.03
ch07       hw1    pub     test_results
ch07.bak   hw2    res.01  users
docs       hw3    res.02  work

命令 ls 支持 -l 选项,它将帮助您获取有关已列出文件详细信息 −

The command ls supports the -l option which would help you to get more information about the listed files −

$ls -l
total 1962188

drwxrwxr-x  2 amrood amrood      4096 Dec 25 09:59 uml
-rw-rw-r--  1 amrood amrood      5341 Dec 25 08:38 uml.jpg
drwxr-xr-x  2 amrood amrood      4096 Feb 15  2006 univ
drwxr-xr-x  2 root   root        4096 Dec  9  2007 urlspedia
-rw-r--r--  1 root   root      276480 Dec  9  2007 urlspedia.tar
drwxr-xr-x  8 root   root        4096 Nov 25  2007 usr
drwxr-xr-x  2    200    300      4096 Nov 25  2007 webthumb-1.01
-rwxr-xr-x  1 root   root        3192 Nov 25  2007 webthumb.php
-rw-rw-r--  1 amrood amrood     20480 Nov 25  2007 webthumb.tar
-rw-rw-r--  1 amrood amrood      5654 Aug  9  2007 yourfile.mid
-rw-rw-r--  1 amrood amrood    166255 Aug  9  2007 yourfile.swf
drwxr-xr-x 11 amrood amrood      4096 May 29  2007 zlib-1.2.3
$

以下是所有列出列的信息 −

Here is the information about all the listed columns −

  1. First Column − Represents the file type and the permission given on the file. Below is the description of all type of files.

  2. Second Column − Represents the number of memory blocks taken by the file or directory.

  3. Third Column − Represents the owner of the file. This is the Linux user who created this file.

  4. Fourth Column − Represents the group of the owner. Every Linux user will have an associated group.

  5. Fifth Column − Represents the file size in bytes.

  6. Sixth Column − Represents the date and the time when this file was created or modified for the last time.

  7. Seventh Column − Represents the file or the directory name.

ls -l 列表示例中,文件行的开头是 d-l 。这些字符表示列出的文件类型。

In the ls -l listing example, every file line begins with a d, -, or l. These characters indicate the type of the file that’s listed.

Prefix

Description

-

Regular file, such as an ASCII text file, binary executable, or hard link.

b

Block special file. Block input/output device file such as a physical hard drive.

c

Character special file. Raw input/output device file such as a physical hard drive.

d

*Directory * which contains a listing of other files and directories.

l

Symbolic link file. Links on any regular file.

p

Named pipe. A mechanism for interprocess communications.

s

Socket which is used for interprocess communication.

Metacharacters in Linux

Linux 元字符在 Unix 中有特殊含义。例如, * and ? are metacharacters. We use * 用于匹配 0 个或更多个字符,问号 ( ? ) 匹配单个字符。

Linux Metacharacters have a special meaning in Unix. For example, and ? are metacharacters. We use to match 0 or more characters, a question mark (?) matches with a single character.

例如 −

For Example −

$ls ch*.doc

显示开头字母为 ch 且结尾字母为 .doc 的所有文件 −

Displays all the files, the names of which start with ch and end with .doc

ch01-1.doc   ch010.doc  ch02.doc    ch03-2.doc
ch04-1.doc   ch040.doc  ch05.doc    ch06-2.doc
ch01-2.doc ch02-1.doc c

此处, * 用作匹配任何字符的元字符。如果你只想显示所有以 .doc 结尾的文件,则可以使用以下命令 −

Here, * works as meta character which matches with any character. If you want to display all the files ending with just .doc, then you can use the following command −

$ls *.doc

Hidden Files in Linux

Linux 和 Unix 有一些对用户不可见隐藏文件。这些文件的名称以点或句点字符 (.) 开头。Linux 程序(包括 shell)使用大多数这些文件来存储系统配置信息。

Linux and Unix have some hidden files which are invisible from the users. These files name starts with a dot or the period character (.). Linux programs (including the shell) use most of these files to store system configuration information.

隐藏文件的一些常见示例包括文件 −

Some common examples of the hidden files include the files −

File

Description

.profile

The Bourne shell ( sh) initialization script

.kshrc

The Korn shell ( ksh) initialization script

.cshrc

The C shell ( csh) initialization script

.rhosts

The remote shell configuration file

为了列出这些隐藏(或不可见的)文件,我们必须使用@+s1+命令指定@+s0+选项-

To list these hidden (or invisible files), we must specify the -a option with ls command −

$ ls -a

.         .profile       docs     lib     test_results
..        .rhosts        hosts    pub     users
.emacs    bin            hw1      res.01  work
.exrc     ch07           hw2      res.02
.kshrc    ch07.bak       hw3      res.03
$
  1. Single dot (.) − This represents the current directory.

  2. Double dot (..) − This represents the parent directory.

Creating Files in Linux

Linux系统中已预装了许多文件编辑器。我最喜欢的文件编辑器是@s4+或简称@+s5,我用它在Ubuntu Linux系统上创建和更新不同的文本文件。

There are manu file editors which come pre-installed on Linux Systems. My favorite is vim or in short vi editor which I use to create and update different text files on my Ubuntu Linux System.

让我们使用@+s6+编辑器在任何Linux系统上创建普通文件。您只需要给如下命令:

So let’s use the vi editor to create ordinary files on any Linux system. You simply need to give the following command −

$ vi filename

以上命令将打开带有给定文件名的文件。现在,按@+s7+键进入编辑模式。一旦进入编辑模式,您可以开始在文件中编写您的内容。

The above command will open a file with the given filename. Now, press the key i to come into the edit mode. Once you are in the edit mode, you can start writing your content in the file.

让我们在我们的文本文件中写下以下内容:

Let’s write the following content in our text file −

This is a text file in Linux....I created it using vi text editor.....
I'm going to save this content in this file.

一旦您在文件中编写完内容,执行以下步骤:

Once you are done with writing your content in the file, follow these steps −

  1. Press the key esc to come out of the edit mode.

  2. Press two keys Shift + ZZ together to come out of the file completely.

您现在将在当前目录中使用@+s10+创建文件。

You will now have a file created with filename in the current directory.

$ vi filename
$

Editing Files in Linux

您可以使用@+s11+编辑器编辑现有文件。我们将简要讨论如何打开现有文件:

You can edit an existing file using the vi editor. We will discuss in short how to open an existing file −

$ vi filename

一旦打开文件,您可以按@+s12+键进入编辑模式,然后您可以继续编辑文件。如果您想在文件内四处移动,那么首先您需要按@+s13+键退出编辑模式。此后,您可以使用以下键在文件中移动:

Once the file is opened, you can come in the edit mode by pressing the key i and then you can proceed by editing the file. If you want to move here and there inside a file, then first you need to come out of the edit mode by pressing the key Esc. After this, you can use the following keys to move inside a file −

  1. l key to move to the right side.

  2. h key to move to the left side.

  3. k key to move upside in the file.

  4. j key to move downside in the file.

使用上述按键,你可以将光标置于任意位置以进行编辑。定位后,你可以使用 i 键进入编辑模式。完成后,在文件内按 Esc ,最后同时按 Shift + ZZ 两个键以完全退出该文件。

So using the above keys, you can position your cursor wherever you want to edit. Once you are positioned, then you can use the i key to come in the edit mode. Once you are done with the editing in your file, press Esc and finally two keys Shift + ZZ together to come out of the file completely.

Display Content of a File

你可以使用 cat 命令查看文件的内容。以下是一个查看上述创建的文件内容的简单示例:

You can use the cat command to see the content of a file. Following is a simple example to see the content of the above created file −

$ cat filename
This is a text file in Linux....I created it using vi text editor.....
I'm going to save this content in this file.

$

你可以使用 -b 选项和 cat 命令一起显示行号,如下所示:

You can display the line numbers by using the -b option along with the cat command as follows −

$ cat -b filename
1    This is a text file in Linux....I created it using vi text editor.....
2    I'm going to save this content in this file.

$

Counting Words in a File

你可以使用 wc 命令获取文件包含的行数、单词数和字符数。以下是一个查看上述创建的文件信息的简单示例:

You can use the wc command to get a count of the total number of lines, words, and characters contained in a file. Following is a simple example to see the information about the file created above −

$ wc filename
2  22 116 filename

$

以下是四个列的详细信息:

Here is the detail of all the four columns −

  1. First Column − Represents the total number of lines in the file.

  2. Second Column − Represents the total number of words in the file.

  3. Third Column − Represents the total number of bytes in the file. This is the actual size of the file.

  4. Fourth Column − Represents the file name.

你可以给出多个文件并一次获取这些文件的信息。以下是简单的语法:

You can give multiple files and get information about those files at a time. Following is simple syntax −

$ wc filename1 filename2 filename3

Copying Files in Linux

要复制现有文件,请使用 cp Linux 命令。该命令的基本语法是:

To make a copy of an existing file use the cp Linux command. The basic syntax of the command is −

$ cp source_file destination_file

以下是创建 filename 现有文件副本的示例。

Following is the example to create a copy of the existing file filename.

$ cp filename copyfile

$

你现在会在当前目录中找到另一个文件 copyfile 。该文件将与原始文件 filename 完全相同。

You will now find one more file copyfile in your current directory. This file will exactly be the same as the original file filename.

Renaming Files in Linux

要更改现有文件的名称,请使用 mv Linux 命令。以下是基本语法:

To change the name of an existing file, use the mv Linux command. Following is the basic syntax −

$ mv old_file new_file

以下程序将现有文件 filename 重命名为 newfile

The following program will rename the existing file filename to newfile.

$ mv filename newfile

$

mv 命令将现有文件完全移动到新文件中。在这种情况下,你只会在当前目录中找到 newfile

The mv command will move the existing file completely into the new file. In this case, you will find only newfile in your current directory.

Deleting Files in Linux

要在 Linux 文件系统中删除现有文件,请使用 rm 命令。以下是基本语法:

To delete an existing file from Linux file system, use the rm command. Following is the basic syntax −

$ rm filename

以下示例显示了如何完全删除现有文件 filename

Following is the example which shows how to completely remove the existing file filename.

$ rm filename

$

你可以使用以下命令同时删除多个文件:

You can remove multiple files at a time with the command given below −

$ rm filename1 filename2 filename3

$

Linux 提供了从另一个位置访问文件的链接机制。您可以说这些链接是现有文件的别名。这些链接可以是符号链接或硬链接。

Linux provides link mechanism to access a file from another location. You can say these links are alternate names for the existing files. These links could be symbolic links or hard links.

下面是创建文件名符号链接的命令:

Following is the command to create a symbolic link on filename:

$ ln -s filename symlink

$

下面是创建现有文件硬链接的命令:

Following is the command to create hard link on an existing file:

$ ln filename hardlink

$

现在可以检查创建的链接:

Now you can check your created links:

$ ls -l
total 8
-rw-r--r-- 2 root root 132 May  1 07:18 filename
-rw-r--r-- 2 root root 132 May  1 07:18 hardlink
lrwxrwxrwx 1 root root   8 May  1 07:17 symlink -> filename

$

创建符号或硬链接后,可以使用这些链接访问原始文件。可以使用链接编辑这些文件,但如果删除链接文件,原始文件将保持未删除且不变。

After creating symbolic or hard link, you can access original file using these links. You can edit these files using links but if you delete a link file then your original file will remain undeleted and unchanged.