Linux(5)文件管理

文件管理

查找目录内容

  • 命令:find

  • 语法:find [查找路径] [options] [参数]

  • 选项:

    [!NOTE]

    -name:按照文件名

    -size:按文件大小

    -type:按文件类型

    按照时间查找

    • atime:文件访问时间
    • mtime:文件内容修改时间
    • ctime:文件状态更改时间
    #查找系统中最近七天内被访问过的文件
    find    /    -type    f    -atime    -7  
    
    #查找系统中访问时间超过七天以上的文件
    find    /    -type    f    -atime    +7   
    
    #查找系统中最近七天内被修改过的文件
    find    /    -type    f    -mtime    +7   
    

    按照文件归属查找

    • -user:
    • -group
    • nouser
    • nogroup
    查找系统中用户所有者为u1的所有文件
    find -type f -user u1
    

    按照权限查找

    [!IMPORTANT]

    -perm mode:精确查找,完全符合的文件才被列出

    -perm -mode:部分查找,给定的部分必须满足,其他位置无要求

    -perm /mode:模糊查找,满足其中一个即可

    [root@localhost test]# find /root/test/ -perm  640 -type f
    /root/test/1.txt
    
    [root@localhost test]# ll
    总用量 0
    -rw-r--r--. 1 root root 0 129 14:24 1.txt
    
    ==========================================================
    [root@localhost test]# find /root/test/ -perm  -040 -type f
    /root/test/1.txt
    /root/test/2.txt
    
    [root@localhost test]# ll
    总用量 0
    -rw-r-----. 1 root root 0 129 14:24 1.txt
    -rw-r---w-. 1 root root 0 129 14:24 2.txt
    --w--w--w-. 1 root root 0 129 14:24 3.txt
    
    ==========================================================
    [root@localhost test]# find /root/test/ -perm  /060 -type f
    /root/test/1.txt
    /root/test/2.txt
    /root/test/3.txt
    
    [root@localhost test]# ll
    总用量 0
    -rw-r-----. 1 root root 0 129 14:24 1.txt
    -rw-r---w-. 1 root root 0 129 14:24 2.txt
    --w--w--w-. 1 root root 0 129 14:24 3.txt
    

    对命令结果的二次处理

    • -exec {} \ ;

      {}是一个占位符,代表find命令找到的当前文件或目录路径,\;用于标记-exec选项中命令的结束。

      找到系统中所有log文件并删除
      find /  -type f -name "*.log" -exec rm -f {} \;
      
    • -ok {} \ ;

查找文件内容

命令:grep

语法:grep [options] [字符串] [参数]

选项:

[!TIP]

-n:显示行号

-i:忽略大小写

-q:静默模式

-o:只显示查找的字符串

-v:反向匹配

^字符串:匹配以字符串开头的行

字符串$:匹配以字符串结尾的行

^$:匹配空行

-E 例:ss -anptl | grep -E ":80|:10050|:10051"

example

grep练习:
	1、查找/etc/passwd文件中包含shutdown的行,并显示行号
	grep -n "shutdown" /etc/passwd
	2、查找/etc/passwd文件中的所有的nfs字符串
	grep -i "nfs" /etc/passwd
	3、查找/etc/man_db.conf文件中的#号
	grep -i "#" /etc/man_db.conf
	4、查找/etc/shadow文件中的:号,并显示行号
	grep -n ":" /etc/shadow
	5、匹配/etc/yum.conf文件中空行以外的行
	grep -v "^$" /etc/yum.conf
	6、查找/etc/passwd中以root开头的行,并显示行号
	grep -n "^root" /etc/passwd
	7、查找/etc/passwd中以nologin结尾的行,并显示行号
	grep -n "nologin$" /etc/passwd
	8、查找/etc/man_db.conf文件中的tree,并显示行号
	grep -n  "tree" /etc/man_db.conf

文件压缩

1、zip

  • 命令:zip
  • 选项:-r
  • 语法:zip 压缩后的文件名 源文件
  • 后缀:.zip

2、gzip

  • 命令:gzip
  • 语法:gzip 文件
  • 后缀:.gz
  • 解压缩:-d

3.tar

  • 命令:tar

  • 语法:tar [options] 压缩后的文件名 源文件

  • 选项:

    [!IMPORTANT]

    -c 创建打包文件

    -f 调用归档格式,必须放在选项最后使用

    -z 调用gzip的压缩格式

    -j 调用bzip2的压缩格式

    -t 查看压缩内容

    -x 解压缩

    -C 指定解压路径

对于压缩目录

zip -r a/ 不加-r 选项,压缩包里无内容

gzip -r a/ 会把目录里的文件压缩为.gz格式

[root@bogon test]# gzip -r a/
[root@bogon test]# ll
总用量 0
drwxr-xr-x. 2 root root 54 1211 14:13 a
drwxr-xr-x. 2 root root  6 1211 14:10 b
[root@bogon test]# cd a
[root@bogon a]# ll
总用量 12
-rw-r--r--. 1 root root 33 1211 14:11 1.txt.gz
-rw-r--r--. 1 root root 33 1211 14:11 2.txt.gz
-rw-r--r--. 1 root root 33 1211 14:11 3.txt.gz

tar czf 8.tar.gz a/

如果tar 加路径 会把绝对路径也压缩

计划任务

一次性任务(at)

  • 命令:at

  • 作用:定义一次任务

  • 语法:

    [!NOTE]

    at time(时间)

    at> 要进行的操作

    at> ctrl+d(快捷键操作)

    [root@bogon ~]# at 14:00
    warning: commands will be executed using /bin/sh
    at> mkdir /test8
    at> <EOT> #ctrl + d
    job 1 at Wed Dec 11 14:00:00 2024
    
  • 查看一次性任务:at q

计划任务(cron)

  • 配置文件:/etc/crontab

  • 命令:crontab

  • 语法:crontab + [options]

  • 选项:

    [!TIP]

    -e:编辑计划任务

    -u:指定用户

  • 格式:* * * * * +执行的操作

    分 时 日 月 周

    范围:.---------------- minute (0 - 59)

    | .------------- hour (0 - 23)

    | | .---------- day of month (1 - 31)

    | | | .------- month (1 - 12) OR jan,feb,mar,apr …

    | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

    | | | | |

    *** * * * * user-name command to be executed**

    [!IMPORTANT]

    *:任意时刻

    /:分割时间点 如每30分钟 /30

    -:时间范围 11-12

    ,:时间点 11,12

    */30   4-6    *     *     5            含义:每周五,4点到6点,每隔半小时
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值