Linux directory traversal (cd command)

Insert image description here

Linux directory traversal (cd command)



1. cd command

On a Linux file system, you canUse the cd command to switch the shell session to another directory. The format of the cd command is also very simple:

[root@localhost   / ]cd tmp    //使用 cd 命令切换到 tmp 目录
[root@localhost tmp ]		   //仔细观察可以看到已经进入到 tmp 目录了

The cd command can accept a single parameter destination, which is used to specify the directory name you want to switch to.

The destination parameter can be expressed in two ways: one is to use an absolute file path, the other is to use a relative file path

If no target path is specified for the cd command, it switches to the user's home directory.


2. Absolute file path

The absolute file path defines the specific location of the directory in the directory structure. In Linux, it starts with the root directory of the virtual directory, which is equivalent to the full name of the directory.

Absolute file paths in Linux systems always start with a forward slash (/), indicating the root directory of the virtual file. For example, to point to the user binary file in the bin directory contained in the usr directory, you can use the following absolute file path:

/usr/bin

To use an absolute file path to switch to a specific location in the file system, just specify the full pathname after the cd command:

[root@localhost  ~ ] cd /usr/bin		//使用绝对文件路径切换到 根(/)目录下的 usr 目录下的 bin 目录
[root@localhost  bin ] 			//可以发现已经切换到根目录下的 usr 目录下的 bin 目录中了

注:在上面的例子中,提示符中一开始有一个波浪号(~)。在切换目录之后就被指向的 bin 目录代替了,这个 CLI 提示符就是用来帮助我们跟踪当前所在虚拟目录结构中的位置

Of course, you can also useThe pwd command displays the absolute file path of the directory of the current session.

[root@localhost  bin ] pwd
/usr/bin		//使用 pwd 命令后,就会显示当前会话目录的绝对路径

Use an absolute file path to switch to any level in a Linux virtual directory:

[root@localhost  /usr/bin ] cd /var/log
[root@localhost  /var/log ]
[root@localhost  /var/log ] pwd
/var/log

You can also jump back to the home directory from any level in the Linux virtual directory:

[root@localhost  /var/log ] cd		 //如果cd 命令没有指定目标路径,那么它将切换到用户主目录
[root@localhost  ~ ]
[root@localhost  ~ ] pwd
/root			//我这里用户是 root ,所以回到用户主目录中

3. Relative file path

Relative paths allow the user to specify a target file path based on the current location.

Relative file paths do not start with a forward slash (/) representing the root directory, but directly start with the directory name or a special character.

/
usr
var
boot
bin
etc
cache
gdm
efi
losf+found

If you are in the usr directory and want to switch to the bin subdirectory below it, you can directly use the cd command plus a file relative path:

[root@localhost  usr ] cd bin
[root@localhost  bin ]
[root@localhost  bin ] pwd
/usr/bin

You can also use a special character to indicate a relative directory location. There are two special characters that can be used in relative file paths:
Single dot character (.), indicating the current directory
Double-dot character (…) indicates the parent directory of the current directory

Among them, the single-dot character (.) has little meaning for the cd command; the double-dot character is very convenient when moving in the directory hierarchy. To add the bin directory currently under the usr directory, you need to switch to its upper-level directory usr directory. You can do this:

[root@localhost  bin ]pwd
/usr/bin
[root@localhost  bin ] cd ../		//切换到当前目录的上一级目录
[root@localhost  var ]
[root@localhost  usr ] pwd
/usr

If you need to switch to the directory one level above the current directory, you can also do this:

[root@localhost  bin ]pwd
/usr/bin
[root@localhost  bin ] cd ../../		//切换到当前目录的上一级目录的再上一级目录
[root@localhost  / ]
[root@localhost  / ] pwd
/

In the same way, go to the upper level of the directory and write it in the same way, and so on.


If the article is helpful to you friends, please welcome me! ! !

In addition, if there are any errors in the article, you are welcome to criticize and correct me! ! !

Guess you like

Origin blog.csdn.net/qq_46286412/article/details/132907234