Unix / Linux - 目录
Unix / Linux - 目录管理
Section titled “Unix / Linux - 目录管理”在类 Unix 系统中,文件被组织在目录(在图形环境中通常称为文件夹)中。目录本身可以包含其他目录,形成一个称为文件系统树 (filesystem tree) 的分层结构。
理解如何导航和管理目录是有效使用命令行 (command line) 的基础。
文件系统始于根目录 (root directory),由一个单独的正斜杠 (/) 表示。所有其他文件和目录都位于此根目录下。
当您登录时,通常会进入您的个人主目录 (home directory)。您在此处存储您的文件并创建您自己的子目录结构。这是您在文件系统中的个人空间。
波浪号字符 (~) 是您的主目录路径的快捷方式。
从任何位置导航到您的主目录:
$ cd ~ # or simply$ cd要导航到其他用户的主目录(如果您有权限):
$ cd ~username绝对路径名与相对路径名
Section titled “绝对路径名与相对路径名”文件或目录的位置由其路径名 (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
Section titled “查找当前目录:pwd”pwd (print working directory) 命令显示您当前目录的绝对路径。
$ pwd/home/alice/projects/website$列出目录内容:ls
Section titled “列出目录内容:ls”ls 命令列出目录中的文件和子目录。默认情况下,它列出当前目录的内容。
$ lsindex.html styles.css images scripts要列出特定目录的内容:
$ ls /etcacpi cron.d hosts lvm profileadobe cron.hourly hosts.allow machine-id protocolsalternatives cron.monthly hosts.deny magic pulse...ls 的常用有用选项包括:
-l:长格式 (显示权限、所有者、大小、修改日期)。-a:所有文件 (包括以.开头的隐藏文件)。-h:人类可读的大小 (例如,4.0K,1.2M)。常与-l一起使用。-t:按修改时间排序,最新的排在前面。-r:排序时反转顺序。
$ ls -alhtotal 20Kdrwxr-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.htmldrwxr-xr-x 2 alice alice 4.0K Jul 25 14:26 imagesdrwxr-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
Section titled “创建目录:mkdir”mkdir (make directory) 命令创建新目录。
$ mkdir backups$ mkdir documents reports presentations第一个命令在当前位置创建一个名为 backups 的目录。第二个命令创建三个独立的目录。
创建父目录 (-p)
Section titled “创建父目录 (-p)”如果您尝试创建一个父目录不存在的目录,mkdir 将会失败。
$ mkdir /tmp/myproject/datamkdir: 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 和 rm -r
Section titled “删除目录:rmdir 和 rm -r”rmdir (remove directory) 命令删除空目录。
$ rmdir old_backups$ rmdir temp_dir project_archive如果目录包含任何文件或子目录,rmdir 将会失败。
$ rmdir documentsrmdir: 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
Section titled “更改目录:cd”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
Section titled “重命名或移动目录:mv”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/特殊目录:. 和 ..
Section titled “特殊目录:. 和 ..”每个目录都包含两个特殊的条目:
.(点):代表当前目录本身。..(点点):代表父目录 (上一级目录)。
这些对于相对路径导航至关重要,特别是 .. 用于向上移动层级。
# 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 .