Shell 中的文件系统操作

ShellShellBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习如何在 shell 中执行各种文件测试。文件测试是检查文件系统中文件和目录属性的重要工具。通过本实验,你将熟悉常见的文件测试命令及其用法,这是在 Linux 环境中处理文件的基本技能。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) shell(("Shell")) -.-> shell/VariableHandlingGroup(["Variable Handling"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) shell(("Shell")) -.-> shell/ControlFlowGroup(["Control Flow"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") shell/VariableHandlingGroup -.-> shell/variables_decl("Variable Declaration") shell/VariableHandlingGroup -.-> shell/variables_usage("Variable Usage") linux/BasicFileOperationsGroup -.-> linux/touch("File Creating/Updating") linux/BasicFileOperationsGroup -.-> linux/chmod("Permission Modifying") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("Directory Creating") shell/ControlFlowGroup -.-> shell/if_else("If-Else Statements") shell/ControlFlowGroup -.-> shell/cond_expr("Conditional Expressions") subgraph Lab Skills linux/echo -.-> lab-388821{{"Shell 中的文件系统操作"}} shell/variables_decl -.-> lab-388821{{"Shell 中的文件系统操作"}} shell/variables_usage -.-> lab-388821{{"Shell 中的文件系统操作"}} linux/touch -.-> lab-388821{{"Shell 中的文件系统操作"}} linux/chmod -.-> lab-388821{{"Shell 中的文件系统操作"}} linux/mkdir -.-> lab-388821{{"Shell 中的文件系统操作"}} shell/if_else -.-> lab-388821{{"Shell 中的文件系统操作"}} shell/cond_expr -.-> lab-388821{{"Shell 中的文件系统操作"}} end

创建测试文件

在开始文件操作之前,了解我们的工作环境非常重要。在 Linux 中,你总是在特定的目录中工作,因此了解你在文件系统中的位置至关重要。

  1. 在 WebIDE 中打开一个终端。这是你输入命令的地方。

  2. 创建一个名为 test_file.txt 的新文件:

    touch test_file.txt

    touch 命令用于创建一个空文件。如果文件已经存在,它会更新文件的时间戳而不改变其内容。

  3. 向文件中添加一些内容:

    echo "This is a test file for our lab." > test_file.txt

    该命令使用 echo 输出文本,并使用 > 将输出重定向到文件中。请注意,> 会覆盖文件中已有的内容。

  4. 验证文件内容:

    cat test_file.txt

    cat 是 "concatenate" 的缩写,但它通常用于显示文件内容。你应该会看到消息 "This is a test file for our lab."。

如果你犯了错误或想重新开始,可以使用 rm test_file.txt 删除文件,然后从步骤 1 重新开始。

测试文件是否存在

现在我们已经创建了一个文件,接下来学习如何检查文件是否存在。这是 shell 脚本中的常见任务,尤其是在需要对文件执行操作时。

  1. 创建一个名为 file_exists.sh 的新脚本文件:

    touch file_exists.sh
  2. 向文件中添加以下内容:

    #!/bin/bash
    
    filename="test_file.txt"
    if [ -e "$filename" ]; then
      echo "$filename exists"
    else
      echo "$filename does not exist"
    fi

    让我们分解一下这段代码:

    • #!/bin/bash 被称为 shebang。它告诉系统这是一个 bash 脚本。
    • 我们将变量 filename 设置为 "test_file.txt"。
    • if 语句检查文件是否存在。-e 是一个测试条件,如果文件存在则返回 true。
    • 我们使用 echo 根据文件是否存在打印消息。
  3. 保存文件并退出编辑器。

  4. 使脚本可执行:

    chmod +x file_exists.sh
  5. 运行脚本:

    ./file_exists.sh

    你应该会看到输出:"test_file.txt exists"。

  6. 现在,让我们测试一个不存在的文件。首先,重命名我们的测试文件:

    mv test_file.txt non_existent.txt

    该命令将 test_file.txt 重命名为 non_existent.txt

  7. 修改脚本以检查原始文件名 "test_file.txt":

    nano file_exists.sh

    如果 filename 变量尚未设置为 "test_file.txt",请将其更改为该值。

  8. 再次运行脚本:

    ./file_exists.sh

    你应该会看到输出:"test_file.txt does not exist"。

这个脚本演示了如何检查文件是否存在,当你的脚本需要处理可能不存在或存在的文件时,这一点至关重要。

测试目录是否存在

与测试文件是否存在类似,我们也可以检查目录是否存在。当你的脚本需要处理可能不存在或存在的目录时,这非常有用。

  1. 创建一个名为 dir_exists.sh 的新脚本文件:

    touch dir_exists.sh
  2. 向文件中添加以下内容:

    #!/bin/bash
    
    dirname="test_directory"
    if [ -d "$dirname" ]; then
      echo "$dirname exists"
    else
      echo "$dirname does not exist"
    fi

    这个脚本与我们的文件存在性脚本非常相似,但它使用 -d 而不是 -e-d 测试专门用于检查目录是否存在。

  3. 保存文件并退出编辑器。

  4. 使脚本可执行:

    chmod +x dir_exists.sh
  5. 运行脚本:

    ./dir_exists.sh

    你应该会看到输出:"test_directory does not exist"。

  6. 现在,让我们创建目录并再次运行脚本:

    mkdir test_directory
    ./dir_exists.sh

    你现在应该会看到输出:"test_directory exists"。

    mkdir 是创建新目录的命令。

这个脚本演示了如何检查目录是否存在。这对于需要创建、修改或删除目录的脚本特别有用。

测试文件权限

在 Linux 中,每个文件和目录都有与之关联的权限,这些权限决定了谁可以读取、写入或执行它们。在这一步中,我们将学习如何检查文件权限,特别是文件是否可读。

  1. 首先,将我们的文件重命名为其原始名称:

    mv non_existent.txt test_file.txt
  2. 创建一个名为 file_readable.sh 的新脚本文件:

    touch file_readable.sh
  3. 向文件中添加以下内容:

    #!/bin/bash
    
    filename="test_file.txt"
    if [ -r "$filename" ]; then
      echo "You have read permission for $filename"
    else
      echo "You do not have read permission for $filename"
    fi

    该脚本使用 -r 测试,用于检查当前用户是否具有读取文件的权限。

  4. 保存文件并退出编辑器。

  5. 使脚本可执行:

    chmod +x file_readable.sh
  6. 运行脚本:

    ./file_readable.sh

    你应该会看到输出:"You have read permission for test_file.txt"。

  7. 现在,让我们移除读取权限并再次运行脚本:

    chmod -r test_file.txt
    ./file_readable.sh

    你现在应该会看到输出:"You do not have read permission for test_file.txt"。

    chmod -r 会移除文件的读取权限。

  8. 恢复读取权限:

    chmod +r test_file.txt

    恢复权限非常重要,以免意外导致文件不可读。

这个脚本演示了如何检查文件权限。理解和管理文件权限对于系统安全和脚本的正常运行至关重要。

总结

在本实验中,你学习了如何在 shell 中执行基本的文件系统操作。你创建了脚本来测试文件和目录是否存在,以及检查文件权限。这些技能是在 Linux 环境中处理文件和目录的基础,也是更复杂的 shell 脚本任务的基石。

你练习了以下内容:

  1. 理解你的工作环境
  2. 创建和操作文件
  3. 编写和执行 shell 脚本
  4. 测试文件和目录是否存在
  5. 检查文件权限

这些技能在你继续使用 Linux 系统和开发更高级的 shell 脚本时将非常有价值。请记住,在对文件或目录执行操作之前,始终检查它们是否存在,以避免脚本中的错误。此外,在处理敏感数据或系统文件时,请注意文件权限。

随着你的进步,你可能会希望探索更高级的文件测试,学习其他 shell 脚本结构(如循环和函数),并通过结合这些概念来创建更复杂和强大的脚本。