背景:Linux下使用cp,mv,rm等命令时报错“Argument list too long”
原因:要删除的文件个数过多,即命令的参数太长

处理:使用find 和xargs 命令操作
image.png

1. 删除/tmp/目录下以.txt结尾的文件

  1. find /tmp/ -name "*.txt" | xargs -i rm {}

2. 拷贝/tmp/目录下以.txt结尾的文件到/tmp/test目录下

find /tmp/ -name "*.txt" | xargs -i cp {} ./test

image.png

备注:由于目录下文件太多导致ls 命令无法使用时,可以使用如下命令查看目录情况:
[root@hadoop100 tmp]# find ./ -name “*.txt” -type f | xargs -n 1 ls (看文件)

[root@hadoop100 tmp]# find ./ -name “*” -type d (看目录)