新用户必备的 8 个 Linux 文件导航命令
基础知识就是基础知识。然而,这些基本命令和技能对于 Linux 系统的日常工作至关重要。有时,新用户会被从命令行管理 Linux 的细节所困扰。他们需要的是快速概述或提醒,以帮助他们入门。花哨的选项稍后再介绍。
为此,本文为您提供了文件管理不可或缺的八个基本文件系统导航概念和命令。
什么是路径?
在学习如何浏览文件系统之前,最好先了解一下 Linux 如何组织数据。导航基于路径的概念。这些路径指定要遍历哪些目录才能到达特定的子目录或文件。路径基本上是说:去这里,去这里,去这里,你就会找到这个。
路径有两种类型:绝对路径和相对路径。
绝对路径
绝对路径是资源的完整路径,从文件系统的根目录开始。文件系统根目录用一个正斜杠表示:/
此路径定义从文件系统顶部开始的目录列表。它最适合用于脚本、目录挂载命令以及当您可能对如何到达给定目标感到困惑时。
相对路径
相对路径因您在文件系统中的位置而异,因此有“相对”一词。因此,路径中指定的目录会根据您所在的位置和您要去的地方而变化。最好的思考方式之一是假设您已经走在绝对路径的一部分,而相对路径只是定义了到达目的地的剩余路程。
例如,如果您已经在主目录中(对我来说,就是/home/damon
),并且想要在目录Rock
的子目录中查找 OGG 文件Music
,那么您不必指定/home/damon
— — 已经遍历过了。您只需要指定相对于您当前位置的其余路径。因此,在这种情况下,相对路径将是Music/Rock
。
8 个命令来导航你的 Linux 文件系统
以下命令是任何 Linux 用户(不仅仅是系统管理员)的必备知识。尝试一下并将它们添加到您的日常工作中。
查找您的位置
导航的秘诀在于,无论是穿越山脉还是 Linux 文件系统,都要知道自己身在何处。如果不知道从哪里出发,就很难到达目的地。
The first command to help with this is pwd
. This command displays the present working directory, letting you know where you are now. From there, you could use an absolute or relative path to get to the desired directory.
Another useful command is tree
. The tree
command displays filesystem information in a similar manner to a graphical interface. This can be handy for new Linux users who are more used to the hierarchical filesystem display in other operating systems.
Go somewhere else
Now that you know where you are and how to use paths to define where you want to go, it's time to cover the cd
(change directory) command. This command moves you to the specified directory, changing your present working directory location.
For example, to use an absolute path to move to the /etc/ssh
directory, type the following command:
$ cd /etc/ssh
By using the absolute path, it doesn't matter where in the filesystem you currently are.
Take a shortcut
Shortcuts can be handy when it comes to navigation. Linux has been around a long time now (30+ years), and Unix even longer. Over the decades, many shortcuts have been created to make navigation easier. Three of them are:
- Single dot, or
.
- Double dot, or
..
- Tilde, or
~
The single dot represents the present working directory, or where you are right now. Say you're in your home directory and you want to copy the sshd_config
file from /etc/ssh
. You can specify it with just a dot because you're copying the file to your current directory. The command looks like this:
$ sudo cp /etc/ssh/sshd_config .
Double dots represent the parent directory, or the directory immediately above the current one in the filesystem. If there's a subdirectory named Rock
in the Music
directory, then Music
is the parent directory of Rock
. As another example, consider where log files are stored: /var/log
. In that case, var
is the parent directory of log
(and the filesystem root /
is the parent of var
).
So, to move from the current Rock
directory to the Music
directory above it, type:
$ cd ..
This is much quicker than defining the entire absolute path:
cd /home/damon/Music
Similarly, the tilde character ~
represents the current, logged-on user's home directory. To copy the sshd_config
file from /etc/ssh
to your home directory (no matter where in the filesystem I'm presently located), type:
$ cp /etc/ssh/sshd_config ~
Again, quicker than cp /etc/ssh/sshd_config /home/damon
.
The tilde works with the cd
command, too, so you can move to your home directory quickly:
$ cd ~
However, cd
itself assumes your home directory if you don't specify an argument, so you can actually jump home with just cd
by itself.
Know the essentials
Experienced Linux users will find this summary pretty trivial (though you may find some time-saving tricks in fundamentals writeups such as this), but for new Linux users, commands such as these are essential. They cover basic concepts and tasks that we all do daily.
Do you know other critical file navigation tricks and commands? Consider sharing them by writing an article for Enable Sysadmin; for more information or to submit an article, contact the editorial team at enable-sysadmin@redhat.com.