第十章、日常运维(补)

10.28 rsync工具介绍

10.29/10.30 rsync常用选项

10.31 rsync通过ssh同步

10.32/10.33 rsync通过服务同步

10.34 linux系统日志

10.35 screen工具

10.36  xargs用法详解

10.28 rsync工具介绍(rsync要掌握)

Linux文件同步工具-rsync

Rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,同时可以对上传部分先进行压缩,因此rsync效率很高。rsync可以复制显示目录属性及文件,而且可以选择性地压缩及递归复制。但是rsync每次执行rsync命令都会遍历目标目录,如果文件数达到了一定规模,每次遍历就会消耗很多资源。

Rsync 可以搭配 rsh 或 ssh 甚至使用 daemon 模式。 Rsync server 会打开一个 873 的服务通道 (port) ,等待对方 Rsync 连接。连接时, Rsync server 会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,下一次就只传送二个文件之间不同的部份。

Rsync 支持大多数的类 Unix 系统,无论是 Linux 、 Solaris 还是 BSD 上都经过了良好的测试。此外,它在 windows 平台下也有相应的版本,比较知名的有 cwRsync 和 Sync2NAS 。

Rsync 的基本特点如下:

1.可以镜像保存整个目录树和文件系统;

2.可以很容易做到保持原来文件的权限、时间、软硬链接等;

3.无须特殊权限即可安装;

4.优化的流程,文件传输效率高;

5.可以使用 rcp 、 ssh 等方式来传输文件,当然也可以通过直接的 socket 连接;

6.支持匿名传输。

核心算法介绍:

假定在名为α和β的两台计算机之间同步相似的文件 A 与 B ,其中α对文件 A 拥有访问权,β对文件 B 拥有访问权。并且假定主机α与β之间的网络带宽很小。那么 rsync 算法将通过下面的五个步骤来完成:

1.β将文件 B 分割成一组不重叠的固定大小为 S 字节的数据块。最后一块可能会比 S 小。

2.β对每一个分割好的数据块执行两种校验:一种是 32 位的滚动弱校验,另一种是 128 位的 MD4 强校验。

3.β将这些校验结果发给α。

4.α通过搜索文件 A 的所有大小为 S 的数据块 ( 偏移量可以任选,不一定非要是 S 的倍数 ) ,来寻找与文件 B 的某一块有着相同的弱校验码和强校验码的数据块。这项工作可以借助滚动校验的特性很快完成。

5.α发给β一串指令来生成文件 A 在β上的备份。这里的每一条指令要么是对文件 B 经拥有某一个数据块而不须重传的证明,要么是一个数据块,这个数据块肯定是没有与文件 B 的任何一个数据块匹配上的。

#rsync能传输文件,备份数据;实现本机到本机的传输,也能本机到远程的传输;

#rsync是增量备份

yum  install -y  rsync     #安装rsync命令

##假设现在有两个目录A、B,A目录内的数据是实时更新的;需求是每隔一小时将A目录数据拷贝到B目录

 可以使用rsync,因为rsync是增量备份,会备份A新增加或变更的数据到B,也能远程使用rsync

例:将/etc/passwd 拷贝到/tmp/1.txt文件

rsync -av /etc/passwd    /tmp/1.txt

[root@xinlinux-02 tmp]# rsync -av /etc/passwd  ./1.txt

sending incremental file list

passwd

sent 1,329 bytes  received 35 bytes  2,728.00 bytes/sec

total size is 1,237  speedup is 0.91

远程拷贝/etc/passwd到192.168.233.129的/tmp/1.txt

rsync -av /etc/passwd  root@192.168.233.129:/tmp/1.txt

然后会提示输入192.168.188.128的密码

[root@xinlinux-02 tmp]# rsync -av /etc/passwd  [email protected]:/tmp/1.txt

bash: rsync: 未找到命令

rsync: connection unexpectedly closed (0 bytes received so far) [sender]

rsync error: remote command not found (code 127) at io.c(226) [sender=3.1.2]

##原因是192.168.233.129没有安装rsync

[root@xinlinux-02 tmp]# rsync -av /etc/passwd  [email protected]:/tmp/1.txt

sending incremental file list

passwd

sent 1,329 bytes  received 35 bytes  909.33 bytes/sec

total size is 1,237  speedup is 0.91

rsync格式

rsync [OPTION] … SRC   DEST                    

 #本机拷贝

rsync [OPTION] … SRC   [user@]host:DEST    

 ##通过ssh的方式同步本机拷贝到远程机器

rsync [OPTION] … [user@]host:SRC   DEST

#通过ssh的方式将远程机器的文件拷贝到本机

rsync [OPTION] … SRC   [user@]host::DEST

#通过服务的方式同步本机拷贝到远程机器

rsync [OPTION] … [user@]host::SRC   DEST

#通过服务的方式同步将远程机器的文件拷贝到本机

#SRC是源目录/文件

#DEST是目标目录/文件

#user@可以不写,如果不写的话表示当前用户

10.29/10.30 rsync常用选项

rsync常用选项

  • -a 包含-rtplgoD选项

  • -r 同步目录时要加上,类似cp时的-r选项

  • -v 同步时显示一些信息,让我们知道同步的过程

  • -l 保留软连接

  • -L 加上该选项后,同步软链接时会把源文件给同步

  • -p 保持文件的权限属性

  • -o 保持文件的属主

  • -g 保持文件的属组

  • -D 保持设备文件信息

  • -t 保持文件的时间属性

  • --delete 删除DEST中SRC没有的文件

  • --exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步

  • -P 显示同步过程,比如速率,比-v更加详细

  • -u 加上该选项后,如果DEST中的文件比SRC新,则不同步(文件的新旧指的是mtime的时间

  • -z 传输时压缩(传输前压缩,传输完后解压,节省传输过程带宽)

  • --port   指定端口号

本地同步例子

#-L,将root目录下111目录同步到tmp目录下的111_dest目录

rsync  -av    /root/111/    /tmp/111_dest/

[root@xinlinux-02 ~]# rsync  -av    /root/111/    /tmp/111_dest/

sending incremental file list

./

1.txt

test.txt -> /root/2.txt

sent 151 bytes  received 41 bytes  384.00 bytes/sec

total size is 11  speedup is 0.06

[root@xinlinux-02 ~]# rsync  -avL    /root/111/    /tmp/111_dest/

sending incremental file list

test.txt

sent 129 bytes  received 35 bytes  328.00 bytes/sec

total size is 0  speedup is 0.00

因为111目录里面有一个软链接,-a选项里面的小l只会同步软链接,并不会同步软链接的源文件,所以同步后的软链接指向的还是原来的源文件

[root@xinlinux-02 ~]# rsync  -av    /root/111/    /tmp/111_dest/

sending incremental file list

./

1.txt

test.txt -> /root/2.txt

sent 151 bytes  received 41 bytes  384.00 bytes/sec

total size is 11  speedup is 0.06

[root@xinlinux-02 ~]# ll /tmp/111_dest

总用量 0

-rw-r--r--. 1 root root  0 9月   2 09:12 1.txt

lrwxrwxrwx. 1 root root 11 9月   2 09:24 test.txt -> /root/2.txt

如果用了L,那a选项里面的小l的含义会被覆盖掉,同步软链接时会把源文件给同步,软链接文件被替换成了源文件

rsync  -avL    /root/111/    /tmp/111_dest/

[root@xinlinux-02 ~]# rsync  -avL    /root/111/    /tmp/111_dest/

sending incremental file list

test.txt

sent 129 bytes  received 35 bytes  328.00 bytes/sec

total size is 0  speedup is 0.00

[root@xinlinux-02 ~]# ll /tmp/111_dest

总用量 0

-rw-r--r--. 1 root root 0 9月   2 09:12 1.txt

-rw-r--r--. 1 root root 0 9月   2 09:24 test.txt

##--delete    将111_dest目录下111目录没有的目录或文件删除掉

[root@xinlinux-02 ~]# ls 111/

1.txt  test.txt

[root@xinlinux-02 ~]# ls /tmp/111_dest/

1.txt  5.txt  test.txt

[root@xinlinux-02 ~]# rsync  -av --delete  /root/111/ /tmp/111_dest/

sending incremental file list

deleting 5.txt

./

test.txt -> /root/2.txt

sent 112 bytes  received 31 bytes  286.00 bytes/sec

total size is 11  speedup is 0.08

[root@xinlinux-02 ~]# ls 111/

1.txt  test.txt

[root@xinlinux-02 ~]# ls /tmp/111_dest/

1.txt  test.txt

##--exclude,将包含“*.txt”和“xin*”的文件过滤掉然后同步其余文件(支持多个--exclude一起用)

rsync -avL --exclude "*.txt" --exclude "xin*"  /root/111/  /tmp/111_dest/

[root@xinlinux-02 111]# ls

1234  1.test  1.txt  fasf  test.txt

[root@xinlinux-02 111]# touch xin

[root@xinlinux-02 111]# rsync -avL --exclude "*.txt" --exclude "xin*"  /root/111/  /tmp/111_dest/

sending incremental file list

./

1.test

1234

fasf

sent 226 bytes  received 76 bytes  604.00 bytes/sec

total size is 0  speedup is 0.00

##-P,查看同步传输过程的速率和百分比

rsync  -avP    /root/111/    /tmp/111_dest/

[root@xinlinux-02 111]# rsync  -avP    /root/111/    /tmp/111_dest/

sending incremental file list

xin

              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/7)

sent 225 bytes  received 35 bytes  520.00 bytes/sec

total size is 11  speedup is 0.04

#一般情况目标目录内的文件修改,然后再同步,目标目录修改的内容会被覆盖,因为同步是以源目录为主

特殊要求:目标目录修改的文件不被源目录同步时覆盖,加-u选项

10.31 rsync通过ssh同步

rsync通过ssh方式同步(用一个冒号":")

#将本机/tmp/1.txt文件同步到远程机器192.168.233.129的/tmp/2.txt文件,需要输入192.168.233.129的密码

rsync -avP /tmp/1.txt   192.168.233.129:/tmp/2.txt

[root@xinlinux-02 ~]# rsync -avP /tmp/1.txt   192.168.233.129:/tmp/2.txt

[email protected]'s password:

sending incremental file list

1.txt

          1,237 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 1,328 bytes  received 35 bytes  545.20 bytes/sec

total size is 1,237  speedup is 0.91

#如果192.168.233.129的端口并不是22,增加一个选项-e "ssh -p 端口号"

rsync -avP -e "ssh -p 22" /tmp/1.txt 192.168.233.129:/tmp/2.txt

[root@xinlinux-02 ~]# rsync -avP -e "ssh -p 22" /tmp/1.txt   192.168.233.129:/tmp/2.txt

[email protected]'s password:

sending incremental file list

1.txt

          1,237 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 1,328 bytes  received 35 bytes  302.89 bytes/sec

total size is 1,237  speedup is 0.91

三、ssh免密同步(密钥验证)

本机免密同步192.168.233.129

1、在本机上Keygen生成密钥对

ssh-keygen -t rsa 

2、将公钥复制到 192.168.233.130

ssh-copy-id 192.168.233.130

3、然后可以ssh免密同步

ssh  192.168.233.130

rsync  -av 192.168.233.130:/etc/passwd  /tmp/mypasswd

10.32/10.33 rsync通过服务同步

rsync 通过服务的方式同步(用两个冒号"::")

原理:开启rsync服务,并会监听一个873端口,端口可以自定义;开启rsync服务后,客户端就能通过873端口和服务端进行通信,可以传输数据

##必要要求,两台机器间能相互通信,且都要安装rsync命令

步骤:

1、先编辑配置文件/etc/rsyncd.conf

rsyncd.conf样例

port=873

log file=/var/log/rsync.log

pid file=/var/run/rsyncd.pid

address=192.168.233.130

[test]

path=/tmp/rsync

use chroot=true

max connections=4

read only=no

list=true

uid=root

gid=root

#auth users=test

#secrets file=/etc/rsyncd.passwd

hosts allow=192.168.233.129 

2、然后启动服务          rsync --daemon           

[root@xinlinux-02 ~]# rsync --daemon

[root@xinlinux-02 ~]# ps aux |grep rsync

root       3745  0.0  0.0 114740   560 ?        Ss   10:43   0:00 rsync --daemon

root       3747  0.0  0.0 112720   972 pts/0    S+   10:43   0:00 grep --color=auto rsync

3、同步文件

格式:#"192.168.233.130::模块名字/同步过来的文件或目录名"后面跟的是模块名字,看配置文件自定义命名的模块是什么:然后模块名字后面接着同步过来的文件/目录命名

用客户机同步

rsync -av /tmp/2.txt  192.168.233.130::test/xin.txt

[root@xinlinux-01 ~]# rsync -av /tmp/2.txt  192.168.233.130::test/xin.txt

sending incremental file list

2.txt

sent 1,333 bytes  received 35 bytes  2,736.00 bytes/sec

total size is 1,243  speedup is 0.91

实验错误:

[root@xinlinux-02 ~]#rsync -av /tmp/2.txt  192.168.233.130::test/xin.txt

rsync: failed to connect to 192.168.233.130 (192.168.233.130): No route tohost (113)

rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]

[root@xinlinux-02 ~]# telnet 192.168.233.130 873

Trying 192.168.233.130...

telnet: connect to address 192.168.233.129: No route to host

##端口不通,可能是iptables的问题;firewalld没关,,用iptables -nvL 查看,

[root@xinlinux-02 ~]# systemctl stop firewalld

[root@xinlinux-02 ~]# telnet 192.168.233.130  873

Trying 192.168.233.130...

Connected to 192.168.233.130.

Escape character is '^]'.

@RSYNCD: 31.0

^]

telnet> quit

Connection closed.

 #telnet  命令用来检测ip端口是否通

yum install  -y  telnet 

telent格式:telnet    IP   端口号

例:telnet 192.168.233.130  873

Ctrl+]方括号键退出,然后输入quit就能退出telent

rsyncd.conf配置文件详解

port

指定在哪个端口启动rsyncd服务,默认是873端口。

log file

指定日志文件

address

指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。

pid file

指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。

[ ]

指定模块名,里面内容自定义。

path

指定数据存放的路径

use chroot true|false

表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是true可以实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,建议设置成false。

max connections

指定最大的连接数,默认是0,即没有限制

read only ture|false

如果为true,则不能上传到该模块指定的路径下。

list

表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。

uid/gid

指定传输文件时以哪个用户/组的身份传输。

auth users

指定传输时要使用的用户名。

secrets file

指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码

hosts allow

表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。

举例注释:

use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是true可以实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,阿铭建议你设置成false。

list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。

#查看可用模块只需要写到“::”时直接回车,系统就会列出可用模块;如果list=false时,则系统就不会列出

auth users:指定传输时要使用的用户名。

secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码

##当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件

服务端与客户端进行免密同步传输

##实现效果为服务端同步客户端不需要输入密码,客户端同步服务端不需要输入密码

vim /etc/rsyncd.conf

增加内容如下:

auth  users=test

secrets file=/etc/rsyncd.passwd

##密码文件权限都要改为600

##服务端上的密码文件内容要写账户密码

##客户端密码文件内容只写密码

1、首先,在服务端的密码文件/etc/rsyncd.passwd写入"用户名:密码";

2、然后在服务端创建一个密码文件/etc/rsync_pass.txt,写入"密码"

3、两端就能进行免密同步传输了

rsync -avL  /tmp/test8/   --password-file=/etc/rsync_pass.txt test@192.168.233.130::test/test1/ 

hosts allow表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开

10.34 linux系统日志

/var/log/messages

#Linux系统总日志,很多服务的信息都会纪录在里面,除非某些特殊的服务有自己定义的单独日志,否则都会纪录在messages里面

#有自主切割功能,当日志数据达到一定程度时会自动切割成其他以messages-日期命名的日志文件

/etc/logrotate.conf 日志切割配置文件

内容说明:

weekly           #每周切割一次

rotate  4        #切割4个

create           #创建新文件

dateext        #后缀为日期

compress     #压缩

include  /etc/logratate.d    #/etc/logratate.d 目录下还有配置文件

参考文章:https://my.oschina.net/u/2000675/blog/908189 

dmesg命令

#可以查看硬件的故障和错误

dmesg     #把系统硬件相关的日志列出来,日志保存到内存中

dmesg  -c      #把硬件日志清空

/var/log/dmesg 日志文件

#记录系统在启动时核心检测过程所生产的各项信息,与dmesg命令没有任何关系

 /var/log/boot.log

#记录守护进程启动和停止相关的日志信息

rsylogd(挺重要的

#rsyslogd的主要配置文件:/etc/rsyslog.conf

#配置文件/etc/sysconfig/rsyslog主要用来配置是否接收其他主机的日志信息。

#syslogd服务可以帮助我们主动收集到设备的各种信息,并将其保存在服务器上,当出现问题时可以省去手动收集信息的麻烦,方便快捷的从syslogd服务中读取各种信息进行分析排错。

在centos7中已经用rsyslogd取代了syslogd。rsyslod是加强版的syslogd,且完美兼容syslogd。

配置日志记录的规则语法格式如下

服务名[.=!]信息等级 记录方式

服务名

ID

服务名

说明

0

kern

内核日志信息

1

user

用户日志信息

2

mail

邮件系统日志信息

3

daemon

系统守护进程日志信息

4

auth

安全管理日志信息

5

syslog

syslogd守护进程日志信息

6

lpr

打印服务日志信息

7

news

新闻组服务日志信息

8

uucp

uucp系统日志信息

9

cron

cron守护进程日志信息

10

authpriv

私有的安全管理日志信息

11

ftp

ftp守护进程

12-15

local0-local7

系统保留

16-23

保留给系统本地使用

信息等级

ID

信息等级

说明

0

emerg或panic

系统不可用,死机

1

alert

警告事件,可能会影响到系统的运行,必须立即采取行动纠正事件

2

cri

严重错误,比error更严重的错误,如硬件故障

3

error或err

错误信息,如某些服务无法启动。需仔细检查错误原因

4

warning

警告信息,但不会影响系统的进程

5

notice

比info更应注意的信息,需特别处理

6

info

基本信息,主要是一些提示信息

7

debug

程序调试信息

连接符

服务名与信息等级之间有一个连接符。与后面的信息等级结合使用可以表示服务要记录哪些级别的日志信息。

1.:记录高于等于该等级的信息

2.=:只记录等于该等级的信息

3.!:记录除了该等级外的所有信息

last命令

#查看正确登录历史,包括reboot重启

#调用的文件/var/log/wtmp,无法通过cat查看,只能通过last  查看

-n  参数指定记录数

例: last -n 5

lastb命令

#查看登录失败的用户

#对应的文件时/var/log/btmp,无法通过cat查看

,只能通过lastb查看

/var/log/secure     #安全日志

#用户登录正确、错误信息都会纪录

10.35 screen工具

#虚拟总端

#为了不让一个任务意外中断

#实用场景:需求:执行一个脚本,执行时间很长,而且脚本就会输出一些东西,所以脚本不能中途中断,通过远程连接时,难免网络会波动导致断开,从而脚本中断

方法一:将脚本丢到后台,让输出信息输出到日志

nohup 执行命令+日志 &

缺点:没法实时查看任务输出的信息

方法二:用screen

先screen回车进入虚拟总端,然后执行命令,ctral a组合键再按d就能退出总端但命令还在执行

screen用法:

screen直接回车就进入了虚拟终端

ctrl +]花括号退出虚拟终端,但不是结束

screen -ls 查看虚拟终端列表

screen -r id 进入指定的终端

screen -S  名字  命名虚拟终端

#screen是一个虚拟终端

yum install -y screen             #安装screen

screen直接回车就进入了虚拟终端

ctrl +]花括号退出虚拟终端,但不是结束域名

[root@xinlinux-02 ~]# screen

[screen is terminating]                           #退出终端提示

[root@xinlinux-02 ~]# screen

[detached from 1527.pts-0.xinlinux-02]

在总端里面输入exit可直接杀死该终端

screen -ls    #查看虚拟终端列表

[root@xinlinux-02 ~]# screen -ls

There is a screen on:

    1527.pts-0.xinlinux-02    (Detached)

1 Socket in /var/run/screen/S-root.

screen -r id   #进入指定的终端, id指的是终端号

[root@xinlinux-02 ~]# screen -r 1527

[detached from 1527.pts-0.xinlinux-02]

screen -r 终端名    #也可进入终端

screen -S aming   #修改终端名,需要进入该终端

10.36  xargs用法详解(重点)http://blog.csdn.net/zhangfn2011/article/details/6776925

#用的最多-i,和管道符连用

#exec 和 xargs区别

exec不用管道符,xargs要跟管道符连用   (格式不同,效果一样)

1.xargs简介

之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如:

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

find /sbin -perm +700 |xargs ls -l   这样才是正确的

xargs 可以读入 stdin 的资料,并且以空白字元或断行字元作为分辨,将 stdin 的资料分隔成为 arguments 。 因为是以空白字元作为分隔,所以,如果有一些档名或者是其他意义的名词内含有空白字元的时候, xargs 可能就会误判了~他的用法其实也还满简单的!就来看一看先!

2. 选项解释

-0 当sdtin含有特殊字元时候,将其当成一般字符,想/'空格等

例如:root@localhost:~/test#echo "//"|xargs  echo

      root@localhost:~/test#echo "//"|xargs -0 echo

       /

-a file 从文件中读入作为sdtin,(看例一)

-e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。(例二)

-p 当每次执行一个argument的时候询问一次用户。(例三)

-n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。(例四)

-t 表示先打印命令,然后再执行。(例五)

-i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替。(例六)

-r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。(例七)

-s num 命令行的最好字符数,指的是xargs后面那个命令的最大命令行字符数。(例八)

-L  num Use at most max-lines nonblank input lines per command line.-s是含有空格的。

-l  同-L

-d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符(例九)

-x exit的意思,主要是配合-s使用。

-P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。

3. 应用举例

例一:

root@localhost:~/test#cat test

#!/bin/sh

echo "hello world/n"

root@localhost:~/test#xargs -a test echo

#!/bin/sh echo hello world/n

root@localhost:~/test#

例二:

root@localhost:~/test#cat txt

/bin tao shou kun

root@localhost:~/test#cat txt|xargs -E 'shou' echo

/bin tao

root@localhost:~/test#

例三:

root@localhost:~/test#cat txt|xargs -p echo

echo /bin tao shou kun ff ?...y

/bin tao shou kun ff

例四:

root@localhost:~/test#cat txt|xargs -n1 echo

/bin

tao

shou

kun

root@localhost:~/test3#cat txt|xargs  echo

/bin tao shou kun

例五:

root@localhost:~/test#cat txt|xargs -t echo

echo /bin tao shou kun

/bin tao shou kun

例六:

$ ls | xargs -t -i mv {} {}.bak

例七:

root@localhost:~/test#echo ""|xargs -t mv

mv

mv: missing file operand

Try `mv --help' for more information.

root@localhost:~/test#echo ""|xargs -t -r  mv

root@localhost:~/test#

(直接退出)

例八:

root@localhost:~/test#cat test |xargs -i -x  -s 14 echo "{}"

exp1

exp5

file

xargs: argument line too long

linux-2

root@localhost:~/test#

例九:

root@localhost:~/test#cat txt |xargs -i -p echo {}

echo /bin tao shou kun ?...y

root@localhost:~/test#cat txt |xargs -i -p -d " " echo {}

echo /bin ?...y

echo tao ?.../bin

y

echo shou ?...tao

再如:

root@localhost:~/test#cat test |xargs -i -p -d " " echo {}

echo exp1

exp5

file

linux-2

ngis_post

tao

test

txt

xen-3

?...y

root@localhost:~/test#cat test |xargs -i -p echo {}

echo exp1 ?...y

echo exp5 ?...exp1

y

echo file ?...exp5

y

猜你喜欢

转载自blog.csdn.net/Lucky_LGX/article/details/87906079
今日推荐