[root@CncLuczk ~]# cd [相对路径或绝对路径]
特殊符号 | 作 用 |
---|---|
~ | 代表当前登录用户的主目录 |
~用户名 | 表示切换至指定用户的用户目录 |
- | 代表上次所在目录 |
. | 代表当前目录 |
… | 代表上级目录 |
[root@CncLucZK www]# cd ~ #切换到root用户,表示回到自己的主目录,对于 root 用户,其主目录为 /root
[root@CncLucZK ~]# pwd
/root #当前登录用户的主目录
[root@CncLucZK ~]# cd ~zk #代表切换到zk 这个用户的主目录,亦即 /home/www
[zk@CncLucZK ~]$ pwd
/home/zk
[root@CncLucZK ~]# cd #没有加上任何路径,也代表回到当前登录用户的主目录
/root
[root@CncLucZK ~]# su - zk #切换到zk普通用户
[zk@CncLucZK ~]$ pwd
/home/zk #当前登录用户的主目录
[zk@CncLucZK ~]# cd #没有加上任何路径,也代表回到当前登录用户的主目录
[zk@CncLucZK ~]$ pwd
/home/zk
[root@CncLucZK ~]# cd ..
#表示切换到目前的上一级目录,亦即是 /root 的上一级目录的意思;
需要注意的是,在 Linux 系统中,根目录确实存在 .(当前目录)以及 …(当前目录的父目录)两个目录,但由于根目录是最顶级目录,因此根目录的 … 和 . 的属性和权限完全一致,也就是说,根目录的父目录是自身。
[root@CncLucZK /]# cd - #表示回到刚刚的那个目录
pwd( Print Working Directory打印工作目录) 命令来显示当前目录的绝对路径。由于 Linux 文件系统中有许多目录,当用户执行一条 Linux 命令又没有指定该命令或参数所在的目录时,Linux 系统就会首先在当前目录(目前的工作目录)搜寻这个命令或它的参数。因此,用户在执行命令之前,常常需要确定目前所在的工作目录,即当前目录。
命令的基本格式为:
[root@CncLucZK ~]# pwd [-P] #-P :显示出确实的路径,而非使用链接 (link) 路径。
[root@CncLucZK ~]# whoami #whoami 命令用于确定当前登陆的用户
root
[root@CncLucZK ~]# pwd
/root #root 用户当前所在目录是它的主目录 /root。
[root@CncLucZK /]# cd /usr/bin
[root@CncLucZK bin]# pwd
/usr/bin
[root@CncLucZK bin]
中的 bin,根本无法判断其所在的具体位置,而使用 pwd 命令,可以输出当前所在目录的完整路径。[root@CncLucZK ~]# cd /var/mail #注意,/var/mail是一个链接档
[root@CncLucZK mail]# pwd
/var/mail #列出目前的工作目录
[root@CncLucZK mail]# pwd -P
/var/spool/mail #有没有加 -P 差很多
[root@CncLucZK mail]# ll -d /var/mail
lrwxrwxrwx. 1 root root 10 May 11 2019 /var/mail -> spool/mail
#因为 /var/mail 是链接档,链接到 /var/spool/mail
# 所以,加上 pwd -P 的选项后,会不以链接档的数据显示,而是显示正确的完整路径!
[root@CncLucZK ~]# ls [选项] 目录名称/文件名
选项 | 功能 |
---|---|
-a | 显示全部的文件,包括隐藏文件(开头为 . 的文件)也一起罗列出来,这是最常用的选项之一。 |
-A | 显示全部的文件,连同隐藏文件,但不包括 . 与 … 这两个目录。 |
-d | 仅列出目录本身,而不是列出目录内的文件数据。 |
-f | ls 默认会以文件名排序,使用 -f 选项会直接列出结果,而不进行排序。 |
-F | 在文件或目录名后加上文件类型的指示符号,例如,* 代表可运行文件,/ 代表目录,= 代表 socket 文件,| 代表 FIFO 文件。 |
-h | 以人们易读的方式显示文件或目录大小,如 1KB、234MB、2GB 等。 |
-i | 显示 inode 节点信息。 |
-l | 使用长格式列出文件和目录信息,没有隐藏文件。ll=ls -l |
-n | 以 UID 和 GID 分别代替文件用户名和群组名显示出来。 |
-r | 将排序结果反向输出,比如,若原本文件名由小到大,反向则为由大到小。 |
-R | 连同子目录内容一起列出来,等於将该目录下的所有文件都显示出来。 |
-S | 以文件容量大小排序,而不是以文件名排序。 |
-t | 以时间排序,而不是以文件名排序。 |
–color=never --color=always --color=auto | never 表示不依据文件特性给予颜色显示。 always 表示显示颜色,ls 默认采用这种方式。 auto 表示让系统自行依据配置来判断是否给予颜色。 |
–full-time | 以完整时间模式 (包含年、月、日、时、分)输出 |
–time={atime,ctime} | 输出 access 时间或改变权限属性时间(ctime),而不是内容变更时间。 |
[root@CncLucZK /]# ls -al ~
total 108
dr-xr-x---. 6 root root 4096 Jun 3 22:14 .
dr-xr-xr-x. 23 root root 4096 Oct 7 23:44 ..
-rw------- 1 root root 14354 Oct 7 23:41 .bash_history
-rw-r--r--. 1 root root 18 May 11 2019 .bash_logout
-rw-r--r--. 1 root root 176 May 11 2019 .bash_profile
-rw-r--r--. 1 root root 267 Jun 29 2021 .bashrc
drwx------ 3 root root 4096 Dec 10 2019 .cache
drwx------ 3 root root 4096 Dec 11 2020 .config
-rw-r--r--. 1 root root 100 May 11 2019 .cshrc
-rw-r--r-- 1 root root 29450 May 31 14:23 install.sh
-rw------- 1 root root 695 Dec 31 2020 .mysql_history
drwxr-xr-x 2 root root 4096 Dec 11 2020 .pip
-rw-r--r-- 1 root root 73 Dec 11 2020 .pydistutils.cfg
drwx------ 2 root root 4096 Oct 6 23:42 .ssh
-rw-r--r--. 1 root root 129 May 11 2019 .tcshrc
-rw------- 1 root root 1449 Mar 9 2021 .viminfo
-rw-r--r-- 1 root root 203 Sep 29 23:08 .wget-hsts
Linux 系统中,隐藏文件不是为了把文件藏起来不让其他用户找到,而是为了告诉用户这些文件都是重要的系统文件,如非必要,不要乱动!所以,不论是 Linux 还是 Windows 都可以非常简单地査看隐藏文件,只是在 Windows 中绝大多数的病毒和木马都会把自己变成隐藏文件,给用户带来了错觉,以为隐藏文件是为了不让用户发现。
这里的 ls 命令还使用了 -l 选项,因此才显示出了文件的详细信息,此选项显示的这 7 列的含义分别是:
查看某个目录的详细信息,例如:
[root@CncLucZK /]# ls -l /test/
total 12
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
[root@CncLucZK /]# ls -ld /test/
drwxr-xr-x 2 root root 4096 Oct 4 23:18 /test/
[root@CncLucZK /]# ls -lh
total 84K
drwxr-xr-x 5 root root 4.0K Dec 22 2020 app
lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin
dr-xr-xr-x. 5 root root 4.0K Dec 10 2019 boot
drwxr-xr-x 3 root root 4.0K Dec 22 2020 data
[root@CncLucZK /]# mkdir [-mp] 目录名
-m 选项用于手动配置所创建目录的权限,而不再使用默认权限 (umask) 。
-p 选项递归创建所有目录,以创建 /home/test/demo 为例,在默认情况下,你需要一层一层的创建各个目录,而使用 -p 选项,则系统会自动帮你创建 /home、/home/test 以及 /home/test/demo。
举例:
[root@CncLucZK ~]# cd ../test
[root@CncLucZK test]# ls
demo.txt test1.sh test.sh
[root@CncLucZK test]# mkdir config
[root@CncLucZK test]# ls
config demo.txt test1.sh test.sh
[root@CncLucZK test]# cd config
[root@CncLucZK config]# ls
[root@CncLucZK config]# mkdir springbootdemo/application.yml
mkdir: cannot create directory ‘springbootdemo/application.yml’: No such file or directory #mkdir:无法创建目录,没有那个文件或目录
[root@CncLucZK config]# mkdir -p springbootdemo/application.yml #使用 -p 选项递归建立目录。
[root@CncLucZK config]# ls
springbootdemo ##这里只查看一级子目录,其实后续的application.yml文件都已经建立
注意,我们在建立目录的时候使用的是相对路径,所以这个目录被建立到当前目录下。
[root@CncLucZK test]# mkdir -m 711 user
[root@CncLucZK test]# ls -l
total 20
drwxr-xr-x 3 root root 4096 Oct 8 10:29 config
-rw-r--r-- 1 root root 90 Oct 5 00:15 demo.txt
-rw-r--r-- 1 root root 381 Oct 5 18:14 test1.sh
-rwxr-xr-x 1 root root 117 Oct 5 18:05 test.sh
drwx--x--x 2 root root 4096 Oct 8 12:54 user
[root@CncLucZK ~]# rmdir [-p] 目录名 #-p 选项用于递归删除空目录。
该命令执行成功与否,取决于要删除目录是否是空目录,因为 rmdir 命令只能删除空目录。
[root@CncLucZK test]# ls -R
.:
config demo.txt test1.sh test.sh user./config:
springdemo./config/springdemo:
template./config/springdemo/template:./user:
[root@CncLucZK test]# rmdir -p config/springdemo/template
[root@CncLucZK test]# ls -R
.:
demo.txt test1.sh test.sh user./user:
注意,此方式先删除最低一层地目录(这里先删除 template),然后逐层删除上级目录,删除时也需要保证各级目录是空目录。
rmdir 命令的作用十分有限,因为只能刪除空目录,所以一旦目录中有内容,就会报错
rmdir -p test/* 如果test里的目录里还有下一层目录的话,也是删除不了的。多级目录下只会先删teset里面最低层级的空目录
[root@CncLucZK test]# ls user/
[root@CncLucZK test]# mkdir user/tmp
[root@CncLucZK test]# ls user/
tmp
[root@CncLucZK test]# rmdir user
rmdir: failed to remove 'user': Directory not empty
touch 命令可以用来创建文件(当指定操作文件不存在时,该命令会在当前位置建立一个空文件),此命令更重要的功能是修改文件的时间参数(但当文件存在时,会修改此文件的时间参数)
Linux 系统中,每个文件主要拥有 3 个时间参数(通过 stat 命令进行查看),分别是文件的访问时间、数据修改时间以及状态修改时间:
touch 命令的基本格式如下
[root@CncLucZK test]# touch [选项] 文件名
YYMMDDhhmm
。touch 命令可以只修改文件的访问时间,也可以只修改文件的数据修改时间,但是不能只修改文件的状态修改时间。因为,不论是修改访问时间,还是修改文件的数据时间,对文件来讲,状态都会发生改变,即状态修改时间会随之改变(更新为操作当前文件的真正时间)。
[root@CncLucZK test]# stat userFile: userSize: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd01h/64769d Inode: 1048584 Links: 3
Access: (0711/drwx--x--x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-10-08 13:08:40.819495664 +0800
Modify: 2022-10-08 13:08:35.287480263 +0800
Change: 2022-10-08 13:08:35.287480263 +0800Birth: -
[root@CncLucZK test]# ll --time=atime user
total 4
drwxr-xr-x 2 root root 4096 Oct 8 13:08 tmp
[root@CncLucZK test]# touch user
[root@CncLucZK test]# ll --time=atime user
total 4
drwxr-xr-x 2 root root 4096 Oct 8 13:29 tmp ##而如果文件已经存在,则也不会报错,只是会修改文件的访问时间。
[root@CncLucZK user]# touch -d '2022-10-6 9:9' tmp
[root@CncLucZK user]# stat tmpFile: tmpSize: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd01h/64769d Inode: 1048581 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-10-06 09:09:00.000000000 +0800
Modify: 2022-10-06 09:09:00.000000000 +0800
Change: 2022-10-08 13:42:44.742189527 +0800Birth: -
[root@CncLucZK user]# ll;ll --time=atime;ll --time=ctime
total 4
drwxr-xr-x 2 root root 4096 Oct 6 09:09 tmp
total 4
drwxr-xr-x 2 root root 4096 Oct 6 09:09 tmp
total 4
drwxr-xr-x 2 root root 4096 Oct 8 13:42 tmp #ctime不会变为设定时间,但更新为当前服务器的时间
参考文献:
Linux 文件与目录管理
Linux cd命令:切换目录
Linux touch命令:创建文件及修改文件时间戳
上一篇:关于PCB的工艺注意事项
下一篇:c++提高编程