linux每日命令:cd命令

Posted Mr_almost

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux每日命令:cd命令相关的知识,希望对你有一定的参考价值。

1.命令格式:

cd   [目录名]

2.命令功能

切换当前目录至 [目录名]

3. 常用范例

1.进入系统根目录

命令:

cd  / 

说明:进入系统根目录,上面命令执行完后拿ls命令看一下,当前目录已经到系统根目录了

输出:

hc@hc-virtual-machine:~$ pwd
/home/hc
hc@hc-virtual-machine:~$ cd /
hc@hc-virtual-machine:/$ ls
bin    dev   initrd.img      lib64       mnt   root  snap      sys  var
boot   etc   initrd.img.old  lost+found  opt   run   srv       tmp  vmlinuz
cdrom  home  lib             media       proc  sbin  swapfile  usr

pwd的作用是查询当前所在目录

2. 返回父级目录

命令:

cd  ..

输出:

hc@hc-virtual-machine:~/PycharmProjects$ pwd
/home/hc/PycharmProjects
hc@hc-virtual-machine:~/PycharmProjects$ cd ..
hc@hc-virtual-machine:~$ pwd
/home/hc

3.进入当前目录的父目录的父目录

命令:

cd  ../..

输出:

hc@hc-virtual-machine:~/PycharmProjects$ pwd
/home/hc/PycharmProjects
hc@hc-virtual-machine:~/PycharmProjects$ cd ../..
hc@hc-virtual-machine:/home$ pwd
/home

4. 进入当前用户主目录

“当前用户主目录”和“系统根目录”是两个不同的概念。进入当前用户主目录有两个方法。

命令1:

cd

直接输入cd,然后回车

输出:

hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd 
hc@hc-virtual-machine:~$ pwd
/home/hc

上面的命令是我在非root用户下操作的,现在我切换到root用户下再操作一遍

[email protected]:~/PycharmProjects/my_test$ sudo su
[email protected]:/home/hc/PycharmProjects/my_test# pwd
/home/hc/PycharmProjects/my_test
[email protected]:/home/hc/PycharmProjects/my_test# cd 
[email protected]:~# pwd
/root

命令2:

cd ~

输出

hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd ~
hc@hc-virtual-machine:~$ pwd
/home/hc

5. 跳转到指定目录

命令

 cd /home/hc/PycharmProjects/my_test/

输出

hc@hc-virtual-machine:/$ pwd
/
hc@hc-virtual-machine:/$ cd /home/hc/PycharmProjects/my_test/
hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test

6.返回进入此目录之前所在的目录

命令:

cd -

说明:自动跳转到进入此目录之前所在的目录,并输出所在目录名称

输出:

hc@hc-virtual-machine:/$ pwd
/
hc@hc-virtual-machine:/$ cd /home/hc/PycharmProjects/my_test/
hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd -
/
hc@hc-virtual-machine:/$ pwd
/

7. 把上个命令的参数作为cd参数使用

命令:

cd !$

输出:

hc@hc-virtual-machine:~/PycharmProjects/my_test$ pwd
/home/hc/PycharmProjects/my_test
hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls /opt/
google
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd !$
cd /opt/
hc@hc-virtual-machine:/opt$ pwd
/opt

以上是关于linux每日命令:cd命令的主要内容,如果未能解决你的问题,请参考以下文章

linux每日命令:ps命令

Linux shell 命令cd是怎么实现的?放在哪个文件夹?

linux每日命令(38):iostat命令

linux中的cd ..和cd -命令有啥区别?

linux每日命令:ls命令

linux每日命令:which命令命令