xargs文件的属性

知识点回顾:
文件的权限
33689416 - rw-r–r-- . 1 root root 5 Jul 30 11:10 1.txt


1 2 3 4 5 6 7 8 9
1.inode index node 索引节点
文件的权限 详细信息 存储在inode索引节点 存放着文件具体的位置
2.文件的类型

  • 普通文件
  • 二进制
  • 数据文件
    d
    l
    c
    b

p
s
3.文件的权限 决定了当前的用户对当前的文件拥有什么权限
r # read 读 cat file.txt less file.txt more head tail…
w # write 写入 vi vim cat echo …
x # execute 可执行 可以运行文件中的命令 可执行颜色变绿

  1. 如何执行文件 ./1.txt 或者全路径方式执行 /root/1.txt
    [root@oldboyedu-lnb ~]# ./1.txt
    -bash: ./1.txt: Permission denied
    [root@oldboyedu-lnb ~]# chmod +x 1.txt
    [root@oldboyedu-lnb ~]# ll 1.txt
    -rwxr-xr-x 1 root root 4 Aug 3 08:16 1.txt
    [root@oldboyedu-lnb ~]# ./1.txt
    /root
    [root@oldboyedu-lnb ~]# cat 1.txt
    pwd
    [root@oldboyedu-lnb ~]# /root/1.txt
    /root
    Linux中文件默认的权限是 rw-r–r--
    为什么Linux中文件默认的权限没有x权限
    普通文本存放什么内容: 字符串信息 配置文件
    文件中有可执行的命令: 不是普通文本 SHLL脚本 需要有x 执行权限
  1. . 开启了selinux后创建文件自带的 关闭selinux点消失

  2. 数字1 代表了硬链接的个数
    什么是硬链接:
    相当于文件有多个入口 类似于超市有个入口 删除一个入口 对文件没有影响
    默认的目录的硬链接个数为2
    [root@oldboyedu-lnb test]# ll -ldi …/test
    17454490 drwxr-xr-x 2 root root 6 Aug 3 08:27 …/test
    [root@oldboyedu-lnb test]# ll -lai
    total 0
    17454490 drwxr-xr-x 2 root root 6 Aug 3 08:27 .
    17454451 drwxr-xr-x. 3 root root 44 Aug 3 08:27 …
    [root@oldboyedu-lnb test]# #cp /etc/hosts /root/3/test/==== cp /etc/hosts .

  3. 文件的属主
    当前的文件属于哪个用户

  4. 文件的属组 stat 1.txt
    当前的文件属于哪个组
    Access 访问时间
    Modify 修改时间
    Change 改变时间

  5. 文件名称

2.文件的类型
使用find按照类型查找文件
-type f d l #
find按名字查找
-name file.txt
模糊查找
-name “.txt"
-name "stu.

-name “.
不区分大小
-iname “.txt"
按照深度等级查找
–maxdepth 1
find ./ -maxdepth 1 -type f
按照文件的大小查找
-size + 大于多少 - 小于多少
find ./ -type f -size +1G
-and 并且 两端同时成立 -o or 或者
[root@oldboyedu-lnb ~]# find ./ -type f -name "
.bak” -size +1G
./1.bak
-o 或者
[root@oldboyedu-lnb ~]# find ./ -type f -name “*.txt” -o -size +1G
./3/1.txt
./3/2.txt
./2.txt
./all.txt
./error.txt
./1.txt
./1.bak
./ok.txt

课程知识介绍
文件的属性
1)find命令
2)文件压缩
3)文件权限
4)软链接硬链接
5)属主属组
6)文件时间
4)inode block

1)find命令 PS: 注意语法中中文的问题
find可以查找文件目录
对查找到的文件或者目录做cp ls mv rm操作
只要是输出到屏幕上的内容称为普通字符串 只有用命令调用普通字符串的时候 才称为文件或目录名称

  1. xargs 可以把其他命令输出的字符串信息 传递到其他命令的最后面 来当做文件或目录名称
    语法格式:
    命令 [参数选项] file|xargs 命令 最后面这个位置
    示例:
    [root@oldboyedu-lnb 3]# echo 1.txt 2.txt|xargs ls -l
    -rw-r–r-- 1 root root 0 Aug 3 08:20 1.txt
    -rw-r–r-- 1 root root 0 Aug 3 08:20 2.txt
    示例:
    [root@oldboyedu-lnb 3]# echo 111 > 1.txt
    [root@oldboyedu-lnb 3]# echo 222 > 2.txt
    [root@oldboyedu-lnb 3]# echo 1.txt 2.txt|xargs cat
    111
    222
  2. find结合xargs find不支持别名
    a. 把find查找到的文件 交给ls命令
    [root@oldboyedu-lnb 3]# find ./ -type f|xargs ls -l
    -rw-r–r-- 1 root root 4 Aug 3 08:57 ./1.txt
    -rw-r–r-- 1 root root 4 Aug 3 08:57 ./2.txt
    -rw-r–r-- 1 root root 0 Aug 3 08:41 ./hehe.
b. 把find查找的文件交给 rm 命令  find不支持别名 所以 rm慎用
[root@oldboyedu-lnb 3]# find ./ -type f -name "1.txt"|xargs rm
c. 把find查找到的文件 交给 mv命令
格式:
[root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs mv 1.bak 1.txt  # 错误的使用方式
[root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs -i mv {} /tmp/1.bak	# 正确的格式

[root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs -i mv {} /tmp/1.bak
[root@oldboyedu-lnb 3]# ll 1.txt /tmp/1.bak
ls: cannot access 1.txt: No such file or directory
-rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/1.bak

cp命令:
[root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt|xargs cp -t /tmp/
[root@oldboyedu-lnb 3]# ll 2.txt /tmp/2.txt
-rw-r--r--  1 root root 0 Aug  3 09:21 2.txt
-rw-r--r--. 1 root root 0 Aug  3 09:28 /tmp/2.txt

[root@oldboyedu-lnb 3]# find ./ -type f -name 3.txt|xargs -i cp {} /tmp
[root@oldboyedu-lnb 3]# ll 3.txt /tmp/3.txt
-rw-r--r--  1 root root 0 Aug  3 09:21 3.txt
-rw-r--r--. 1 root root 0 Aug  3 09:29 /tmp/3.txt

结合grep过滤查找到文件的字符串
[root@oldboyedu-lnb 3]# find ./ -type f 
./hehe.
./2.txt
./3.txt
./4.txt
./5.txt
./6.txt
./7.txt
./8.txt
./9.txt
./10.txt
[root@oldboyedu-lnb 3]# find ./ -type f |xargs grep oldboy
./10.txt:oldboy

PS: grep 默认只过滤当前目录 递归过滤文件内容使用-R
[root@oldboyedu-lnb 3]# grep -R oldboy ./*
./test/10.txt:oldboy

---------------- 批量查找文件 批量改名
find ./ -type f |xargs -i cp {} {}.bak
  1. find查找到的内容交给其他命令
    命令格式:
    find ./ -type -name 1.txt -exec 命令 {} ;
    a. find的结果交给ls 查看
    [root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt -exec ls -l {} ;
    -rw-r–r-- 1 root root 0 Aug 3 09:21 ./2.txt
b. find的结果交给rm 使用
[root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt -exec rm {} \;
[root@oldboyedu-lnb 3]# ll 2.txt
ls: cannot access 2.txt: No such file or directory

c. find结果交给mv 使用
[root@oldboyedu-lnb 3]# find ./ -type f -name 3.txt -exec mv {} /tmp/3.bak \;
[root@oldboyedu-lnb 3]# ll 3.txt /tmp/3.bak
ls: cannot access 3.txt: No such file or directory
-rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/3.bak

cp:
[root@oldboyedu-lnb 3]# find ./ -type f -name 4.txt -exec cp {} /tmp/ \;
[root@oldboyedu-lnb 3]# ll 4.txt /tmp/4.txt
-rw-r--r--  1 root root 0 Aug  3 09:21 4.txt
-rw-r--r--. 1 root root 0 Aug  3 09:42 /tmp/4.txt
  1. find查找的内容交给其他的命令
    语法格式:
    命令 find ./ -type f -name # 反引号中执行后的结果 留在原地 供其他命令使用
    a. find的结果交给ls 查看 命令行支持别名
    [root@oldboyedu-lnb 3]# ll find ./ -type f
    -rw-r–r-- 1 root root 0 Aug 3 09:21 ./4.txt
    b. find 结果交给rm
    [root@oldboyedu-lnb 3]# rm -f find ./ -type f -name 4.txt
    [root@oldboyedu-lnb 3]# ll 4.txt
    ls: cannot access 4.txt: No such file or directory
c. find的结果交给mv
[root@oldboyedu-lnb 3]# mv `find ./ -type f -name 5.txt` /tmp/5.bak
[root@oldboyedu-lnb 3]# ll 5.txt /tmp/5.bak 
ls: cannot access 5.txt: No such file or directory
-rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/5.bak

cp:
[root@oldboyedu-lnb 3]# \cp -r `find ./ -type d` /tmp/

PS: ;分号在命令行中有特殊含义 执行多条命令使用 不管前面的命令是否执行成功都会继续执行
用来分隔命令
[root@oldboyedu-lnb 3]# ls -l;mkdir oldboy;touch oldboy/test.txt
total 0
-rw-r–r-- 1 root root 0 Aug 3 09:21 6.txt
-rw-r–r-- 1 root root 0 Aug 3 09:21 7.txt
-rw-r–r-- 1 root root 0 Aug 3 09:21 8.txt
-rw-r–r-- 1 root root 0 Aug 3 09:21 9.txt
-rw-r–r-- 1 root root 0 Aug 3 08:41 hehe.
drwxr-xr-x 2 root root 20 Aug 3 09:32 test
[root@oldboyedu-lnb 3]# ll oldboy/
total 0
-rw-r–r-- 1 root root 0 Aug 3 10:10 test.txt

-----命令执行失败 继续执行后面的命令
[root@oldboyedu-lnb 3]# lll -l;mkdir alex;touch lidao/hehe.txt
-bash: lll: command not found
touch: cannot touch ‘lidao/hehe.txt’: No such file or directory

&&	# 前面的命令必须执行成功才执行后面的命令 
	示例1:
	[root@oldboyedu-lnb 3]# mkdir test && mkdir alex
	[root@oldboyedu-lnb 3]# ll
	total 0
	drwxr-xr-x 2 root root 6 Aug  3 10:13 alex
	drwxr-xr-x 2 root root 6 Aug  3 10:13 test
	示例2:
	[root@oldboyedu-lnb 3]# mkdir oldboy
	[root@oldboyedu-lnb 3]# ll
	total 0
	drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy
	[root@oldboyedu-lnb 3]# mkdir oldboy && mkdir alex
	mkdir: cannot create directory ‘oldboy’: File exists
	[root@oldboyedu-lnb 3]# ll
	total 0
	drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy

	示例3:
	[root@oldboyedu-lnb 3]# ls -ld oldboy && echo hehe
	drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy
	hehe


||	# 前面的命令必须执行失败 才执行后面的命令
	[root@oldboyedu-lnb 3]# ls test.txt || echo hehe
	ls: cannot access test.txt: No such file or directory
	hehe

	[root@oldboyedu-lnb 3]# ls test.txt || echo hehe && ls hehe.txt || echo ok
	ls: cannot access test.txt: No such file or directory
	hehe
	ls: cannot access hehe.txt: No such file or directory
	ok

	示例:
	[root@oldboyedu-lnb 3]# cd alex|| mkdir alex
	-bash: cd: alex: No such file or directory
	[root@oldboyedu-lnb 3]# ll
	total 0
	drwxr-xr-x 2 root root 6 Aug  3 10:18 alex

2)文件压缩
windows的压缩方式 .rar .rar5 .zip # 提前安装rar压缩软件
Linux的压缩方式:
命令格式:
命令 [参数选项] 压缩包的名称.tar.gz 文件/目录
tar -zcvf test.tar.gz /etc/hosts
命令 参数选项 筐子 水果/香蕉/荔枝
tar -zcvf test.tar.gz /etc/hosts 1.txt /tmp/test.txt
参数选项:
z # 使用gzip方式压缩
c # create 创建压缩包
v # verbose 显示压缩的过程
f # 指定文件
x # 解压
t # 查看压缩包的文件名称
C # 指定解压到哪里
-P # 不提示从成员中删除/ PS: 进入到相对路径打包 不提示
–exclude # 排除文件 --exclude=file
–exclude-from # 排除文件中的文件名 --exclude-from=file
示例1: 把/etc/hosts文件打包成test.tar.gz
[root@oldboyedu-lnb ~]# tar zcvf hosts.tar.gz /etc/hosts
tar: Removing leading `/’ from member names
/etc/hosts

		示例2: 不显示压缩过程
		[root@oldboyedu-lnb ~]# touch 1.txt 2.txt
		[root@oldboyedu-lnb ~]# tar zcf test.tar.gz {1..2}.txt
		-rw-r--r-- 1 root root 116 Aug  3 10:51 test.tar.gz
		
		示例3: 同时压缩打包压缩多个文件
		[root@oldboyedu-lnb ~]# tar zcvf all.tar.gz *.txt /etc/hosts /etc/passwd
		1.txt
		2.txt
		tar: Removing leading `/' from member names
		/etc/hosts
		/etc/passwd
		不显示删除/
		[root@oldboyedu-lnb ~]# tar zcvfP all.tar.gz *.txt /etc/hosts /etc/passwd
		1.txt
		2.txt
		/etc/hosts
		/etc/passwd
		
		示例4: 指定压缩包的位置
		[root@oldboyedu-lnb ~]# tar zcvf /opt/all.tar.gz 1.txt 2.txt
		1.txt
		2.txt
		[root@oldboyedu-lnb ~]# ll /opt/
		total 4
		-rw-r--r-- 1 root root 116 Aug  3 10:57 all.tar.gz

		
		
		示例5: 如何解压 xf
		[root@oldboyedu-lnb ~]# tar xf hosts.tar.gz 
		
		示例6: 解压到固定的目录

		[root@oldboyedu-lnb ~]# tar xf all.tar.gz -C /opt/
		[root@oldboyedu-lnb ~]# ll /opt/
		total 4
		-rw-r--r-- 1 root root   0 Aug  3 10:51 1.txt
		-rw-r--r-- 1 root root   0 Aug  3 10:51 2.txt
		-rw-r--r-- 1 root root 116 Aug  3 10:57 all.tar.gz
		drwxr-xr-x 2 root root  33 Aug  3 10:59 etc

		示例7: 查看压缩包里面的文件或目录名称  tf
		[root@oldboyedu-lnb ~]# tar tf hosts.tar.gz 
		etc/hosts
		[root@oldboyedu-lnb ~]# tar tf test.tar.gz 
		1.txt
		2.txt
		[root@oldboyedu-lnb ~]# echo hehe > 1.txt
		[root@oldboyedu-lnb ~]# tar tf test.tar.gz |xargs cat
		hehe
		[root@oldboyedu-lnb ~]# 
		[root@oldboyedu-lnb ~]# cat 1.txt 2.txt

		示例8: 排除压缩某个文件 exclude 排除

		打包当前目录所有的文件
		[root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./*
		./1.txt
		./2.txt
		./etc/
		./etc/hosts

		排除当前目录的1.txt
		[root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude=1.txt
		./2.txt
		./etc/
		./etc/hosts
		[root@oldboyedu-lnb ~]# tar tf exclude.tar.gz 
		./2.txt
		./etc/
		./etc/hosts
		 
		排除当前目录的1.txt和2.txt
		[root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude=1.txt --exclude=2.txt
		./etc/
		./etc/hosts
		
		[root@oldboyedu-lnb ~]# ll
		total 4
		-rw-r--r-- 1 root root  5 Aug  3 11:02 1.txt
		-rw-r--r-- 1 root root  0 Aug  3 10:51 2.txt
		-rw-r--r-- 1 root root  0 Aug  3 11:13 alex.txt
		drwxr-xr-x 2 root root 19 Aug  3 10:56 etc
		-rw-r--r-- 1 root root  0 Aug  3 11:13 oldboy.txt
		[root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude={1,alex}.txt
		./2.txt
		./etc/
		./etc/hosts
		./oldboy.txt

		排除多个文件: 可以把多个文件名称 写入到文本中 排除文本中的文件
		第一步: 排除的文件名写入exclude.txt
		[root@oldboyedu-lnb ~]# echo -e "1.txt\nalex.txt\noldboy.txt" > exclude.txt
		[root@oldboyedu-lnb ~]# cat exclude.txt 
		1.txt
		alex.txt
		oldboy.txt

		第二步: 打包排除 --exclude-from=file.txt
		[root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude-from=exclude.txt 
		./2.txt
		./all.tar.gz
		./etc/
		./etc/hosts
		./exclude.txt

		
	PS: xargs -n  输出的内容显示n列	
		[root@oldboyedu-lnb ~]# echo {1..10}|xargs -n3
		1 2 3
		4 5 6
		7 8 9
		10
		[root@oldboyedu-lnb ~]# echo {1..10}|xargs -n4
		1 2 3 4
		5 6 7 8
		9 10

3)文件权限
33575026 -rw-r–r-- 1 root root 5 Aug 3 11:02 1.txt
rw-r–r-- # 9位权限位 三位一组
rwx # 三位最大的权限

前三位		# 属主的权限位  文件的主人 文件的创建者对文件的权限
			rw-
			r   可读
			w   可写
			-   没有权限
			最大权限 rwx
			类似于 笔记本(文件) 属于 张三的
			当前的1.txt文件是属于root用户的
			
中三位     # 属组的权限位  在组内的成员对我的文件拥有什么权限
			r--
			r   可读
			-	没有权限
			-   没有权限
			类似于 笔记本属于张三 也属于某个组 张三的家庭
后三位	   # 其他用户的权限位 对于张三来说 陌生人拥有什么权限
		   不是属主 也不在组内的人 对文件的权限
		   r--	可读
		   -	没有权限 
		   -    没有权限
		   类似于 张三不认识的人 也不在组内的成员 对笔记本的权限
		   
PS:组的权限和其他用户的权限默认相同
		   
rwx 相对应的数字来表示权限
r  read----> 4
w  write---> 2
x  write---> 1

rw-r--r--  # 使用数字来表示权限 
每个权限位相加 rw- 
把所有位置的数字组合在一起

r(4)w(2)-(0)===6			# 属主 前三位相加
r(4)-(0)-(0)===4			# 属组 中三位相加
r(4)-(0)-(0)===4			# 其他 后三位相加

权限组合在一起 644   # Linux文件默认的权限
目录的权限: 默认的权限755
rwxr-xr-x
rwx=7
r-x=5
r-x=5
  1. 文件的属主
    rw-r–r-- 1 root root 5 Aug 3 11:02 1.txt
    ---- —
    1 2
    1.文件的属主 文件的主人 文件的创建者

  2. 文件的属组

    2.文件的属组 用户组 对文件的权限

总结:
find 命令查找文件
xargs
exec
反引号
; && ||
tar 压缩 尽量使用相对路径

下次内容:
	inode
	软硬链接
	时间属性

作业:
1.在oldboy目录下创建lidao1…lidao5 5个目录
2.在oldboy目录下创建alex1.txt aelx2.txt 然后在5个目录下创建2个文件 www.conf bbs.conf
3.使用find的三种方法查找oldboy的一级目录下所有的文件 复制到/tmp下
4.使用find的三种方式把oldboy下所有的文件mv 到 /opt目录下
5.使用find的三种方式把 oldboy目录下所有目录删除 把 /opt所有文件删除

find命令如何取反 find命令如何排除某个文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值