Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Welcome to comment on the deficiencies in the text!

Linux minimal knowledge: 41, the most commonly used CD command details, CD -, CD ~ and switch space directory introduction

This article is part 41 of the Linux Minimalist Trivia series. It introduces useful and practical Linux knowledge.

The CD command

This section describes the CD command

CD (change directory) Changes a directory. Used to switch from the current directory to another specified directory.

Directory path after CD. It can be a relative or absolute directory.

/ Relative path, indicating the current directory. . / Indicates the relative path of the upper-level directory. / XXX backslash indicates an absolute path.

/ indicates the root directory, from which all content starts. /root indicates the home directory of user root.

For Home directories, the Home directories of all users except root are located under /home/(/ Home /

).

  • Absolute Path: The Path starting from the root directory.
  • Relative Path: Relative Path from the current position, e.g. /../.

CD Switch command demo

In Linux, you can use the TAB key to complete all commands related to directories. That is, enter part of the pathname and press TAB to complete the rest.

cd .. / / CD.. / Switches to the current directory

[root_test@VM_0_15_centos test] $cd ../
[root_test@VM_0_15_centos ~]$ 
Copy the code

/, or directly starts with the directory name (file name), indicating the current directory.

Switch to the test directory under the current directory as follows.

[root_test@VM_0_15_centos ~]$ cd test/
[root_test@VM_0_15_centos test] $cd ../
[root_test@VM_0_15_centos ~]$ cd. /test/
[root_test@VM_0_15_centos test] $Copy the code

Switch to the absolute directory /etc/my.cnf.d.

[root_test@VM_0_15_centos test] $cd /etc/my.cnf.d
[root_test@VM_0_15_centos my.cnf.d]$
Copy the code

Help View the help information about the CD command

The CD command is a built-in command of the shell. When viewed using man, the help information of the shell is displayed, but not the help information of the CD.

$ man cd
BASH_BUILTINS(1)            General Commands Manual           BASH_BUILTINS(1)

NAME
       bash,  :,  .,  [, alias.bg.bind.break.builtin.caller.cd.command,
       compgen, complete, compopt,  continue.declare.dirs.disown.echo.enable.eval.exec.exit.export.false.fc.fg.getopts.hash.help.history.jobs.kill.let.local.logout.mapfile.popd.printf.pushd.pwd.read.readonly.return.set.shift.shopt.source.suspend.test.times.trap.true.type.typeset.ulimit.umask.unalias.unset.wait -
       bash built-in commands, see bash(1)

BASH BUILTIN COMMANDS
......
Copy the code

Help CD View the help command:

$ help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
......
Copy the code

CD – Switch to the last directory

The CD – command is used to switch directly to the last directory, that is, to switch between two directories.

[root_test@VM_0_15_centos ~]$ cd /etc/sysconfig/network-scripts/
[root_test@VM_0_15_centos network-scripts]$ pwd
/etc/sysconfig/network-scripts
[root_test@VM_0_15_centos network-scripts]$ cd -
/home/root_test
[root_test@VM_0_15_centos ~]$ cd -
/etc/sysconfig/network-scripts
[root_test@VM_0_15_centos network-scripts]$
Copy the code

cd ~cd $homeBack home directory

The CD ~ command is used to quickly return to the user’s Home Directory. For user root, the value is /root. For other users, the value is /home/user_name.

[root_test@VM_0_15_centos tmp]$ pwd
/var/tmp
[root_test@VM_0_15_centos tmp]$ cd ~
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
Copy the code

The ~ before $or # indicates the current home directory.

~ (tilde) is short for home directory.

CD $HOME is also used for the back home directory.

The root user runs CD ~username to switch to the home directory of the specified user

CD ~username or CD ~username/ is used to switch to the specified user’s home directory. However, this command can only be executed by the root user and cannot be executed by sudo for the administrator.

For example, the root_test user tried to switch to root_test1 and root_test2, but failed!

[root_test@VM_0_15_centos ~]$ cd ~root_test2
-bash: cd: /home/root_test2: Permission denied
[root_test@VM_0_15_centos ~]$ sudo cd ~root_test1
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
[root_test@VM_0_15_centos ~]$ sudo cd ~root_test2
[root_test@VM_0_15_centos ~]$ pwd
/home/root_test
Copy the code

The root user can switch over normally.

[root@VM_0_15_centos ~]# pwd
/root
[root@VM_0_15_centos ~]# cd ~root_test2
[root@VM_0_15_centos root_test2]# pwd
/home/root_test2
Copy the code

Three ways to switch to a directory name with Spaces

There is a directory with Spaces my space dir:

$ ls -l
total 12
-rw-rw-r-- 1 root_test root_test    2 Oct 29 08:52 123
drwxrwxr-x 2 root_test root_test 4096 Oct 29 08:52 my
drwxrwxr-x 2 root_test root_test 4096 Oct 30 10:28 my space dir
-rw-rw-r-- 1 root_test root_test    0 Oct 26 21:52 my.txt
Copy the code

You can use the TAB key to complete the following three methods.

Use the \ escape character

[root_test@VM_0_15_centos test] $cd my\ space\ dir/
[root_test@VM_0_15_centos my space dir]$ pwd
/home/root_test/test/my space dir
Copy the code

Include directory names in double quotes

[root_test@VM_0_15_centos test] $cd "my space dir"/
[root_test@VM_0_15_centos my space dir]$
Copy the code

Use single quotes

[root_test@VM_0_15_centos test] $cd 'my space dir'/
[root_test@VM_0_15_centos my space dir]$ 
Copy the code

The other, CD! $: indicates that the parameters of the previous command are used as CD parameters