Linux 目录结构和重要文件路径详细说明

注:机翻,未校。


Linux Directory Structure and Important Files Paths Explained

Ravi Saive

Last Updated: November 15, 2023

Brief: This article gives a breakdown of the Linux File System/directory structure, some of the critical files, their usability, and their location.

简介:本文详细介绍了 Linux 文件系统 / 目录结构、一些关键文件、它们的可用性和位置。

You must have probably heard that everything is considered a file in UNIX and UNIX derivatives such as Linux. If not a file, then it must be a running process.

您可能听说过,在 UNIX 和 UNIX 衍生产品(如 Linux)中,所有内容都被视为文件。如果不是文件,则它必须是正在运行的进程。

There are three broad categories of files in Linux.Linux 中有三大类文件。

  • General Files – These are ordinary files made up of binary or ASCII data. These are regular files such as documents, images, audio and video files, etc.

    常规文件 – 这些是由二进制或 ASCII 数据组成的普通文件。例如文档、图像、音频和视频文件等,这些是常规文件。

  • Directory Files – In Linux, directories are also categorized as files since they also serve as storage space for other files and folders.

    目录文件 – 在 Linux 中,目录也被归类为文件,因为它们也用作其他文件和文件夹的存储空间。

  • Device Files – These are special files that provide an interface to device drivers which enable the usability of hardware devices on the system such as mice, keyboards, USB devices, hard drives, etc. These are found in the /dev directory.

    设备文件 – 这些是特殊文件,为设备驱动程序提供接口,使系统上的硬件设备(如鼠标、键盘、USB 设备、硬盘驱动器等)可用。这些位于 /dev 目录中。

Linux Directory Structure Diagram

Linux 目录结构图

A standard Linux distribution follows the directory structure as provided below with Diagram and explanation.

标准 Linux 发行版遵循下面提供的目录结构,并附有图表和说明。

树状概览

  ~  
Linux File System Structure
  ~  
星状概览
  ~  
在这里插入图片描述

Linux Directory Structure

Each of the above directories (which is a file, in the first place) contains important information, required for booting to device drivers, configuration files, etc. Describing briefly the purpose of each directory, we are starting hierarchically.

上述每个目录(首先是一个文件)都包含启动到设备驱动程序、配置文件等所需的重要信息。简要描述每个目录的用途,我们按层次结构开始。

/ Directory

The root directory, denoted by a single forward slash (/), is the uppermost directory in the Linux directory structure. It contains all directories, sub-directories, and files on your Linux system. It’s from the root directory where the Linux directory hierarchy starts.

根目录,由单个正斜杠表示(/),是 Linux 目录结构中最上层的目录。它包含 Linux 系统上的所有目录、子目录和文件。它来自 Linux 目录层次结构开始的根目录。

NOTE: The root directory (/) should not be confused with the root home directory (/root).

注意 : 不应将根目录(/)与根主目录 (/root) 混淆。

  ~  
在这里插入图片描述

/boot Directory

This is one of the most vital directories in a Linux system. As the name infers, the boot directory contains Linux boot files such as the bootloader, the kernel, and its associated files. Files such as ‘vmlinuz‘ represent the compressed image of the Linux kernel.

这是 Linux 系统中最重要的目录之一。顾名思义,引导目录包含 Linux 引导文件,例如引导加载程序、内核及其关联文件。诸如 ’ vmlinuz ’ 之类的文件表示 Linux 内核的压缩映像。

  ~  
在这里插入图片描述

/etc Directory

The /etc directory contains system configuration files for all the services, scripts, and third-party applications that are installed. This directory is considered the nerve center of the Linux system.

/etc 目录包含已安装的所有服务、脚本和第三方应用程序的系统配置文件。这个目录被认为是 Linux 系统的神经中枢。

  ~  
/etc Directory in Linux

/home Directory

The /home directory is a directory that contains a user’s personal folders and files. On a graphical Linux system, the home directory, by default, contains folders such as Desktop, Documents, Downloads, Pictures, Videos, and Public.

/home 目录是包含用户个人文件夹和文件的目录。在图形 Linux 系统上,默认情况下,主目录包含 Desktop、Documents、Downloads、Pictures、Videos 和 Public 等文件夹。

In addition, the /home directory contains personal configuration files which are prefixed with a dot (.). These are hidden files that contain user-specific settings for the login shell session.

此外,/home 目录包含以 dot 为前缀的个人配置文件(.)。这些是隐藏文件,其中包含 login shell 会话的用户特定设置。

  ~  
Home Directory in Linux   ~  

/root Directory

The /root directory is the home directory for the root user, which is also referred to as the root user’s home directory (and not as the root (/) directory).

/root 目录是 root 用户的主目录,也称为 root 用户的主目录(而不是根(/)目录)。

The root account also referred to as the superuser, administrative user, system administrator or just the root user has all the access to commands and system files in Linux.

root 账户也称为超级用户、管理用户、系统管理员或简称为 root 用户,拥有对 Linux 中命令和系统文件的所有访问权限。

  ~  
Root User Directory in Linux   ~  

/opt Directory

The /opt directory is a directory that is reserved for add-on packages and third-party software applications that are not included by default in the system’s official repositories.

/opt 目录是为默认情况下未包含在系统官方存储库中的附加组件包和第三方软件应用程序保留的目录。

For example, when you install applications such as Skype, Discord, Spotify, and Java, to mention a few, they get stored in the /opt directory.

例如,当您安装应用程序时,例如 Skype,Discord,Spotify及 Java,仅举几例,它们存储在 /opt 目录中。

  ~  
/opt Directory in Linux   ~  

/dev Directory

The /dev directory contains device files or special files for devices that are attached to the system such as the hard drive, keyboard, and mouse. As mentioned, these are not regular that a user can read and write to.

/dev 目录包含设备文件或连接到系统的设备的特殊文件,例如硬盘驱动器、键盘和鼠标。如前所述,这些不是用户可以读取和写入的规则。

These are abstractions of standard devices that applications on your system interact with through input and output system calls.

这些是标准设备的抽象,系统上的应用程序通过输入和输出系统调用与之交互。
  ~  
在这里插入图片描述   ~  

/var Directory

The /var directory stores system-generated variable files, which include log files, caches, and spool files just to mention a few.

/var 目录存储系统生成的变量文件,其中包括日志文件、高速缓存和假脱机文件,仅举几例。

  ~  
/var Directory in Linux   ~  

/bin Directory

The /bin directory contains user binaries, executable programs, and common system commands that are used by all users in the system. These include ls, pwd, cat, mkdir, cd, mv, cp, du, df, tar, rpm, wc, history, etc.

/bin 目录包含系统中所有用户使用的用户二进制文件、可执行程序和常用系统命令。这些包括 ls、pwd、cat、mkdir、cd、mv、cp、du、df、tar、rpm、wc、history 等。
  ~  
/bin Directory in Linux   ~  

/sbin Directory

The /sbin directory contains executable files, utilities, and system commands that are reserved for the root user or a user with root privileges. Such commands include halt, reboot, mkfs, fsck, fdisk, halt, iptables, ifconfig, ip, swapon, etc.

/sbin 目录包含为 root 用户或具有 root 权限的用户保留的可执行文件、实用程序和系统命令。此类命令包括 halt、reboot、mkfs、fsck、fdisk、halt、iptables、ifconfig、ip、swapon 等。

  ~  
在这里插入图片描述   ~  

/usr Directory

The /usr directory ranks as one of the most important directories due to the enormous amount of data it holds. The directory contains system-wide read-only files. These include libraries, user binaries and their documentation, programs, and system utilities.

/usr 目录因其包含大量数据而被列为最重要的目录之一。该目录包含系统范围的只读文件。这些包括库、用户二进制文件及其文档、程序和系统实用程序。

  ~  
/usr Directory in Linux   ~  

/proc Directory

The /proc directory is somewhat of a strange directory. It is a virtual or pseudo filesystem that contains vital information about running processes. It is considered the control and information center for the Linux kernel.

/proc 目录有点奇怪。它是一个虚拟或伪文件系统,包含有关正在运行的进程的重要信息。它被认为是 Linux 内核的控制和信息中心。

The filesystem is created on the fly upon system startup and is destroyed once the system is powered off.

文件系统是在系统启动时动态创建的,并在系统关闭电源后销毁。

  ~  
在这里插入图片描述   ~  

/mnt Directory

The /mnt directory along with its subdirectories is a directory intended to be used as a temporary mount point for mounting storage devices such as Hard disk drives, USB drives, and CDROMs.

/mnt 目录及其子目录是用作挂载存储设备(如硬盘驱动器、USB 驱动器和 CDROM)的临时挂载点的目录。

  ~  
mnt Directory in Linux   ~  

/sys Directory

The /sys is a virtual file system that contains a set of virtual files that provide an interface to the Linux kernel.

/sys 是一个虚拟文件系统,其中包含一组为 Linux 内核提供接口的虚拟文件。

  ~  
在这里插入图片描述   ~  

/media Directory

The /media directory is a directory where the system mounts removable media such as USB drives.

/media 目录是系统挂载可移动媒体(如 USB 驱动器)的目录。

  ~  
/media Directory in Linux   ~  

/run Directory

The /run directory is a temporary filesystem that contains volatile runtime data that shows the system has since it was booted. Files under the /run directory must be deleted (removed or truncated as convenient) at the start of the boot process.

/run 目录是一个临时文件系统,其中包含可变的运行时数据,这些数据显示系统自引导以来的运行时间。在引导过程开始时,必须删除 /run 目录下的文件(为了方便起见,将其删除或截断)。

  ~  
/run Directory in Linux   ~  

/tmp Directory

The /tmp directory is a directory that stores temporary files and many programs use this directory to create lock files and keep the temporary storage of data.

/tmp 目录是存储临时文件的目录,许多程序使用此目录创建锁定文件并保留数据的临时存储。

Do not delete files under the/tmp directory unless you know exactly what you are doing! Many of these files are critical for presently running programs and removing them may affect a system crash.

除非您确切地知道自己在做什么,否则不要删除 /tmp 目录下的文件!其中许多文件对于当前正在运行的程序至关重要,删除它们可能会影响系统崩溃。

  ~  
/tmp Directory in Linux   ~  

/lib Directory

The /lib directory stores all the essential standard libraries required by user binaries in the /bin directory.

/lib 目录将用户二进制文件所需的所有基本标准库存储在 /bin 目录中。

  ~  
/lib Directory in Linux   ~  

/lost+found Directory

The lost+found directory is installed during the installation of Linux, useful for recovering files that may be broken due to unexpected shut-down.lost+found 目录是在 Linux 安装期间安装的,可用于恢复可能因意外关闭而损坏的文件。

/srv Directory

The /srv directory is the service directory and is abbreviated as ‘srv‘. This directory contains server-specific and service-related files.

/srv 目录是服务目录,缩写为“srv”。此目录包含特定于服务器的文件和与服务相关的文件。

Exploring Important file, their location, and their Usability

探索重要文件、它们的位置和可用性

Linux is a complex system that requires a more complex and efficient way to start, stop, maintain and reboot a system, unlike Windows. There is a well-defined configuration file, binaries, man pages, info files, etc. for every process in Linux.

Linux 是一个复杂的系统,需要一种更复杂和有效的方法来启动、停止、维护和重新启动系统,这与 Windows 不同。Linux 中的每个进程都有一个定义明确的 configuration file、binaries、man pages、info files 等。

In addition to the major directories, here is a list of some of the prominent files and directories and their uses.

除了主要目录之外,这里还列出了一些重要的文件和目录及其用途。

Directory/Files 目录/文件Description 描述
/boot/vmlinuzThe Linux Kernel file.
Linux 内核文件。
/dev/hdaDevice file for the first IDE HDD (Hard Disk Drive).
第一个 IDE HDD(硬盘驱动器)的设备文件。
/dev/hdcDevice file for the IDE Cdrom, commonly.
IDE Cdrom 的设备文件,通常。
/dev/sdaDevice file for the first SATA Drive (Hard Disk Drive).
第一个 SATA 驱动器(硬盘驱动器)的设备文件。
/dev/nullA pseudo-device, that doesn’t exist. Sometimes garbage output is redirected to /dev/null, so that it gets lost, forever.
不存在的伪设备。有时,垃圾输出会重定向到 /dev/null,因此它会永远丢失。
/etc/bashrcThis file contains system-wide defaults, functions, and aliases among other files that are used by all the system users.
此文件包含系统范围的默认值、函数和别名以及所有系统用户使用的其他文件。
/etc/crontabThis is a system-wide file that is uniquely formatted to schedule or automate system tasks on a Linux system.
这是一个系统范围的文件,具有独特的格式,用于在 Linux 系统上计划或自动执行系统任务。
/etc/exportsA file that determines which file systems are exported to remote hosts and specifies options.
确定将哪些文件系统导出到远程主机并指定选项的文件。
/etc/fstabThis is a special file that contains information about all available mount points and mount point options. Each line in this file provides six options, each of which denotes information about a mount point or filesystem.
这是一个特殊文件,其中包含有关所有可用挂载点和挂载点选项的信息。此文件中的每一行都提供 6 个选项,每个选项都表示有关挂载点或文件系统的信息。
/etc/hostsThis is a configuration file that maps system hostnames to their corresponding IP addresses.
这是一个配置文件,用于将系统主机名映射到其相应的 IP 地址。
/etc/hosts.allowThe file specifies which hosts are permitted to connect to the local system.
该文件指定允许哪些主机连接到本地系统。
/etc/host.denyThe file specifies which hosts are denied access and services on the local machine.
该文件指定拒绝本地计算机上的哪些主机访问和服务。
/etc/issueContains a pre-login message.
包含登录前消息。
/etc/modulesThis file contains the names of kernel modules that should be loaded at boot time, one per line.
此文件包含应在启动时加载的内核模块的名称,每行一个。
/etc/motdmotd stands for a message of the day, the message users get upon login.
motd 代表 a message of the day,用户登录时收到的消息。
/etc/mtabA read-only file that contains a list of currently mounted filesystems.
一个只读文件,其中包含当前挂载的文件系统列表。
/etc/passwdA file that contains the system user’s information such as the username, UID, GID, and login shell among others.
包含系统用户信息的文件,例如用户名、UID、GID 和登录 shell 等。
/etc/printcapContains printer information that is generated by the /etc/cups/printers.conf file.
包含由 /etc/cups/printers.conf 文件生成的打印机信息。
/etc/profileContains Linux system-wide environment and other startup scripts.
包含 Linux 系统范围的环境和其他启动脚本。
/etc/profile.dApplication script, executed after login.
应用程序脚本,登录后执行。
/etc/rc.dInformation about run level specific script.
有关运行级别特定脚本的信息。
/etc/rc.d/init.dRun Level Initialisation Script.
运行级初始化脚本。
/etc/resolv.confThis is a DNS resolver file. It specifies how the system leverages DNS to resolve hostnames.
这是一个 DNS 解析程序文件。它指定系统如何利用 DNS 解析主机名。
/etc/securityContains configuration files for various PAM modules.
包含各种 PAM 模块的配置文件。
/etc/skelThis is a directory that contains a set of user configuration files that are copied to the user’s home directory when a user is created.
这是一个目录,其中包含一组用户配置文件,这些文件在创建用户时被复制到用户的主目录。
/etc/X11This is a directory that contains configuration files for the X-window System.
这是包含 X-window 系统的配置文件的目录。
/usr/binNormal user executable commands.
普通用户可执行命令。
/usr/bin/X11This directory contains infinitely nested directories and binaries for the X Windows System.
此目录包含 X Windows 系统的无限嵌套目录和二进制文件。
/usr/includeThe directory contains header files for C compilers. This includes stdio.h, stdlib.h, and string.h among others.
该目录包含 C 编译器的头文件。这包括 stdio.h、stdlib.h 和 string.h 等。
/usr/shareShared directories of man files, info files, etc.
man files、info files 等的共享目录。
/usr/libThis directory consists of object files and directories.
此目录由目标文件和目录组成。
/usr/sbinThe directory contains binaries with superuser privileges or for System Administration.
该目录包含具有超级用户权限或用于系统管理的二进制文件。
/proc/cpuinfoThe file contains system info including CPU model, model name, number of cores, and clock speed to mention a few files.
该文件包含系统信息,包括 CPU 型号、型号名称、内核数和时钟速度等。
/proc/interruptsInformation about the current interrupts being utilized currently.
有关当前正在使用的中断的信息。
/proc/ioportsThe file contains all the Input/Output addresses used by devices on the server.
该文件包含服务器上设备使用的所有输入/输出地址。
/proc/meminfoA file that stores memory usage information including swap information.
存储内存使用信息(包括交换信息)的文件。
/proc/modulesA file that lists all the modules being used by the kernel.
列出内核正在使用的所有模块的文件。
/proc/mountThe file contains detailed mounted file-system information.
该文件包含详细的挂载文件系统信息。
/proc/statThe file contains detailed information about the system and kernel activity.
该文件包含有关系统和内核活动的详细信息。
/proc/swapsThe file contains information about the swap file.
该文件包含有关交换文件的信息。
/proc/versionThe file contains Linux version information.
该文件包含 Linux 版本信息。
/var/log/lastlogA binary file that contains information about the last successful user logins.
一个二进制文件,其中包含有关上次成功用户登录的信息。
/var/log/messagesThe file contains a log of messages produced by the syslog daemon at boot.
该文件包含 syslog 守护程序在启动时生成的消息日志。
/var/log/syslogA file that contains non-critical system logs.
包含非关键系统日志的文件。
/var/log/wtmpA file listing the login time and duration of each user on the system currently.
列出当前系统上每个用户的登录时间和持续时间的文件。

  ~  
下表来自百科,可作为参照理解。
  ~  

Directory 目录Description 描述
/Primary hierarchy root and root directory of the entire file system hierarchy.
主层次结构 整个文件系统层次结构的根目录和根目录。
/binEssential command binaries that need to be available in single-user mode, including to bring up the system or repair it, for all users (e.g., cat, ls, cp).
需要在单用户模式下可用的基本命令二进制文件,包括启动系统或修复系统,所有用户(例如 cat、ls、cp)。
/bootBoot loader files (e.g., kernels), initrd.
引导加载程序文件(例如,kernels)、initrd。
/devDevice files (e.g., /dev/null, /dev/disk0, /dev/sda1, /dev/tty, /dev/random).
设备文件(例如 /dev/null、/dev/disk0、/dev/sda1、/dev/tty、/dev/random)。
/etcHost-specific system-wide configuration files.
特定于主机的系统范围配置文件。
/etc/optConfiguration files for add-on packages stored in /opt.
存储在 /opt 中的附加组件包的配置文件。
/etc/sgmlConfiguration files, such as catalogs, for software that processes SGML.
处理 SGML 的软件的配置文件,例如目录。
/etc/X11Configuration files for the X Window System, version 11.
X Window System 版本 11 的配置文件。
/etc/xmlConfiguration files, such as catalogs, for software that processes XML.
处理 XML 的软件的配置文件,例如目录。
/homeUsers’ home directories, containing saved files, personal settings, etc.
用户的主目录,包含已保存的文件、个人设置等。
/libLibraries essential for the binaries in /bin and /sbin.
/bin 和 /sbin 中的二进制文件所必需的库。
/lib<qual>Alternate format essential libraries. These are typically used on systems that support more than one executable code format, such as systems supporting 32-bit and 64-bit versions of an instruction set.
替代格式基本库。这些通常用于支持多种可执行代码格式的系统,例如支持 32 位和 64 位版本的指令集的系统。
/mediaMount points for removable media such as CD-ROMs.
可移动媒体(如 CD-ROM)的挂载点。
/mntTemporarily mounted filesystems.
临时挂载的文件系统。
/optAdd-on application software packages.
附加应用程序软件包。
/procVirtual filesystem providing process and kernel information as files.
虚拟文件系统,以文件的形式提供进程和内核信息。
/rootHome directory for the root user.
root 用户的 Home 目录。
/runRun-time variable data: Information about the running system since last boot, e.g., currently logged-in users and running daemons.
运行时变量数据:自上次引导以来正在运行的系统的信息,例如,当前登录的用户和正在运行的守护进程。
/sbinEssential system binaries (e.g., fsck, init, route).
基本系统二进制文件(例如 fsck、init、route)。
/srvSite-specific data served by this system, such as data and scripts for web servers, data offered by FTP servers, and repositories for version control systems.
该系统提供的特定站点数据,例如 Web 服务器的数据和脚本、FTP 服务器提供的数据以及版本控制系统的存储库。
/sysContains information about devices, drivers, and some kernel features.
包含有关设备、驱动程序和某些内核功能的信息。
/tmpDirectory for temporary files (see also /var/tmp). Often not preserved between system reboots and may be severely size-restricted.
临时文件的目录(另请参阅 /var/tmp)。通常在系统重启之间不保留,并且可能会受到严重大小限制。
/usrSecondary hierarchy for read-only user data; contains the majority of (multi-)user utilities and applications. Should be shareable and read-only.
只读用户数据的辅助层次结构;包含大多数(多)用户实用程序和应用程序。应该是可共享的和只读的。
/usr/binNon-essential command binaries (not needed in single-user mode); for all users.
非必要的命令二进制文件(在单用户模式下不需要);适用于所有用户。
/usr/includeStandard include files.
标准包含文件。
/usr/libLibraries for the binaries in /usr/bin and /usr/sbin.
/usr/bin 和 /usr/sbin 中的二进制文件库。
/usr/libexecBinaries run by other programs that are not intended to be executed directly by users or shell scripts (optional).
由其他程序运行的二进制文件,这些程序不打算由用户或 shell 脚本直接执行(可选)。
/usr/lib<qual>Alternative-format libraries (e.g., /usr/lib32 for 32-bit libraries on a 64-bit machine (optional)).
替代格式库(例如,/usr/lib32 表示 64 位计算机上的 32 位库(可选)。
/usr/localTertiary hierarchy for local data, specific to this host. Typically has further subdirectories (e.g., bin, lib, share).
特定于此主机的本地数据的 Tertiary 层次结构。通常具有其他子目录(例如 bin、lib、share)。
/usr/sbinNon-essential system binaries (e.g., daemons for various network services).
非必要的系统二进制文件(例如,各种网络服务的守护进程)。
/usr/shareArchitecture-independent (shared) data.
独立于架构的 (共享) 数据。
/usr/srcSource code (e.g., the kernel source code with its header files).
源代码(例如,内核源代码及其头文件)。
/usr/X11R6X Window System, Version 11, Release 6 (up to FHS-2.3, optional).
X Window 系统,版本 11,版本 6(最高 FHS-2.3,可选)。
/varVariable files: files whose content is expected to continually change during normal operation of the system, such as logs, spool files, and temporary e-mail files.
可变文件:在系统正常运行期间,其内容预计会不断更改的文件,例如日志、假脱机文件和临时电子邮件文件。
/var/cacheApplication cache data. Such data are locally generated as a result of time-consuming I/O or calculation. The application must be able to regenerate or restore the data. The cached files can be deleted without loss of data.
应用程序缓存数据。此类数据是本地生成的,是耗时的 I/O 或计算的结果。应用程序必须能够重新生成或还原数据。可以删除缓存的文件而不会丢失数据。
/var/libState information. Persistent data modified by programs as they run (e.g., databases, packaging system metadata, etc.).
状态信息。程序在运行时修改的持久性数据(例如,数据库、打包系统元数据等)。
/var/lockLock files. Files keeping track of resources currently in use.
锁定文件。跟踪当前正在使用的资源的文件。
/var/logLog files. Various logs.
日志文件。各种日志。
/var/mailMailbox files. In some distributions, these files may be located in the deprecated /var/spool/mail.
邮箱文件。在某些发行版中,这些文件可能位于已弃用的 /var/spool/mail 中。
/var/optVariable data from add-on packages that are stored in /opt.
存储在 /opt 中的附加组件包中的变量数据。
/var/runRun-time variable data. This directory contains system information data describing the system since it was booted. In FHS 3.0, /var/run is replaced by /run; a system should either continue to provide a /var/run directory or provide a symbolic link from /var/run to /run for backwards compatibility.
运行时变量数据。此目录包含描述系统

Rules for Creating File Names in Linux

Linux 中创建文件名的规则

  • File Names in Linux are case sensitive. Hence, “File1” and “file1” would represent different files.

    Linux 中的文件名区分大小写。因此,“File1” 和 “file1” 将代表不同的文件。

  • File names that begin with a period (“.file1”) are hidden.

    以句点 ( “.file1” ) 开头的文件名将被隐藏。

  • Don’t use spaces.

    不要使用空格。

  • Limit punctuation to periods, dashes, and underscores.

    将标点符号限制为句点、破折号和下划线。

  ~  
directory-structure.jpg   ~  

Root, Parent, and Subdirectories

根目录、父目录和子目录

The directory at the highest level of hierarchy in a file system is called the root directory. The root directory contains all other folders and files. Every directory, except the root directory, lies beneath another directory. The directory containing the current directory is called the parent directory, and the directory located within the current directory is called a subdirectory. Subdirectories branch off the root of the directory tree. Unlike a real tree, directory trees are upside down. Hence, the root is at the top and the branches reach down. When you move into a subdirectory, you are moving down the tree. When you move into a directory’s parent directory, you are moving up the tree.

文件系统中层次结构最高级别的目录称为根目录。根目录包含所有其他文件夹和文件。除根目录外,每个目录都位于另一个目录下。包含当前目录的目录称为 父目录,位于当前目录中的目录称为 子目录 。子目录从目录树的根目录分支出来。与真正的树不同,目录树是颠倒的。因此,根在顶部,树枝向下延伸。当您移动到子目录时,您将沿着树向下移动。当您移动到目录的父目录时,您将向上移动树。

  ~  
root-directory.jpg
  ~  

By default, when you open a terminal and begin using the command line, you are placed in your home directory.

默认情况下,当您打开终端并开始使用命令行时,您将被放置在您的主目录中。

Absolute Path

绝对路径

It’s the path that indicates the position of a file or directory with respect to the root directory. The absolute path of the root directory is a forward slash ( / ). It starts at the root directory ( / ) and works down. To check the absolute path of any directory, open a terminal and type the pwd command. When the command is invoked, the completed path of the current working directory gets printed. The below example shows the absolute path of the home directory.

它是指示文件或目录相对于根目录的位置的路径。根目录的绝对路径是正斜杠 ( / )。它从根目录 ( / ) 开始,然后向下工作。要检查任何目录的绝对路径,请打开终端并键入 pwd 命令。调用该命令时,将打印当前工作目录的完整路径。以下示例显示了主目录的绝对路径。

Example 1

pwd

Outpu

/home/<your-username>

Example 2

Consider that the home directory contains the following files and subdirectories.

假设主目录包含以下文件和子目录。

sample-scripts text.txt
  • If you want to open test.txt from the home directory, simply type the following command.

    如果要从主目录打开 test.txt,只需键入以下命令即可。

cat test.txt
  • On the other hand, if your current working directory isn’t the home directory and you want to open the test.txt file, then you need to specify the absolute path to the file to display it.

    另一方面,如果当前工作目录不是主目录,并且您想要打开test.txt文件,则需要指定文件的绝对路径才能显示它。

cat /home/<your-username>/test.txt

Relative Path

相对路径

It’s the path which is related to the current working directory. It starts at your current directory and never starts with a /. Consider the following example.

它是与当前工作目录相关的路径。它从当前目录开始,从不以 / 开头。请考虑以下示例。

relative path

Example

In the image above, the absolute path for a python directory will be, /home/<username>/sample-scripts/python. Consider that you’re already in the sample-scripts directory and hence the relative path will be python.

在上图中,python目录的绝对路径将为 /home/<username>/sample-scripts/python .假设您已经在目录中,sample-scripts因此相对路径将为 python

Two cryptic symbols are used with relative paths.

两个晦涩的符号用于相对路径。

Symbols 符号Explanation 解释
. (a single dot)
.(单个点)
to represent the current working directory.
表示当前工作目录
… (two dots)
…(两个点)
to represent the parent directory and move up the file hierarchy.
表示父目录并在文件层次结构中向上移动

The above symbols can be better understood with the following examples. Consider that you are inside the python directory.

通过以下示例可以更好地理解上述符号。假设您位于python目录内。

pwd
/home/<username>/sample-scripts/python
  • cd .. helps to go one level up in the file hierarchy.

    cd .. 有助于在文件层次结构中向上提升一个级别。

cd ..
pwd
/home/username/sample-scripts
  • cd . indicates the current working directory and doesn’t move up.

    cd . 指示当前工作目录,并且不会上移。

cd .
pwd
/home/username/sample-scripts/python

via:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值