Búsqueda de archivos y empaquetado del sistema linux (serializado)

Búsqueda de archivos

grep: filtrado de contenido de archivos

[root@linux-server ~]# grep 'root' /etc/passwd  #从/etc/passwd文件中过滤root字段
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

Buscar comando

[root@linux-server ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
[root@linux-server ~]# which cd
/usr/bin/cd
[root@linux-server ~]# which rm
alias rm='rm -i'
        /usr/bin/rm

Consultar la ubicación de los comandos y archivos de configuración

[root@linux-server ~]# whereis rpm 
rpm: /usr/bin/rpm /usr/lib/rpm /etc/rpm /usr/share/man/man8/rpm.8.gz
[root@linux-server ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz

1. Encuentre una explicación detallada: búsqueda de archivos, por nombre de archivo

语法:
#find 路径 条件 跟条件相关的操作符   [-exec 动作]
路径:
1.默认不写路径时查找的是当前路径.
2.加路径。
条件:
1.指定的名称  -name
2.文件类型 - type

1.1. Por nombre de archivo

从根开始找文件
[root@linux-server ~]# find / -name “file2” #从根开始找文件
/root/file2
/var/tmp/file2
[root@linux-server ~]# find /etc -name "ifcfg-ens33" #以名字的方式查找 
[root@linux-server ~]# find /etc -iname "ifcfg-ens33" #-i忽略大小写

Familiarizado con * comodín

[root@linux-server ~]# find /etc -iname "ifcfg-ens*"
参数解释:
*:表示所有字符

1.2. Por tamaño de archivo

[root@linux-server ~]# find /etc -size +5M      #大于5M
[root@linux-server ~]# find /etc -size 5M       #等于5M
[root@linux-server ~]# find /etc -size -5M      #小于5M
[root@linux-server ~]# find /etc -size +5M -ls  #-ls找到的处理动作,不是平时用的ls

1.3 Encontrar por tiempo

按时间找(atime,mtime,ctime)
-atime=  access访问时间
-mtime = modify改变时间  内容修改时间会改变
-ctime =change修改时间   属性修改时间会改变
[root@linux-server ~]# find /etc -mtime +5      #修改时间5天之前
[root@linux-server ~]# find /etc -atime +1      #访问时间1天之前
[root@linux-server ~]# find /etc -mtime -5      #修改时间5天之内

1.4 Por tipo de archivo

[root@youngfit ~]# find /dev -type f    #f普通文件
[root@youngfit ~]# find /dev -type d    #d目录
[root@youngfit ~]# find /dev -type l    #l链接
[root@youngfit ~]# find /dev -type b    #b块设备
[root@youngfit ~]# find /dev -type c    #c字符设备
[root@youngfit ~]# find /dev -type s    #s套接字

1.5 por permisos de archivo

[root@linux-server ~]# find . -perm 644            #.是当前目录    精确查找644  
[root@linux-server ~]# find /usr/bin  -perm -4000  #包含set uid
[root@linux-server ~]# find /usr/bin  -perm -2000  #包含set gid
[root@linux-server ~]# find /usr/bin  -perm -1000  #包含sticky

1.6 Encontrar la acción de posprocesamiento ACCIONES

-name "ifcfg*" | xargs
-name "ifcfg*" -print   #打印
[root@linux-server ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #exec命令用于调用并执行指令的命令  查找带ifcfg开头的文件复制到tmp下
[root@linux-server ~]# touch /home/test{1..20}.txt
[root@linux-server ~]# find /home/ -name test* -exec rm -rf {} \; #exec为执行一条shell命令      {}为前面查找到的内容   \; 格式

encontrar usando xargs

[root@linux-server ~]# find . -name "yang*.txt" |xargs rm -rf #重点 找到之后删除处理xargs 参数传递处理找出后删除
[root@linux-server ~]# touch /home/test{1..20}.txt
[root@linux-server ~]# find /home/ -name *test* | xargs -i cp -rf {} /var/tmp/

La diferencia entre -exec y xargs

-exec:命令会对每个匹配的文件执行一个单独的操作
xargs:是对参数进行处理的命令。它的任务就是将输入行转换成下一个命令的参数列表。
===============
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \; 作为命令的结束符,书写不便。 
3、综上,如果要使用的命令支持一次处理多个文件, 那么使用 xargs比较方便; 
4、xargs不能操作文件名有空格的文件;

Caso 1: busque test5 y archivos distintos de test5

[root@linux-server ~]# find /home/ -name *test5*
[root@linux-server ~]# find /home/ ! -name *test5* # !--取反

Dos, compresión de paquetes

Herramienta de compresión de paquetes de ventana:

结尾:.rar     .zip
打包工具:winrar zip 7zip 好压

Herramienta de compresión de paquetes de Linux:

结尾:.tar.gz      .tar.bz2     .zip
工具:gzip  bzip2(只压缩) 和 tar(打包)

Bala

#tar cvf file.tar 被打包的文件 ...
c :create  创建
v :verbose 详细信息
f :file  文件

Deshacer

#tar xvf 打包文件 [-C /root/Desktop]
x: extract  加压缩  解包
-C: 指定解包路径

Caso

[root@linux-server ~]# tar cvf dir1.tar /home/dir10/ #打包目录dir10,将包命名为dir1.tar
[root@linux-server ~]# tar xf dir1.tar -C /usr/local/ #将dir1包解压到指定目录

compresión

gzip bzip2
压缩:
    #gzip  源文件 ...   #格式  file.gz结尾
    #bzip2 源文件 ...   #格式  file.bz2结尾

abrir la cremallera

#gunzip    压缩文件
#bunzip2   压缩文件
#gzip   -d 压缩文件  
#bzip2  -d 压缩文件
-d:dicompress 解压缩

Caso

[root@linux-server ~]# gzip file1  #压缩
[root@linux-server ~]# gzip -d file1.gz #解压缩
[root@linux-server ~]# gunzip file1.gz  #也是解压缩包
[root@linux-server ~]# gzip -c file1 > /usr/local/file1.gz  #压缩到指定位置(注意以.gz结尾)
[root@linux-server ~]# gunzip -c /usr/local/file1.gz > /opt/file1 #解压到指定位置(解压出的名字可以自定义)

Empaque y comprima juntos:

[root@linux-server ~]# yum -y install bzip2  #打包bzip2需要安装
#tar cvzf file.tar.gz  源文件 ...
#tar cvjf file.tar.bz2 源文件 ...
z:表示gz压缩
j:表示bz2压缩

Descomprimir y desempaquetar juntos:

#tar xvzf 压缩文件 [-C 解压路径]
#tar xvjf 压缩文件 [-C 解压路径]

Caso

[root@linux-server ~]# tar czf dir1.tar.gz dir1   #打包并压缩
[root@linux-server ~]# tar xzf dir1.tar.gz -C /usr/local/ #解压到指定位置

Empaquete a la ruta especificada

[root@linux-server ~]# tar czf /tmp/`date +%F-%T`-etc.tar.gz /etc/  #将打包的文件放到/tmp目录下,并以当前时间开头命名

Extensión: cree directorios o archivos por tiempo.

# mkdir `date +%F`-upload
# touch file-`date +%F`.txt

Tres archivos de enlace

Enlace flexible o enlace simbólico enlace duro

4.1 enlace simbólico

语法:ln -s  源文件  链接文件
[root@linux-server ~]# echo 111 > /file1
[root@linux-server ~]# ln -s /file1 /file11     //将文件file1软链接到file11
[root@linux-server ~]# ll /file11 
lrwxrwxrwx 1 root root 6 Dec 20 17:58 /file11 -> /file1
​
[root@linux-server ~]# cat /file1 
111
[root@linux-server ~]# cat /home/file11 
111
​
[root@linux-server ~]# rm -rf /file11 #取消软连接。
[root@linux-server ~]# rm -rf /file1  #删除源文件
[root@linux-server ~]# ll /file11 
lrwxrwxrwx 1 root root 6 Dec 20 17:58 /home/file11 -> /file1   #已失效
​
#给目录设置软链接必须是绝对路劲
[root@linux-server ~]# ln -s /root/aaa/ /usr/bbb
[root@linux-server ~]# ll /usr/bbb
lrwxrwxrwx 1 root root 10 Dec 29 21:08 /usr/bbb -> /root/aaa/
[root@linux-server ~]#rm -rf /usr/bbb  #取消链接,注意:删除目录链接时目录后面加“/”是删除目录,不加是删除链接

4.2. Enlace fijo

[root@linux-server ~]# echo 222 > /file2
[root@linux-server ~]# ll -i /file2          #-i:显示inode编号
34045994 -rw-r--r-- 1 root root 4 Dec 29 20:52 file2
[root@linux-server ~]# ln /file2 /file2-h1
[root@linux-server ~]# ln /file2 /etc/file-h3
[root@linux-server ~]# ll -i /file2 /file2-h1 /etc/file-h3
34045994 -rw-r--r--. 4 root root 4 Nov  9 15:01 /etc/file-h3
34045994 -rw-r--r--. 4 root root 4 Nov  9 15:01 /file2
34045994 -rw-r--r--. 4 root root 4 Nov  9 15:01 /file2-h1
[root@linux-server ~]# rm -rf file2  #删除源文件
[root@linux-server ~]# ll /file2-h1 /etc/file-h3 #查看链接文件
34045994 -rw-r--r--. 3 root root 4 Nov  9 15:01 /etc/file-h3
34045994 -rw-r--r--. 3 root root 4 Nov  9 15:01 /file2-h1

Crea varios enlaces a algunos archivos importantes

注:硬链接 
1. 不能跨文件系统(分区)
2. 不支持目录做硬链接
 [root@linux-server ~]# ln /home/ /mnt
 ln: “/home/”: 不允许将硬链接指向目录

La diferencia entre enlace suave y enlace duro:

- 软链接可以跨文件系统,硬链接不可以;
- 软链接可以对目录进行连接,硬链接不可以;
- 删除源文件之后,软链接失效,硬链接无影响;
- 两种链接都可以通过命令 ln 来创建;
- ln 默认创建的是硬链接;
- 使用 -s 参数可以创建软链接。

Supongo que te gusta

Origin blog.csdn.net/qfxulei/article/details/108405917
Recomendado
Clasificación