Linux 学习笔记 十四

十四单元笔记

- 设备的识别

[root@foundation12 ~]# df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       124G   12G  113G  10% /
/dev/sda1       207M  113M   94M  55% /boot
/dev/sdb1        33G   13G   20G  39% /run/media/kiosk/0A6D-5E49
/dev/sda        ###系统中的第一块串口硬盘
/dev/hda        ###系统中第一快并口硬盘
/dev/hdb        ###系统中第二快并口硬盘
/dev/cdrom      ###系统光驱
/dev/mapper/xxx ###虚拟设备

- 设备的使用

1)挂载

mount 设备挂载点  ##挂载
mount /dev/sda1 /mnt    ##把系统中的第一块硬盘的第一个分区挂载到/mnt下

【例】挂载u盘设备到/mnt
先用df找到挂载的设备:u盘/dev/sdb1,再执行

mount  /dev/sdb1 /mnt/

2)挂载信息
blkid ##显示所有可用设备的id信息

df  -h(2的n次方) -H(10的n次方)            ##查看挂载信息,-h能精确一点
du  -h(显示单位)    -s(只统计目录本身) file|dir    ##统计文件大小
[root@foundation12 westos]# du -h
4.0K    ./.cache/abrt
4.0K    ./.cache
[root@foundation12 westos]# du -H
4   ./.cache/abrt
4   ./.cache

3)卸载
umount 设备|挂载点 ###卸载设备
当设备卸载出现以下问题时

[root@foundation0 ~]# umount /dev/sdb1    ##卸载sdb1设备
umount: /mnt: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))    
##设备正被占用,正被挂载在/mnt下

找到占用该设备的进程,并将其关闭,再执行umount 进程号卸载设备
fuser 设备|挂载点

fuser -vm /dev/sdb1
lsof    设备|挂载点

来找到占用设备的进程

  • 如何在系统中查找文件————find 命令

find 路径 条件 条件值

find /mnt -name file
find /mnt -name "file*"
find /etc -name "*.conf"
条件1 -user       ##用户
[root@foundation12 mnt]# find /mnt/ -user kiosk ##查找/mnt下用户为kiosk的文件
    -group      ##组
    -o  条件2  ##或者
    -type       ##类型
    -mmin   +|-数字   ##时间
    -a    ##并且

用户及文件情况

[root@foundation12 mnt]# ll
total 0
-rw-r--r-- 1 westos root   0 Oct 13 09:57 file1
-rw-r--r-- 1 kiosk  root   0 Oct 13 09:57 file2
-rw-r--r-- 1 westos kiosk  0 Oct 13 09:57 file3
-rw-r--r-- 1 kiosk  kiosk  0 Oct 13 09:57 file4
-rw-r--r-- 1 root   root   0 Oct 13 09:57 file5
drwxr-xr-x 2 root   root  41 Oct 12 10:48 westos

按用户查找文件操作

[root@foundation12 mnt]# find /mnt/ -group kiosk -a -user westos
/mnt/file3  ##查找kiosk组并且是westos用户的file3文件
[root@foundation12 mnt]# find /mnt/ -not -group kiosk -a -not -user westos      ##查找不是kiosk组也不是westos用户的文件
/mnt/
/mnt/westos
/mnt/westos/file
/mnt/westos/file1
/mnt/westos/file2
/mnt/file2
/mnt/file5

按文件属性查找文件
dd if=/dev/zero of=/mnt/file bs=1024 count=10

if(inputfile) zero代表空字节,即没有设备
dd      ##数据截取
if      ##数据模板(输入)
of      ##数据存放文件
bs      ##数据块大小(1024个字节)
count       ##数据块个数
[root@foundation12 mnt]# dd if=/dev/zero of=/mnt/file1 bs=1024 count=10     ##新建一个字节数为1024,大小是10,设备为空的文件file1
10+0 records in
10+0 records out
10240 bytes (10 kB) copied, 0.000143099 s, 71.6 MB/s
[root@foundation12 mnt]# dd if=/dev/zero of=/mnt/file1 bs=1024 count=20     ##新建一个字节数为1024,大小是20,设备为空的文件file1
20+0 records in
20+0 records out
20480 bytes (20 kB) copied, 0.000209821 s, 97.6 MB/s

–size 数字|-数字|+数字 ###查找大小符合条件的文件
-perm 444 ###文件全权必须为rrr
-perm -444 ###文件每一位都要含有r权限(-并且,数字越大文件越少)
-perm /444 ###文件任意一位含有r权限(/或者,数字越大文件越多)

[root@foundation12 mnt]# find /mnt/ -size 20k   ##大小等于20k
/mnt/file1
[root@foundation12 mnt]# find /mnt/ -size -20k      ##小于20k
/mnt/
/mnt/westos
/mnt/westos/file
[root@foundation12 mnt]# find /mnt/ -size +20k      ##大于20k
[root@foundation12 mnt]# find /mnt/ -size -20k -a -type f   ##查找/mnt下大小小于20k并且(-a)类型为file(-f)的文件
/mnt/westos/file
/mnt/westos/file1
/mnt/westos/file2
[root@foundation12 mnt]# find /mnt/ -size -20k -a -type d       ##查找/mnt下大小小于20k并且(-a)类型为directory(-d)的文件
/mnt/
/mnt/westos
[root@foundation12 mnt]# chmod 000 *        ##清空权限
[root@foundation12 mnt]# ll
---------- 1 westos root  20480 Oct 13 10:11 file1
---------- 1 kiosk  root      0 Oct 13 09:57 file2
[root@foundation12 mnt]# find /mnt -perm(权限permissions) 444
[root@foundation12 mnt]# find /mnt -perm -444       ##每一位都有r权限的文件
[root@foundation12 mnt]# find /mnt -perm /444       ##任意一位都有r权限的文件
[root@foundation12 mnt]# find /mnt -perm /777       ##任意一位都有x权限的文件

-maxdepth 数字 ###最深目录层,根目录
-mindepth 数字 ###最小目录层
-exec(执行) 命令 {} \; ###对查找出的结果做相应处理

[root@foundation12 mnt]# find /etc/ -name passwd    ##查找名为passwd的文件
/etc/passwd
/etc/pam.d/passwd
[root@foundation12 mnt]# find /etc/ -name passwd -maxdepth 1        ##查找根目录下名为passwd的文件
find: warning: you have specified the -maxdepth option after a non-option argument -name, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.
##我们可以看到会报错,建议修改为下面的格式
/etc/passwd
[root@foundation12 mnt]# find /etc/ -maxdepth 1 -name passwd
/etc/passwd 
[root@foundation12 mnt]# find /etc/ -maxdepth 2 -name passwd
/etc/passwd     ##查找有两层目录的passwd文件
/etc/pam.d/passwd
[root@foundation12 mnt]# find /etc/ -mindepth 2 -name passwd
/etc/pam.d/passwd       ##查找至少有两层目录名为passwd的文件
[root@foundation12 mnt]# find /etc/ -maxdepth 2 -mindepth 2 -name passwd        ##查找最深目录层为2,最小目录层为2的文件
/etc/pam.d/passwd
[root@foundation12 mnt]# find /etc/ -maxdepth 2 -mindepth 2 -name passwd -exec cp {} /mnt \;        ##
[root@foundation12 mnt]# find /mnt -perm -444 -exec rm -fr {} \;
  • locate
    -find files by name根据文件名查找文件
    使用: locate [OPTION]... PATTERN...
locate +条件      ##在数据库中查找符合条件的文件
locate westos       ##在数据库里查找名为westos的文件
updatedb        ##更新数据库

【在对westos进行建立/删除的操作,如果没有更新数据库,还是找不到/找到westos】

$locate /etc/sh        ##搜索etc目录下,所有以sh开头的文件
$locate ~/m       ##搜索用户主目录下,所有以m开头的文件
$locate -i ~/m        ##搜索用户主目录下,所有以m开头的文件,且忽略大小写
  • 链接

硬链接——是文件的副本,为了节省设备节点号
ln 文件 链接

touch file  ##新建一个文件file
ln /mnt/file /mnt/westos    ##将file和westos建立硬链接,就是把file里的内容重命名到westos里

软链接——是文件的快捷方式,为了节省设备存储快
多个节点对应一个文件内容
ln -s 文件 链接

ln -s /mnt/file1 /mnt/westos1   ##将file1和westos1建立软链接,就是建立一个file1的快捷方式,并命名为westos1

ps:粘贴复制会复制文件的属性,但是软硬链接只是复制文件的内容

ls -i       ##查看一个文件节点号
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值