Linux 删除大量的文件和移动大量的文件(Argument list too long)

本文介绍了解决Linux中使用mv命令时遇到的参数列表过长问题,提供了三种实用的方法:通过字母范围分组文件、利用find命令结合-exec选项以及自定义函数实现批量移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

find ./ -name '*' | xargs -i mv {}  ../test/

 

find ./ -name '*' | xargs rm -rf

 

2.Question :

 

    [user@localhost directory]$ mv * ../directory2

    bash: /bin/mv: Argument list too long

#Method1 : when file name can be distributed across the alphabet.
[user@localhost directory]$ mv [a-l]* ../directory2
[user@localhost directory]$ mv [m-z]* ../directory2

#Method2: using find and exec commands
[user@localhost directory]$ find $directory -type f -name '*' -exec mv {} $directory2/. /;
find $directory -type f -name '*' -exec scp {} username@servername:/$path/. /;

#Method3: using function
function large_mv ()
{ while read line1; do
mv directory/$line1 ../directory2
done
}
ls -1 directory/ | large_mv


### 关于 `/bin/tar` 中 `Argument list too long` 的解决方案 当尝试使用 `/bin/tar` 处理大量文件时,可能会遇到 `Argument list too long` 错误。这是因为命令行参数长度受到系统限制(通常由 `ARG_MAX` 定义)。以下是几种有效的解决方法: #### 方法一:分批处理 可以利用工具如 `find` `xargs` 将文件列表分成多批次传递给 `tar` 命令执行[^1]。 ```bash find /path/to/files -type f -print0 | xargs -0 tar cvf archive.tar ``` 上述命令通过 `-print0` `-0` 参数支持包含空格或其他特殊字符的文件名,并将它们分批传递给 `tar` 进行打包操作。 --- #### 方法二:直接读取文件列表 如果已知目标路径下的所有文件名称,则可以通过生成一个临时文件来存储这些文件名并将其作为输入提供给 `tar`[^2]。 ```bash find /path/to/files -type f > filelist.txt tar cf archive.tar --files-from=filelist.txt rm filelist.txt ``` 此方式避免了一次性向命令行传入过长参数的问题,而是让 `tar` 自己去读取指定文件中的条目清单。 --- #### 方法三:调整环境变量或重新编译内核 (不推荐) 理论上也可以增加系统的 `ARG_MAX` 上限值以允许更长的命令行参数,但这需要修改操作系统配置甚至重编译内核,在实际应用中并不常见也存在风险[^3]。 对于大多数情况来说,采用前两种基于现有工具链的方法更为安全可靠。 --- #### 方法四:编写自定义脚本 另一种思路是创建简单的 shell 脚本来逐个添加文件至压缩包当中[^4]: ```bash #!/bin/bash archive="my_archive.tar" touch "$archive" for file in $(find . -maxdepth 1); do [[ ! -d $file ]] && echo "Adding $file..." tar rvf "$archive" "$file" done ``` 注意这里仅示范了一个基本框架,具体实现可能还需要考虑更多边界条件比如子目录递归等问题。 --- 以上就是针对 `/bin/tar Argument list too long` 出现的情况所提供的多种可行对策介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值