Skip to content

Unix / Linux - 目录

在类 Unix 系统中,文件被组织在目录(在图形环境中通常称为文件夹)中。目录本身可以包含其他目录,形成一个称为文件系统树 (filesystem tree) 的分层结构。

理解如何导航和管理目录是有效使用命令行 (command line) 的基础。

文件系统始于根目录 (root directory),由一个单独的正斜杠 (/) 表示。所有其他文件和目录都位于此根目录下。

当您登录时,通常会进入您的个人主目录 (home directory)。您在此处存储您的文件并创建您自己的子目录结构。这是您在文件系统中的个人空间。

波浪号字符 (~) 是您的主目录路径的快捷方式。

从任何位置导航到您的主目录:

$ cd ~ # or simply
$ cd

要导航到其他用户的主目录(如果您有权限):

$ cd ~username

文件或目录的位置由其路径名 (pathname) 指定。

  • 绝对路径名 (Absolute Pathname):从根目录 (/) 开始指定位置。无论您当前位置如何,它都是明确的。始终以 / 开头。
  • 相对路径名 (Relative Pathname):相对于您当前工作目录 (current working directory) 指定位置。不以 / 开头。

绝对路径名示例:

/home/alice/documents/report.txt
/etc/ssh/sshd_config
/usr/bin/python3

相对路径名示例(假设您当前目录是 /home/alice):

documents/report.txt # Refers to /home/alice/documents/report.txt
../bob/pictures # Refers to /home/bob/pictures (.. goes up one level)

pwd (print working directory) 命令显示您当前目录的绝对路径。

$ pwd
/home/alice/projects/website
$

ls 命令列出目录中的文件和子目录。默认情况下,它列出当前目录的内容。

$ ls
index.html styles.css images scripts

要列出特定目录的内容:

$ ls /etc
acpi cron.d hosts lvm profile
adobe cron.hourly hosts.allow machine-id protocols
alternatives cron.monthly hosts.deny magic pulse
...

ls 的常用有用选项包括:

  • -l:长格式 (显示权限、所有者、大小、修改日期)。
  • -a:所有文件 (包括以 . 开头的隐藏文件)。
  • -h:人类可读的大小 (例如,4.0K, 1.2M)。常与 -l 一起使用。
  • -t:按修改时间排序,最新的排在前面。
  • -r:排序时反转顺序。
$ ls -alh
total 20K
drwxr-xr-x 4 alice alice 4.0K Jul 25 14:30 .
drwxr-xr-x 5 alice alice 4.0K Jul 25 14:25 ..
-rw-r--r-- 1 alice alice 1.5K Jul 25 14:28 index.html
drwxr-xr-x 2 alice alice 4.0K Jul 25 14:26 images
drwxr-xr-x 2 alice alice 4.0K Jul 25 14:27 scripts
-rw-r--r-- 1 alice alice 850 Jul 25 14:30 styles.css

mkdir (make directory) 命令创建新目录。

$ mkdir backups
$ mkdir documents reports presentations

第一个命令在当前位置创建一个名为 backups 的目录。第二个命令创建三个独立的目录。

如果您尝试创建一个父目录不存在的目录,mkdir 将会失败。

$ mkdir /tmp/myproject/data
mkdir: cannot create directory ‘/tmp/myproject/data’: No such file or directory

使用 -p (parents) 选项可以自动创建任何必需的父目录:

$ mkdir -p /tmp/myproject/data
$ # Success! Both myproject and data are created.

rmdir (remove directory) 命令删除空目录。

$ rmdir old_backups
$ rmdir temp_dir project_archive

如果目录包含任何文件或子目录,rmdir 将会失败。

$ rmdir documents
rmdir: failed to remove 'documents': Directory not empty

要删除一个目录及其所有内容(文件和子目录),请使用 rm 命令并带有 -r (recursive) 选项。**使用 rm -r 时务必极其小心!**操作无法撤消。

$ rm -r documents # Deletes the documents directory and everything inside it!

添加 -i (interactive) 标志 (rm -ri) 可以提供一些安全性,会在每次删除前提示您确认。

cd (change directory) 命令在文件系统树中导航。您提供要进入目录的路径(绝对或相对)。

# Go to the backups directory (relative path)
$ cd backups
# Go to the system's configuration directory (absolute path)
$ cd /etc
# Go back to the previous directory
$ cd -
# Go up one level
$ cd ..
# Go up two levels
$ cd ../..

提示:大多数现代 Shell 都支持 Tab 补全 (Tab Completion)。输入目录名称的开头,然后按 Tab 键;Shell 会尝试补全名称或显示可能的匹配项。这节省了输入时间并避免了错误。

mv (move) 命令既用于将目录(和文件)移动到不同的位置,也用于重命名它们。

重命名语法:

$ mv old_directory_name new_directory_name

示例:将 temp_files 重命名为 archive。

$ mv temp_files archive

移动语法:

$ mv directory_to_move destination_directory/

示例:将 archive 目录移动到 backups 目录中。

$ mv archive backups/

每个目录都包含两个特殊的条目:

  • . (点):代表当前目录本身。
  • .. (点点):代表父目录 (上一级目录)。

这些对于相对路径导航至关重要,特别是 .. 用于向上移动层级。

# List contents of the current directory using .
$ ls .
# List contents of the parent directory using ..
$ ls ..
# Copy a file from the parent directory to the current directory
$ cp ../important_file.txt .