Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the cd command in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use the cd command in Linux", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to use the cd command in Linux" this article.

Cd command is the most commonly used command in Linux, and its main function is to switch the user's current working directory.

Cd switches the user's current working directory.

Summary cd [- L | [- P [- e]] [dir] the main purpose is to switch the working directory to dir. The representation of dir can be an absolute path or a relative path.

If the parameter dir is omitted, it defaults to the user's shell variable HOME.

The shell variable HOME,. that is represented as the consumer if dir is specified as ~ Represents the current directory. Represents the directory one level above the current directory.

The environment variable CDPATH is one or more directories separated by colons, and you can add the upper level of frequently used directories to CDPATH to make it easy to access them; if dir starts with /, then CDPATH will not be used.

When the shopt option cdable_vars is turned on, if dir does not exist in both CDPATH and the current directory, it will be treated as a variable and its value will be read as the directory to enter.

Parameter dir (optional): specifies the directory to switch to.

Option-L (default) if the target directory you want to switch to is a symbolic link, change to the directory of the symbolic connection. -P if the target directory you want to switch to is a symbolic connection, switch to the physical location directory it points to. -the current working directory will be changed to the directory represented by the environment variable OLDPWD, that is, the previous working directory. The return status is successful unless you cannot enter the specified directory.

For example, cd # goes to the user's home directory; cd / # to the root directory cd ~ # to the user's home directory; cd. # return to the parent directory (if the current directory is "/", it will still be "/"; ".." after execution For the meaning of superior directory); cd.. /.. # return to the upper two-level directory; cd! $# use the parameter of the previous command as the cd parameter. Instructions for switching to the previous working directory

The cd-# command first displays the target directory to be changed to, and then enters. The cd ${OLDPWD} # command changes directly to the previous working directory. About CDPATH

# set the desktop folder as the value of CDPATH. CDPATH='~/Desktop' # suppose we're going to demonstrate that there are no test3 folders under the paths ~ and ~ / Desktop, so create them now. Mkdir ~ / test3 mkdir ~ / Desktop/test3 # enter the ~ directory. Cd ~ # enter the test3 directory. After cd test3 # executes, display ~ / Desktop/test3 and enter that directory instead of the test3 directory of ~ directory. # if there is a value in CDPATH, then first find and enter the first successful match in CDPATH, and if all fail, then try the current directory last. About cdable_vars

# Open option. Shopt-s cdable_vars # assumes that the current path and CDPATH do not have a directory named new_var. New_var='~/Desktop' # attempts to enter. Cd new_var # turn off option. Shopt-u cdable_vars Note that this command is a bash built-in command. For more information, please see the help command.

It is recommended that when it is necessary to use the cd command during scripting, add the necessary comments to remind the reader of the current working directory to avoid problems such as not finding the file.

The above is all the contents of the article "how to use cd commands in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report