【Linux】Linux 系统编程——cd 命令

文章目录

1.命令概述

"cd 命令,即 'change directory' 的缩写,主要用于 Unix、Linux 和 macOS 等操作系统中,用于改变当前工作目录。该命令支持绝对路径和相对路径两种形式。若未指定目录名称,cd 命令默认将工作目录切换至用户的主目录(即用户登录时的初始目录)。

在路径表示上,'~' 符号代表用户的主目录,'.' 符号表示当前目录,而 '...' 用于表示当前目录的上级目录。例如,命令 'cd ~' 会切换到用户的主目录,而 'cd ..' 则会将工作目录切换到上一级目录。

2.命令格式

基本格式是 cd [目录]

  • 如果不指定目录,它通常会切换到用户的主目录。
  • 可以使用绝对或相对路径。

3.常用选项

  • cd ~:切换到用户的主目录。
  • cd .:保持在当前目录(不常用)。
  • cd ..:切换到当前目录的上级目录。
  • cd -:切换到上一个工作目录。

4.相关描述

  • cd 是最基本的命令之一,在日常工作中非常有用。
  • 它帮助用户导航到文件系统的不同部分。

5.参考示例

切换到主目录

复制代码
xjc@ubuntu:~/a$ ls
a1.c  a1.txt  b  output.txt
xjc@ubuntu:~/a$ cd 
xjc@ubuntu:~$ ls
a                          A1-QP-MPC-Controller-main.zip  Desktop    Downloads  Music     Pictures  QtCreator  Templates  test1    vi
A1-QP-MPC-Controller-main  clash                          Documents  lesson2    Newcoder  Public    snap       test       test_ws  Videos
xjc@ubuntu:~$ ^C
xjc@ubuntu:~$ 

使用绝对路径切换目录

命令:cd /usr/local

解释:此命令会将当前工作目录切换到 /usr/local。这里,/usr/local 是一个绝对路径,从根目录开始的完整路径。

复制代码
xjc@ubuntu:~$ ls
a                          A1-QP-MPC-Controller-main.zip  Desktop    Downloads  Music     Pictures  QtCreator  Templates  test1    vi
A1-QP-MPC-Controller-main  clash                          Documents  lesson2    Newcoder  Public    snap       test       test_ws  Videos
xjc@ubuntu:~$ cd /usr/local
xjc@ubuntu:/usr/local$ ls
bin  etc  games  include  lib  man  sbin  share  src  webots
xjc@ubuntu:/usr/local$ 

使用相对路径切换目录

命令:cd Documents

解释:如果当前目录中存在 Documents 子目录,该命令会切换到这个子目录。相对路径是基于当前目录的路径。

在这里插入图片描述

复制代码
xjc@ubuntu:/usr/local$ cd 
xjc@ubuntu:~$ ls
a                          A1-QP-MPC-Controller-main.zip  Desktop    Downloads  Music     Pictures  QtCreator  Templates  test1    vi
A1-QP-MPC-Controller-main  clash                          Documents  lesson2    Newcoder  Public    snap       test       test_ws  Videos
xjc@ubuntu:~$ cd Documents
xjc@ubuntu:~/Documents$ 

返回上一级目录

命令:cd ..

解释:该命令会将工作目录从当前目录切换到其父目录(即上一级目录)。

复制代码
xjc@ubuntu:/usr/local$ cd 
xjc@ubuntu:~$ ls
a                          A1-QP-MPC-Controller-main.zip  Desktop    Downloads  Music     Pictures  QtCreator  Templates  test1    vi
A1-QP-MPC-Controller-main  clash                          Documents  lesson2    Newcoder  Public    snap       test       test_ws  Videos
xjc@ubuntu:~$ cd Documents
xjc@ubuntu:~/Documents$ cd ..
xjc@ubuntu:~$ ls
a                          A1-QP-MPC-Controller-main.zip  Desktop    Downloads  Music     Pictures  QtCreator  Templates  test1    vi
A1-QP-MPC-Controller-main  clash                          Documents  lesson2    Newcoder  Public    snap       test       test_ws  Videos
xjc@ubuntu:~$ 

切换到用户的主目录

命令:cd ~

解释:无论当前位于文件系统的哪个位置,此命令都会将工作目录直接切换到用户的主目录。

复制代码
xjc@ubuntu:~$ cd A1-QP-MPC-Controller-main/
xjc@ubuntu:~/A1-QP-MPC-Controller-main$ cd ~
xjc@ubuntu:~$ 

在最近两个工作目录间切换

命令:cd -

解释:此命令允许用户在最近使用的两个工作目录之间来回切换,提高工作效率。

复制代码
xjc@ubuntu:~$ cd A1-QP-MPC-Controller-main/
xjc@ubuntu:~/A1-QP-MPC-Controller-main$ cd ~
xjc@ubuntu:~$ cd -
/home/xjc/A1-QP-MPC-Controller-main
xjc@ubuntu:~/A1-QP-MPC-Controller-main$ 

利用环境变量切换目录

命令:cd $HOME

解释:此命令会将工作目录切换到 HOME 环境变量所指示的目录。在大多数 Unix 和类 Unix 系统中,$HOME 环境变量代表用户的主目录。

复制代码
xjc@ubuntu:~/A1-QP-MPC-Controller-main$ cd $HOME
xjc@ubuntu:~$ 
相关推荐
知北游天1 分钟前
Linux:多线程---深入互斥&&浅谈同步
linux·运维·服务器
Gappsong8743 分钟前
【Linux学习】Linux安装并配置Redis
java·linux·运维·网络安全
whoarethenext14 分钟前
使用 C++/Faiss 加速海量 MFCC 特征的相似性搜索
开发语言·c++·faiss
try2find36 分钟前
移动conda虚拟环境的安装目录
linux·运维·conda
码农101号1 小时前
Linux中容器文件操作和数据卷使用以及目录挂载
linux·运维·服务器
PanZonghui1 小时前
Centos项目部署之Nginx 的安装与卸载
linux·nginx
PanZonghui1 小时前
Centos项目部署之安装数据库MySQL8
linux·后端·mysql
PanZonghui1 小时前
Centos项目部署之运行SpringBoot打包后的jar文件
linux·spring boot
PanZonghui1 小时前
Centos项目部署之Java安装与配置
java·linux
向阳@向远方2 小时前
第二章 简单程序设计
开发语言·c++·算法