每天一个Linux命令(2)cd命令

      cd命令用来切换工作目录至dirname。 其中dirName表示法可为绝对路径或相对路径。若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录)。另外,~也表示为home directory的意思,.则是表示目前所在的目录,..则表示目前目录位置的上一层目录。

    1.用法:

    cd (选项) [目录]

    2.功能:

    切换当前目录至dirName
    3.选项:

    (1) -p                                                           如果要切换到的目标目录是一个符号连接,直接切换到符号连接指向的目标目录

    (2) -L                                                           如果要切换的目标目录是一个符号的连接,直接切换到字符连接名代表的目录,而非符号连接所指向的目标目录。

    (3) -                                                              当仅实用"-"一个选项时,当前工作目录将被切换到环境变量"OLDPWD"所表示的目录。
    4.实例:

    (1)  cd 进入用户主目录;cd ~ 进入用户主目录;

[sunjimeng@localhost ~]$ cd /        /*刚开启terminal终端,是用户的主目录*/   /* “cd /” 进入根目录*/
[sunjimeng@localhost /]$ cd          /*cd命令默认进入用户的主目录*/
[sunjimeng@localhost ~]$             /*默认的主目录是这样的“~”*/
[sunjimeng@localhost ~]$ cd /        /*"cd"命令和"cd ~"可以达到一样的功能*/
[sunjimeng@localhost /]$ cd ~
[sunjimeng@localhost ~]$ 

    (2) cd - 返回进入此目录之前所在的目录;

[sunjimeng@localhost /]$ cd /home/sunjimeng                  /*由根目录进入用户目录*/
[sunjimeng@localhost ~]$ ll                                  //列出用户目录下的目录及文件
total 0
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Desktop
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Documents
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Downloads
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Music
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Pictures
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Public
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Templates
drwxr-xr-x. 2 sunjimeng sunjimeng 6 May  1 01:23 Videos
[sunjimeng@localhost ~]$ cd Desktop                         //选择进入Desktop文件夹
[sunjimeng@localhost Desktop]$ cd -                         //"cd -"命令功能:(1)输出之前的目录名称;(2)返回之前的目录。
/home/sunjimeng 

    (3) cd .. 返回上级目录(若当前目录为“/“,则执行完后还在“/";".."为上级目录的意思);cd ../.. 返回上两级目录;

[sunjimeng@localhost /]$ cd ..                       //在根目录下返回上一级目录,依然是根目录
[sunjimeng@localhost /]$ cd /home/sunjimeng/Desktop  //进入自定义文件夹
[sunjimeng@localhost Desktop]$ cd ..                 //返回上一级 /home/sunjimeng,即用户主目录
[sunjimeng@localhost ~]$ cd ../..                    //返回上两级目录
[sunjimeng@localhost /]$

    (4) cd !$ 把上个命令的参数作为cd参数使用。

[sunjimeng@localhost ~]$ cd /home/sunjimeng/Desktop  //进入用户主目录的桌面文件夹
[sunjimeng@localhost Desktop]$ cd !$                 //cd !$功能:(1)打印这个命令的解释;(2)执行这个命令
cd /home/sunjimeng/Desktop
[sunjimeng@localhost Desktop]$ 
[sunjimeng@localhost Desktop]$ cd ..
[sunjimeng@localhost ~]$ cd !$
cd ..
[sunjimeng@localhost home]$
原文地址:https://www.cnblogs.com/MenAngel/p/5448795.html