1.Linux 详细信息
ls命令:
参数选项:
-i # 查看inode号码
-l # 查看详细信息
-a # 显示隐藏的文件
-r # 逆序排序
-t # 按照创建文件的时间排序
-h # 人类可读 K M G 显示
按照时间的逆序显示文件:
[root@oldboy:~]# ll -rt /etc/
[root@oldboy:~]# ll -i a.txt
67161329 - rw-r--r-- 1 root root 0 Jul 10 10:03 a.txt
第一列: inode号
第二列: 文件类型
第三列: 文件权限
第四列: 硬链接个数
第五列: 属主
第六列: 属组
第七列: 大小
第八列: 文件的时间
第九列: 文件名称
2.inode 号
文件详细属性的第一列: inode号码
作用: 存放着文件具体内容的指针指向。存放了文件的详细信息。
类似藏宝图(指向宝藏的路线和宝藏的说明)
查看inode号码:
[root@oldboy:~]# ll -i
total 0
67161329 -rw-r--r-- 1 root root 0 Jul 10 10:03 a.txt
查看磁盘总inode号:
[root@oldboy:~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
devtmpfs 246345 481 245864 1% /dev
tmpfs 250391 1 250390 1% /dev/shm
tmpfs 250391 709 249682 1% /run
tmpfs 250391 17 250374 1% /sys/fs/cgroup
/dev/mapper/klas-root 24619008 124001 24495007 1% /
查看磁盘block:
[root@oldboy:~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 963M 0 963M 0% /dev
tmpfs 979M 0 979M 0% /dev/shm
tmpfs 979M 57M 922M 6% /run
tmpfs 979M 0 979M 0% /sys/fs/cgroup
/dev/mapper/klas-root 47G 3.9G 44G 9% /
导致无法存储数据的原因有两个:
一是inode号满
二是磁盘空间满
3.文件类型
文件类型: 文件类型决定了文件是啥类型。
windows: .txt .exe .pfd .mp3 .mp4 .avi .jpg.....
linux: 文件类型是方便给我们看的。Linux一切皆文件
文件类型:
- 普通文件,命令文件,数据文件 图片 视频 rpm zip
d 目录
l 软链接 类似windows的快捷方式
c 字节文件
b 快设备 硬件
s 接口文件 系统用得
p 管道文件 系统使用
文件类型软链接:
[root@oldboy:~]# ll /etc/rc.local
lrwxrwxrwx 1 root root 13 Apr 20 2022 /etc/rc.local -> rc.d/rc.local
字节文件:
[root@oldboy:~]# ll /dev/urandom # 一直不断的往外吐乱码 了解
crw-rw-rw- 1 root root 1, 9 Jul 5 14:08 /dev/urandom
字节文件
作用: 不想看到的结果定向到空/dev/null,使用$?判断执行是否成功
[root@oldboy:~]# ll /dev/null # 空 类似宇宙的黑洞
crw-rw-rw- 1 root root 1, 3 Jul 5 14:08 /dev/null
案例: /dev/null
[root@oldboy:~]# ping -c1 -W1 www.baidu.com &> /dev/null
[root@oldboy:~]# echo $?
0
[root@oldboy:~]# ping -c1 -W1 www.bssssssssssaidu.com &> /dev/null
[root@oldboy:~]# echo $?
2
[root@oldboy:~]# llllll &>/dev/null
[root@oldboy:~]# echo $?
127
$? 上一条命令的执行结果,0为成功,非0失败。
字节设备: /dev/zero
作用: 经常用来做测试,生成大文件使用。
[root@oldboy:~]# ll /dev/zero
crw-rw-rw- 1 root root 1, 5 Jul 5 14:08 /dev/zero
案例: 在linux中生成一个1g的文件怎么办?
[root@oldboy:~]# dd if=/dev/zero of=1g.txt bs=1M count=1000
dd: 命令
if: input file 要读取/etc/zero
of: output file 输出到当前的1g.txt
bs: 1次输出多大
count: 总共输出的次数
[root@oldboy:~]# ll -h
total 1000M
-rw-r--r-- 1 root root 1000M Jul 10 10:52 1g.txt
快设备:
[root@oldboy:~]# ll /dev/sr0
brw-rw---- 1 root cdrom 11, 0 Jul 5 14:08 /dev/sr0
[root@oldboy:~]# ll /dev/sda
brw-rw---- 1 root disk 8, 0 Jul 5 14:08 /dev/sda
4.文件权限
rw-r--r-- # 九位权限位
前三位: 表示属主的权限 笔记本是我们自己的 rw-
中三位: 表示属组的权限 笔记本在小组的权限 r--
后三位: 其他用户陌生人的权限 隔壁网安 r--
r read # 读
w write # 写
x excute# 执行
- # 空的
对于文件谁是主人,文件属于哪个组。
root root 1048576000 Jul 10 10:55 1g.txt # 第一个root是主人 第二个root是小组名称
小明主人 系统自动创建叫小明的组
5.查找文件 find
作用: 按照文件类型查找文件
语法格式:
find 路径 - d c b
find 路径
/
/opt
/mtp
/etc
./ 当前目录查找
环境准备:
[root@oldboy:~]# mkdir oldboy
[root@oldboy:~]# cd oldboy/
[root@oldboy:oldboy]# ll
[root@oldboy:oldboy]# touch {1..3}.txt
[root@oldboy:oldboy]# mkdir oldboy{1..3}
[root@oldboy:oldboy]# touch {4..6}.TXT
total 0
-rw-r--r-- 1 root root 0 Jul 10 11:25 1.txt
-rw-r--r-- 1 root root 0 Jul 10 11:25 2.txt
-rw-r--r-- 1 root root 0 Jul 10 11:25 3.txt
-rw-r--r-- 1 root root 0 Jul 10 11:31 4.TXT
-rw-r--r-- 1 root root 0 Jul 10 11:31 5.TXT
-rw-r--r-- 1 root root 0 Jul 10 11:31 6.TXT
drwxr-xr-x 2 root root 6 Jul 10 11:26 oldboy1
drwxr-xr-x 2 root root 6 Jul 10 11:26 oldboy2
drwxr-xr-x 2 root root 6 Jul 10 11:26 oldboy3
1.find按照文件类型查找
案例1.find查找当前目录下所有的普通文件
[root@oldboy:oldboy]# find ./ -type f
./1.txt
./2.txt
./3.txt
案例2.查找当前所有的目录
[root@oldboy:oldboy]# find ./ -type d
./
./oldboy1
./oldboy2
./oldboy3
2.find按照名称查找
案例1.查找名字是2.txt的文件
[root@oldboy:oldboy]# find ./ -name "2.txt"
./2.txt
案例2.查找名称是oldboy1
[root@oldboy:oldboy]# find ./ -name "oldboy1"
./oldboy1
案例3.按照名称模糊查找*
[root@oldboy:oldboy]# find ./ -name "*.txt"
./1.txt
./2.txt
./3.txt
案例4.忽略大小写
[root@oldboy:oldboy]# find ./ -iname "*.txt"
./1.txt
./2.txt
./3.txt
./4.TXT
./5.TXT
./6.TXT
案例5.查找名字是1.txt的普通文件
[root@oldboy:oldboy]# find ./ -type f -name "1.txt"
./1.txt
-and # 并且关系
-or # 或者关系
查找目录是1.txt的
[root@oldboy:oldboy]# ll /tmp/
total 0
drwxr-xr-x 2 root root 40 Jul 10 11:33 1.txt
drwx------ 3 root root 60 Jul 5 14:08 systemd-private-93b8153508bb49898a1fd7e2e79132ba-chronyd.service-V7IxOl
drwx------ 3 root root 60 Jul 5 14:08 systemd-private-93b8153508bb49898a1fd7e2e79132ba-systemd-logind.service-fjh1Oi
[root@oldboy:oldboy]# find /tmp/ -type f -name "1.txt"
[root@oldboy:oldboy]# find /tmp/ -type d -name "1.txt"
/tmp/1.txt
-or 或者 查找/tmp目录下所有的普通文件 或者 名字叫1.txt的
[root@oldboy:oldboy]# find /tmp -type f -or -name "1.txt"
/tmp/1.txt
3.find按照inode号查找
[root@oldboy:oldboy]# find ./ -inum 68979295
./4.TXT
通过查找inode号码,删除文件 # 遇到乱码的文件无法删除情况下使用
[root@oldboy:oldboy]# find ./ -inum 68979300|xargs rm
4.按照深度等级查找
[root@oldboy:oldboy]# find ./ -maxdepth 1 -type f
./1.txt
./2.txt
./3.txt
./4.TXT
./5.TXT
./6.TXT
6.按照文件的大小查找
环境准备:
[root@oldboy:oldboy]# cat /etc/services /etc/services > 1.txt
[root@oldboy:oldboy]# ll -h
total 2.0M
-rw-r--r-- 1 root root 1.4M Jul 10 11:51 1.txt
-rw-r--r-- 1 root root 677K Jul 10 11:51 services
[root@oldboy:oldboy]# cat 1.txt 1.txt 1.txt 1.txt 1.txt 1.txt 1.txt 1.txt > 2.txt
[root@oldboy:oldboy]# ll -h
total 13M
-rw-r--r-- 1 root root 1.4M Jul 10 11:51 1.txt
-rw-r--r-- 1 root root 11M Jul 10 11:52 2.txt
-rw-r--r-- 1 root root 677K Jul 10 11:51 services
[root@oldboy:oldboy]# dd if=/dev/zero of=3.txt bs=1M count=1200
1200+0 records in
1200+0 records out
1258291200 bytes (1.3 GB, 1.2 GiB) copied, 35.4469 s, 35.5 MB/s
案例1.查找文件小10M的文件
[root@oldboy:oldboy]# find ./ -size -10M
./
./services
./1.txt
案例2.查找文件等于10的文件
[root@oldboy:oldboy]# find ./ -size 10M
案例3.查找文件大于10M的文件
[root@oldboy:oldboy]# find ./ -size +10M
./2.txt
./3.txt
案例4.查找文件大于10M并且小于1000M的文件
[root@oldboy:oldboy]# find ./ -size +10M -size -1000M
./2.txt
案例5.查找文件大于1G 或者 文件名称是1.txt
[root@oldboy:oldboy]# find ./ -size +1G -or -name "1.txt"
查看目录的总大小:
[root@oldboy:~]# du -sh oldboy
1.2G oldboy
find按照文件大小查找:
find ./ -size 10M 等于10M
+10M 大于10M
-10M 小于10M
单位:
1字节=8bit
1M=1024字节
1G=1024M
1T=1024G
知识点小结:
文件的详细属性
1.ls参数
-a
-l
-r
-t
-i
2.inode号码
3.文件类型
-
d
c
b
/dev/zero dd命令生成文件
/dev/null 空
4.文件权限 9位 三位一组 rwx
5.find查找
find按照文件类型查找
find ./ -type f
find ./ -type d
find按照文件名称查找
find ./ -name "1.txt"
find ./ -name "*.txt"
find ./ -iname "*.txt"
find按照深度等级查找
find ./ -maxdepth 1 -type f
find按照inum进行查找
find ./ -inum 2342342 # 删除无法正常删除的文件
find按照大小进行查找
find ./ -size 10M
+10M
-10M
或者-or
并且-and
find ./ -type f -name "1.txt" # 默认为并且
find ./ -type -f -or -size 10M
6.find 结果交给其他命令
1.第一步查找文件或者目录
2.查找文件后做的动作
动作: 复制,移动,删除,查看
第一种方法:
注意: 在xargs后面别名失效
[root@oldboy:oldboy]# find ./ -name "1.txt"|xargs ll
xargs: ll: No such file or directory
案例.find的结果交给ls命令
[root@oldboy:oldboy]# find ./ -name "1.txt"
./1.txt
[root@oldboy:oldboy]# find ./ -name "1.txt"|xargs ls -l
-rw-r--r-- 1 root root 1384504 Jul 10 11:51 ./1.txt
[root@oldboy:oldboy]# find ./ -name "1.txt"|xargs cat
案例2.find结果交给rm命令
[root@oldboy:oldboy]# find ./ -name "1.txt"|xargs rm
案例3.find结果交给cp命令
[root@oldboy:oldboy]# find ./ -name "1.txt"|xargs -i cp {} /opt/
[root@oldboy:oldboy]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Jul 10 15:31 1.txt
[root@oldboy:oldboy]# find ./ -name "1.txt"|xargs -i cp {} /opt/1.txt.bak
[root@oldboy:oldboy]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Jul 10 15:31 1.txt
-rw-r--r-- 1 root root 0 Jul 10 15:31 1.txt.bak
案例4.find结果交给mv命令
[root@oldboy:oldboy]# find ./ -name "1.txt"|xargs -i mv {} /opt/
[root@oldboy:oldboy]# ll
total 1240304
-rw-r--r-- 1 root root 11076032 Jul 10 11:52 2.txt
-rw-r--r-- 1 root root 1258291200 Jul 10 11:54 3.txt
-rw-r--r-- 1 root root 926 Jul 10 12:11 count.txt
-rw-r--r-- 1 root root 692252 Jul 10 11:51 services
[root@oldboy:oldboy]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Jul 10 15:29 1.txt
第二种方法:exec
案例1.find的结果交给ls命令
[root@oldboy:oldboy]# find ./ -name "1.txt" -exec ls -l {} \;
-rw-r--r-- 1 root root 0 Jul 10 15:48 ./1.txt
[root@oldboy:oldboy]#
[root@oldboy:oldboy]# ls -l 1.txt 2.txt
-rw-r--r-- 1 root root 0 Jul 10 15:48 1.txt
-rw-r--r-- 1 root root 11076032 Jul 10 11:52 2.txt
[root@oldboy:oldboy]# find ./ -name "*.txt" -exec ls -l {} \;
-rw-r--r-- 1 root root 11076032 Jul 10 11:52 ./2.txt
-rw-r--r-- 1 root root 1258291200 Jul 10 11:54 ./3.txt
-rw-r--r-- 1 root root 926 Jul 10 12:11 ./count.txt
-rw-r--r-- 1 root root 0 Jul 10 15:48 ./1.txt
案例2.find结果交给rm命令
[root@oldboy:oldboy]# find ./ -name "1.txt" -exec rm {} \;
案例3.find结果交给cp命令
[root@oldboy:test]# find ./ -name "1.txt" -exec cp {} /opt/ \;
[root@oldboy:test]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Jul 10 15:53 1.txt
案例4.find结果交给mv命令
[root@oldboy:test]# find ./ -name "1.txt" -exec mv {} /tmp/a.txt \;
; # shell中命令的分隔符
[root@oldboy:~]# mkdir test;cd test;touch a.txt;ll a.txt
方法3 `` 反引号 $() 先执行反引号和$()中的命令,然后交给其他的命令,相当于(加)减乘除 先算括号
[root@oldboy:test]# awk 'BEGIN{print 10+2*2}'
14
[root@oldboy:test]# awk 'BEGIN{print (10+2)*2}'
24
案例1.find的结果交给ls命令
[root@oldboy:test]# ll `find ./ -name "a.txt"`
-rw-r--r-- 1 root root 0 Jul 10 15:57 ./a.txt
案例2.find结果交给rm命令
[root@oldboy:test]# rm -f `find ./ -name "a.txt"`
案例3.find结果交给cp命令
[root@oldboy:test]# cp `find ./ -name "a.txt"` /opt/
[root@oldboy:test]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Jul 10 15:53 1.txt
-rw-r--r-- 1 root root 0 Jul 10 16:00 a.txt
案例4.find结果交给mv命令
[root@oldboy:test]# mv `find ./ -name "a.txt"` /tmp/
或者$()
[root@oldboy:test]# ll $(find /tmp/ -name "1.txt")
-rw-r--r-- 1 root root 0 Jul 10 15:52 /tmp/1.txt
重点小结:
1.inode号和block了解
2.磁盘无法写入检查 inode block df -i df -h
3.文件类型 - d l c b
4.字节设备/dev/zero /dev/null (dd命令笔记)
5.文件权限 了解
6.find 查找文件 重点
7.find的执行结果交给其他命令调用
find ./ -name "1.txt"|xargs ls -l
find ./ -name "1.txt"|xargs -i cp {} /opt/
find ./ -name "1.txt" -exec ls -l {} \;
find ./ -name "1.txt" -exec cp {} /opt/ \;
ll `find ./ -name "1.txt"`
cp `find ./ -name "1.txt"` /opt/
软硬链接
压缩 解压缩
tar zip