Marco Blog Homework Week 5

                                                                                                                                              File search and compression

1. File Search

1.locate non-real-time search  

• locate Query the pre-built file index database /var/lib/mlocate/mlocate.db on the system

• The index is built automatically when the system is relatively idle (periodic task), updatedb can update the database

• The index building process needs to traverse the entire root file system, which consumes resources

• Working characteristics: fast search speed; fuzzy search; non-real-time search; searching the full path of the file, not just the file name; may only search the directory where the user has read and execute permissions

• Common options: Format: locate [OPTION] ... [PATTERN] ...

  -i case-insensitive search

  -n N lists only the first N matching items

  -r Use basic regular expressions

 

• find real-time search tool, complete the file search by traversing the specified path

• Search path: specify a specific target path; the default is the current directory

• Search conditions: The specified search criteria can be based on file name, size, type, permissions, etc .; the default is to find all files under the specified path

• Processing action: operate on files that meet the conditions, and output to the screen by default

• Working characteristics: slightly slower search speed; precise search; real-time search; rich search conditions; may only search the directory where the user has read and execute permissions

• Format: find [OPTION] ... [Search Path] [Search Condition] [Processing Action]

 

• Specify the search directory level
• -maxdepth level The maximum search directory depth, the files in the specified directory are level 1
• -mindepth level The minimum search directory depth

• 示例: find /etc -maxdepth 2 -mindepth 2  -type d

 

 

2. Query by file name and inode node number

 

• -name "file name": supports the use of glob, such as: *,?, [], [^], Wildcards must be enclosed in double quotes
• -iname "file name": does not distinguish between uppercase and lowercase letters
• -inum n press inode number search
• -samefile name files with the same inode number
• -links n files with n links
• -regex “PATTERN”: match the entire file path with PATTERN instead of the file name

 

2.1 Search by owner and group

• -user USERNAME: find the file whose owner is the specified user (UID)
• -group GRPNAME: find the file whose owner group is the specified group (GID)
• -uid UserID: find the file whose owner is the specified UID number
• -gid GroupID: find files with the specified GID number belonging to the group
• -nouser: find files without the owner
• -nogroup: find files without the group

 

2.3 Search by file type

 

• -type TYPE
• TYPE can be in the following forms:
• f: normal file
• d: directory file
• l: symbolic link file
• s: socket file
• b: block device file
• c: character device file
• p: pipe file

 

 

2.4 Empty files or directories

• -empty

• Example: find / app -type d -empty

 

2.5 Combination conditions

• AND: -a, the default multiple conditions are AND
• OR: -o
• NOT: -not!

•  德·摩根定律:
•  (非 A) 或 (非 B) = 非(A 且 B)
•  (非 A) 且 (非 B) = 非(A 或 B)

•  示例:
•  !A -a !B = !(A -o B)
•  !A -o !B = !(A -a B)

 

2.6根绝文件大小查找

• 常用单位:k, M, G,c(byte),注意大小写敏感

• -size   6k   表示[5 K - 6 K] 之间

• -size   -6k   表示[0 K - 6 K] 之间

• -size   +6k   表示[6K - 6 K以上] 之间

 

2.7根据时间查找

• -atime/amin  10  表示[10天/分钟 - 11天/分钟] 加1天

• -ctime/cmin  -10  表示[0天/分钟 - 10天/分钟]

• -mtime/mmin  +10 表示[10天/分钟 - 10天/分钟以上]

 

2.8根据权限查找

• -perm [/|-]MODE
• MODE: 精确权限匹配
• /MODE:任何一类(u,g,o)对象的权限中只要能一位匹配即可,或关系,+ 从CentOS 7开始淘汰
• -MODE:每一类对象都必须同时拥有指定权限,与关系
• 0 表示不关注

•示例:find /etc -perm -777

• 处理动作

 

• -print:默认的处理动作,显示至屏幕
• -ls:类似于对查找到的文件执行“ls -l”命令
• -fls file:查找到的所有文件的长格式信息保存至指定文件中,相当于 -ls > file
• -delete:删除查找到的文件,慎用!
• -ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认
• -exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令
• {}: 用于引用查找到的文件名称自身

 

 

2.9参数替换 xargs

• 由于很多命令不支持管道|来传递参数,xargs用于产生某个命令的参数,xargs 可以读入 stdin 的数据,并且以空格符或回车符将 stdin 的数据分隔成为参数

• 另外,许多命令不能接受过多参数,命令执行可能会失败,xargs 可以解决

• 注意:文件名或者是其他意义的名词内含有空格符的情况

• 示例:

#批量创建和删除用户
echo user{1..10} |xargs -n1 useradd
echo user{1..100} | xargs -n1 userdel -r

#这个命令是错误的
find /sbin/ -perm /700 | ls -l

#查找有特殊权限的文件,并排序
find /bin/ -perm /7000 | xargs ls -Sl

#此命令和上面有何区别?

find /bin/ -perm -7000 | xargs ls -Sl

#以字符nul分隔

find -type f -name "*.txt” -print0 | xargs -0 rm

#并发执行多个进程
seq 100 |xargs -i -P10 wget -P /data http://10.0.0.8/{}.html

#并行下载视频
seq 199 | xargs -i -P3 you-get https://www.bilibili.com/video/BV1Et411E7dx?p={}

 

2.压缩和解压缩

2.1compress和uncompress  此工具来自于ncompress包   

• 常用选项

• -d 解压缩,相当于uncompress
• -c 结果输出至标准输出,不删除原文件
• -v 显示详情

• 格式:

• compress Options [file ...]
• uncompress file.Z #解压缩

• zcat file.Z 不显式解压缩的前提下查看文本文件内容

 

2.2 gzip和gunzip 

常用选项

• -k keep, 保留原文件,CentOS 8 新特性
• -d 解压缩,相当于gunzip
• -c 结果输出至标准输出,保留原文件不改变
• -# 指定压缩比,#取值为1-9,值越大压缩比越大

• 格式:gzip [OPTION]... FILE ...

• 示例:

• #解压缩
• gunzip file.gz
• #不显式解压缩的前提下查看文本文件内容
• zcat file.gz

 

 

2.3 bzip2和bunzip2  来自于 bzip2 包

• 常用选项

• -k keep, 保留原文件
• -d 解压缩
• -c 结果输出至标准输出,保留原文件不改变
• -# 1-9,压缩比,默认为9

• 格式:bzip2 [OPTION]... FILE ...

• 示例:

• bunzip2 file.bz2 解压缩
• bzcat file.bz2 不显式解压缩的前提下查看文本文件内容

 

2.4 xz和unxz   来自于xz包 

• 常用选项

• -k keep, 保留原文件
• -d 解压缩
• -c 结果输出至标准输出,保留原文件不改变
• -# 压缩比,取值1-9,默认为6

• 格式:xz [OPTION]... FILE ...

• 示例

• unxz file.xz 解压缩
• xzcat file.xz 不显式解压缩的前提下查看文本文件内容

 

2.5 zip和unzip     来自于zip 和 unzip 包

• zip 可以实现打包目录和多个文件成一个文件并压缩,但可能会丢失文件属性信息,如:所有者和组信息,一般建议使用 tar 代替

• 示例

#打包并压缩
zip –r /backup/sysconfig.zip /etc/sysconfig/

#不包括目录本身,只打包目录内的文件和子目录
cd /etc/sysconfig; zip -r /root/sysconfig.zip *

#默认解压缩至当前目录
unzip /backup/sysconfig.zip

#解压缩至指定目录,如果指定目录不存在,会在其父目录(必须事先存在)下自动生成
unzip /backup/sysconfig.zip -d /tmp/config
cat /var/log/messages | zip messages -

#-p 表示管道
unzip -p message.zip > message

 

3. tar   打包和解包

• tar 即 Tape ARchive 磁带归档,可以对目录和多个文件打包一个文件,并且可以压缩,保留文件属性不丢失,常用于备份功能,推荐使用

• 常用选项 和组合用法

• -c 打包

• -t 预览

• -x 解包

• -f 后边跟文件

• -v 显示过程

• cvf 打过文件并显示过程    

• tvf 预览文件

• xcf 解包并显示过程

• 压缩成 gz格式  前边需要加 z   tar zcvf etc.tar.gz /etc/

• 压缩成  bz2格式  前边需要加 j   tar jcvf etc.tar.bz2 /etc/

• 压缩成  xz格式    前边需要加 J   tar Jcvf etc.tar.xz /etc/

 

• 只打包目录内的文件,不所括目录本身
• cd /etc
• tar zcvf /root/etc.tar.gz *

 

3.1 split 命令可以分割一个文件为多个文件

• 分割大的 tar 文件为多份小文件

 split -b 1M mybackup.tgz mybackup-parts

• 切换成的多个小分文件使用数字后缀

 split -b 1M –d mybackup.tgz mybackup-parts

• 将多个切割的小文件合并成一个大文件

 cat mybackup-parts* > mybackup.tar.gz

 

 

 

Guess you like

Origin www.cnblogs.com/chaidakun/p/12727871.html