极客进化岛
技术自由路

【linux】循序渐进学运维-ls

前言:

ls是最简单的命令,只有连个字母,但是也是最难的命令,因为他的参数太多,让人眼花缭乱,不过很少有人用到太多的命令,毕竟ls的一些功能,其他的命令也可以实现。技术很少有标准答案,这也是我最喜欢的一点。有个词叫多维度打击,在技术里依旧通用。
ls
功能: 列出目录内容
功能:列出目录内容
常用选项:
-a 显示所有文件,包括隐藏的
-l 长格式列出信息
-i 显示文件inode号
-t 按修改时间排序
-r 按修改时间倒序排序
-h 打印易读大小单位

ls实例:

[root@zmkjedu71 ~]# ll
total 4
drwxr-xr-x 2 root root 4096 Oct 23 22:22 test
-rw-r--r-- 1 root root    0 Oct 23 22:22 test1
[root@zmkjedu71 ~]# 

-rw-r–r– 1 root root 0 Oct 23 22:22 test1

一列: 文件类型(共10个字符) + 权限 –> man find 搜索type可看
-:普通文件
d:目录
c:字符设备 –> /dev/tty 例如 USB接口、猫 等一些串行端口设备
b:块设备 –> /dev下查找 例如光驱,硬盘等 属于块设备
.:SELINUX相关
l:链接文件 –> 软连接
第二列:硬链接个数 默认从1开始 如果是目录,则默认是2(目录不做硬链接)
第三列:文件属主
第四列:文件属组
第五列:文件大小
第六-八列:创建时间/最后一次修改时间
第九列:文件名

1 按照时间顺序排序

[root@zmkjedu71 ~]# ls -ltr 
total 4
-rw-r--r-- 1 root root    0 Oct 23 22:22 test1
drwxr-xr-x 2 root root 4096 Oct 23 22:25 test
-rw-r--r-- 1 root root    0 Oct 23 22:25 2
[root@zmkjedu71 ~]# 

2 只显示目录

[root@zmkjedu71 ~]# ls -l |grep "^d"
drwxr-xr-x 2 root root 4096 Oct 23 22:25 test

3 显示数字开头的文件

[root@zmkjedu71 ~]# ls [0-9]*
01a  02a  2

4 显示隐藏文件

[root@zmkjedu71 ~]# ls -a
.    2              .cache             .ssh
..   .bash_history  .cshrc             .tcshrc
01a  .bash_logout   .history           test
02a  .bash_profile  .oracle_jre_usage  test1
03a  .bashrc        .pki               .viminfo

5 打印每个文件的索引号

[root@zmkjedu71 ~]# ls -i
2491308 anaconda-ks.cfg     2491345 公共的  2491348 图片  2491347 音乐
2490370 install.log         2491344 模板    2491346 文档  2491342 桌面
2490371 install.log.syslog  2491349 视频    2491343 下载

6 增加/(斜线) 标记目录

[root@zmkjedu71 ~]# ls -p 
anaconda-ks.cfg  install.log.syslog  模板/  图片/  下载/  桌面/
install.log      公共的/             视频/  文档/  音乐/

7 如何找到每个文件的创建者

[root@zmkjedu71 ~]# ls -lrt --author
总用量 104
-rw-r--r--. 1 root root root 10608 5月  16 2021 install.log.syslog
-rw-r--r--. 1 root root root 50101 5月  16 2021 install.log
-rw-------. 1 root root root  1726 5月  16 2021 anaconda-ks.cfg
drwxr-xr-x. 2 root root root  4096 5月  16 2021 音乐
drwxr-xr-x. 2 root root root  4096 5月  16 2021 下载
drwxr-xr-x. 2 root root root  4096 5月  16 2021 文档
drwxr-xr-x. 2 root root root  4096 5月  16 2021 图片
drwxr-xr-x. 2 root root root  4096 5月  16 2021 视频
drwxr-xr-x. 2 root root root  4096 5月  16 2021 模板
drwxr-xr-x. 2 root root root  4096 5月  16 2021 公共的
drwxr-xr-x. 3 root root root  4096 5月  16 2021 桌面

8 将目录中的文件按照修改时间进行排序,并显示关联的信息

[root@zmkjedu71 ~]# ls -ltc
总用量 104
drwxr-xr-x. 3 root root  4096 5月  16 2021 桌面
drwxr-xr-x. 2 root root  4096 5月  16 2021 公共的
drwxr-xr-x. 2 root root  4096 5月  16 2021 模板
drwxr-xr-x. 2 root root  4096 5月  16 2021 视频
drwxr-xr-x. 2 root root  4096 5月  16 2021 图片
drwxr-xr-x. 2 root root  4096 5月  16 2021 文档
drwxr-xr-x. 2 root root  4096 5月  16 2021 下载
drwxr-xr-x. 2 root root  4096 5月  16 2021 音乐
-rw-------. 1 root root  1726 5月  16 2021 anaconda-ks.cfg
-rw-r--r--. 1 root root 50101 5月  16 2021 install.log
-rw-r--r--. 1 root root 10608 5月  16 2021 install.log.syslog

9 通过逗号打印目录的内容

[root@zmkjedu71 ~]# ls -m
anaconda-ks.cfg, install.log, install.log.syslog, 公共的, 模板, 视频, 图片, 文档, 下载,
音乐, 桌面

10 以反序列输出内容

[root@zmkjedu71 ~]# ls -r
桌面  下载  图片  模板    install.log.syslog  anaconda-ks.cfg
音乐  文档  视频  公共的  install.log

结束语:

ls的面试题,我只在阿里的笔试题里见过,也无非就是上面的部分内容, 阿里的面试都是基础,只要把基础掌握扎实了,学历稍微好点,进入大公司还是很有希望的。

赞(1)
未经允许不得转载:极客进化岛 » 【linux】循序渐进学运维-ls