1.查看文件
- 命令: ls(选项较多,讲几个常用的)
作用:list的简写,用于显示指定路径下所有文件的文件信息
命令格式:
[root@redhat ~]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
常用选项(参数):
-a 显示所有文件(包括隐藏文件及目录)
-d 显示目录本身
-l 以长列表形式显示内容
-h 以人类阅读习惯显示内容
使用命令:
#显示根目录root下所有的内容
[root@redhat ~]# ls /
afs bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
#注:
/ 是Linux中的根目录所有的文件或者目录都是挂载这个目录之下的(一定要记!)
afs 通常指的是android的文件系统
bin 存放二进制文件的地方
boot 表示的是系统启动的时候所需要的文件
dev 用来存放硬件设备信息的文件
etc 系统中所有配置文件所在的目录
home 普通用户的家目录
lib 库文件
lib64 也是库文件所在的目录
media 媒体文件,用来挂载
mnt 挂载目录
opt 第三方软件所在的目录
proc 进程所在的目录
root root用户的家目录
run 程序运行时相关文件的所在目录
sbin 超级用户可执行命令的所在目录
srv 网络服务的数据
sys 用户存储虚拟文件的目录
tmp 运行程序时所产生的临时文件(存放的都是一些不重要的文件)
usr 软件资源所在的目录
var 用来存放一些变化的数据 例如:日志
1.1 -a
作用:显示所有文件(包括隐藏文件及目录)
#显示当前目录下面所有的文件信息
[root@redhat ~]# ls
anaconda-ks.cfg Desktop Documents Downloads Music Pictures Public Templates Videos
[root@redhat ~]# ls -a
. .bash_history .bashrc .cshrc Downloads Music .ssh Videos
.. .bash_logout .cache Desktop .lesshst Pictures .tcshrc .Xauthority
anaconda-ks.cfg .bash_profile .config Documents .local Public Templates
[root@redhat ~]# ls -A
anaconda-ks.cfg .bash_profile .config Documents .local Public Templates
.bash_history .bashrc .cshrc Downloads Music .ssh Videos
.bash_logout .cache Desktop .lesshst Pictures .tcshrc .Xauthority
# 可以看看这三者的区别
#注:. 表示的时当前目录
.. 表示的上一级目录
.bash_history 表示隐藏文件或目录
切换路径,看看不同路径显示的文件信息
# 切换到 home 下普通用户目录,即/home/wuxinchun,查看文件信息
#方法一:先切换路径,再查看
[root@redhat ~]# cd /home/wuxinchun
[root@redhat wuxinchun]# ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .mozilla
#方法二:直接指定查看的路径
[root@redhat ~]# ls -a /home/wuxinchun
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .mozilla
总结:
1、在Linux中,以.和…开头的会显示隐藏文件或目录
2、任何一个目录,当执行了ls -a 命令之后都可以看到.和…
1.2 -d
作用:显示目录本身
显示需要查询的目录
#查询目录 /usr/bin
[root@redhat ~]# ls -d /usr/bin
/usr/bin
哎~你是不是觉得这个选项没什么用啊?
别急!先来看看ls -d 的实际用处:(后面讲,别偷懒,快记吧!)
-
查看目录属性:配合 -l 选项(如 ls -ld 目录名 ),快速获取目录权限、所有者、大小、修改时间等属性信息,不被目录内文件干扰。
-
脚本编写:在脚本中用于判断目录是否存在,或获取目录信息,方便后续逻辑处理 。
-
筛选目录:结合通配符(如 ls -d dir* )找出特定命名规则的目录;也可将其输出通过管道传递给其他命令(如 grep )进一步筛选 。
1.3 -l
作用:以长列表形式显示内容
来看看加 -l 和不加 -l 的区别
[root@redhat ~]# ls /usr
bin games include lib lib64 libexec local sbin share src tmp
[root@redhat ~]# ls -l /usr
total 240
dr-xr-xr-x. 2 root root 49152 Dec 27 15:41 bin
drwxr-xr-x. 2 root root 6 Aug 10 2021 games
drwxr-xr-x. 3 root root 23 Dec 27 15:39 include
dr-xr-xr-x. 38 root root 4096 Dec 27 15:43 lib
dr-xr-xr-x. 113 root root 73728 Dec 27 15:41 lib64
drwxr-xr-x. 45 root root 12288 Dec 27 15:41 libexec
drwxr-xr-x. 12 root root 131 Dec 27 15:37 local
dr-xr-xr-x. 2 root root 16384 Dec 27 15:41 sbin
drwxr-xr-x. 200 root root 8192 Dec 27 15:41 share
drwxr-xr-x. 4 root root 34 Dec 27 15:37 src
lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp
长列表内容说明:(后续会详细讲解)
d r-xr-xr-x. 2 root root 49152 Dec 21 21:04 bin
1 2 3 4 5 6 7 8
1、文件的类型
2、文件的操作权限
3、硬连接次数
4、文件所属者(创建这个文件的人)
5、文件所属组的名称
6、文件的大小,单位是字节
7、文件最后一次修改的时间
8、文件的名称
与Windows的相关文件属性类似
1.4 -h
作用:以人类阅读习惯显示内容
一般会和 -l 连用
#方法一:
[root@redhat ~]# ls -l -h /usr
total 240K
dr-xr-xr-x. 2 root root 48K Dec 27 15:41 bin
drwxr-xr-x. 2 root root 6 Aug 10 2021 games
drwxr-xr-x. 3 root root 23 Dec 27 15:39 include
dr-xr-xr-x. 38 root root 4.0K Dec 27 15:43 lib
dr-xr-xr-x. 113 root root 72K Dec 27 15:41 lib64
drwxr-xr-x. 45 root root 12K Dec 27 15:41 libexec
drwxr-xr-x. 12 root root 131 Dec 27 15:37 local
dr-xr-xr-x. 2 root root 16K Dec 27 15:41 sbin
drwxr-xr-x. 200 root root 8.0K Dec 27 15:41 share
drwxr-xr-x. 4 root root 34 Dec 27 15:37 src
lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp
#方法二:
[root@redhat ~]# ls -lh /usr
total 240K
dr-xr-xr-x. 2 root root 48K Dec 27 15:41 bin
drwxr-xr-x. 2 root root 6 Aug 10 2021 games
drwxr-xr-x. 3 root root 23 Dec 27 15:39 include
dr-xr-xr-x. 38 root root 4.0K Dec 27 15:43 lib
dr-xr-xr-x. 113 root root 72K Dec 27 15:41 lib64
drwxr-xr-x. 45 root root 12K Dec 27 15:41 libexec
drwxr-xr-x. 12 root root 131 Dec 27 15:37 local
dr-xr-xr-x. 2 root root 16K Dec 27 15:41 sbin
drwxr-xr-x. 200 root root 8.0K Dec 27 15:41 share
drwxr-xr-x. 4 root root 34 Dec 27 15:37 src
lrwxrwxrwx. 1 root root 10 Aug 10 2021 tmp -> ../var/tmp
#注:可以在命令的后面跟上多个选项,选项之间用空格隔开-l -h ;
也可以将多个选项进行组合 -lh
可以看到加 -h 和不加 -h 的区别
*实用小知识:
常见 ls 简写命令
常见 ls 命令 | 简写 |
---|---|
ls -l | ll |
ls -A | la |
ls -lh | lh |
文件类型
符号 | 文件类型 |
---|---|
- | 普通文件 |
d | 目录文件 |
l | 链接文件(分为软链接和硬链接) |
c | 字符设备文件 |
b | 块设备文件 |
p | 管道文件(也叫FIFO文件) |
s | 套接字文件 |
- | 普通文件 |
举两个例子:
2.路径切换
- 命令: cd
作用:切换用户的工作环境
命令格式:
[root@redhat ~]# cd --help
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
切换到路径/var/log,并返回
#切换路径
[root@redhat ~]# cd /var/log
#注:路径已改变
[root@redhat log]# pwd
/var/log
#注:可以看到~变为log,说明路径变了,也可以通过命令pwd验证
#返回上级目录
#方法一:直接返回
[root@redhat log]# cd .. #切换到上一级
[root@redhat var]# pwd
/var
#方法二:切换路径
[root@redhat log]# cd /var
[root@redhat var]# pwd
/var
连续切换
#先切换一个多层级路径
[root@redhat var]# cd /usr/local/etc
[root@redhat etc]# pwd
/usr/local/etc
#输入cd ../../
[root@redhat etc]# cd ../../ #切换到上一级的上一级
[root@redhat usr]# pwd
/usr
#或cd ../..
[root@redhat etc]# cd ../.. #也可以切换到上一级的上一级
[root@redhat usr]# pwd
/usr
#注:cd ../../ 和 cd ../.. 等价
返回之前操作的目录
[root@redhat usr]# cd -
/usr/local/etc
[root@redhat etc]# cd -
/usr
[root@redhat usr]# cd -
/usr/local/etc
[root@redhat etc]# cd -
/usr
#注:cd - 表示在最近的两个目录之间来回切换
直接切换到家目录
#管理员root用户
#方法一:cd
[root@redhat usr]# cd
[root@redhat ~]# pwd
/root
#方法二:cd ~
[root@redhat usr]# cd ~
[root@redhat ~]# pwd
/root
#普通用户
#先切换到普通用户
[root@redhat ~]# su -l wuxinchun
[wuxinchun@redhat ~]$ pwd
/home/wuxinchun #这是我现在的路径,也是家目录路径
#先看看有哪些目录
#可能出现状况
[wuxinchun@redhat ~]$ ls
[wuxinchun@redhat ~]$ #没有显示文件,都是隐藏文件,需要-a,后续我们会学习创建目录
#正确操作:
[wuxinchun@redhat ~]$ ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .cache .mozilla
#切换路径,蓝色的才是目录(也可以称“文件夹”)
[wuxinchun@redhat ~]$ cd /home/wuxinchun/.cache
[wuxinchun@redhat .cache]$ pwd
/home/wuxinchun/.cache
#切换回到家目录
#方法一:cd
[wuxinchun@redhat .cache]$ cd
[wuxinchun@redhat ~]$ pwd
/home/wuxinchun
#方法二:cd ~
[wuxinchun@redhat .cache]$ cd ~
[wuxinchun@redhat ~]$ pwd
/home/wuxinchun
# 注:root超级用户 和 普通用户都可以通过 cd 和 cd ~ 切换回家目录