四种解决”Argument list too long”参数列表过长的办法

转自
http://hi.baidu.com/cpuramdisk/item/5aa49ce00c0757aecf2d4f24
 

在linux中删除大量文件时,直接用rm会出现:-bash: /bin/rm: 参数列表过长,的错误。

这时可以用find命令来结合使用。

例:
1、rm * -rf 改为:
find . -name "*" | xargs rm -rf '*' 就行了。

2、rm test* -rf 改为:
find . -name "test*" | xargs rm -rf "test*"

mv时报参数列表过长,

for i in *.m;do mv $i ${i%.m};done

于是求助于google,探索过程就省略了,直接说解决方法吧:

ls dir1 | xargs -t -I {} mv {} dir2/{}

这里的一对大括号是原文给的例子里用的,后来看了参数的用法,其实那对大括号是可以用任意字符串替换的,比如:

ls dir1 | xargs -t -I asdf mv asdf dir2/asdf

效果和大括号那一版是完全一样的,就是看起来有点儿不严肃。

需要说明的是,上面xargs的第二个参数,是大写的i,读作”爱“的那个字母,不是小写的L。至于参数的含义嘛,我忘了。

Linux下 报错“命令参数列表过长”,在用mv命令一次移动3万多个文件时失败了,原始命令类似这样:”mv $(ls dir1) dir2“。错误提示的中心思想是:”你这参数也太TM多了吧“。

按照LZ想法大概可以这么做:find /dir1/ -maxdepth 1 | xargs -i mv {} /dir2/
如果参数过长,使用tar比较简单
tar -C /dir1/ -cf - . | tar -xf - -C /dir2/

  于是求助于google,探索过程就省略了,直接说解决方法吧:

  ls dir1 | xargs -t -I {} mv {} dir2/{}

  这里的一对大括号是原文给的例子里用的,后来看了参数的用法,其实那对大括号是可以用任意字符串替换的,比如:

  ls dir1 | xargs -t -I asdf mv asdf dir2/asdf

  效果和大括号那一版是完全一样的,就是看起来有点儿不严肃。

  需要说明的是,上面xargs的第二个参数,是大写的i,读作”爱“的那个字母,不是小写的L。至于参数的含义嘛,我忘了。

另外4种方法

作为一个linux用户/系统管理员, 有些时候你会遇到以下错误提示:

[user@localhost foo]$ mv * ../foo2

bash: /bin/mv: Argument list too long

“Argument list too long”参数列表过长错误经常发生在用户在一行简单命令中提供了过多的参数而导致,经常在ls *, cp *, rm * 等中出现。
根据问题的原因以下提供了四种方法,可以根据自己的情况酌情选用

方法1 : 将文件群手动划分为比较小的组合
e.g 1:

[user@localhost foo]$ mv [a-l]* ../foo2

[user@localhost foo]$ mv [m-z]* ../foo2

这是最基本的方法,只是简单的使参数数量符合要求,这种方法应用范围有限,只适用于文件列表中的名字分布比较均匀,另外这也是个初级用户可以考虑的解决方案,不过需要很多重复命令和对文件名分布的观察与猜测。

方法2 : 使用find命令
e.g 2:

[user@localhost foo]$ find $foo -type f -name '*' -exec mv {}$foo2/. \;

方法2通过find命令,将文件清单输出到mv命令,使其一次处理一个,这样就完全避免了过量参数的存在,另外通过不同的参数,可以指定除了名称以外的时间戳,权限,以及inode等匹配模式。
方法2的缺点在于比较耗费时间。

方法3 : 创建shell函数
e.g 3.1:

function huge_mv ()
{whileread line1; do
mv foo/$line1 ../foo2
done
}
ls -1 foo/ | huge_mv

写一个shell函数并不涉及到某种程度的复杂性, 这种方法比方法1和方法2相比更加灵活。
下面我们来扩展一下例3.1 :
e.g 3.2:

function huge_mv ()
{whileread line1; do
md5sum foo/$line1 >> ~/md5sums
ls -l foo/$line1 >> ~/backup_list
mv foo/$line1 ../foo2
done
}
ls -1 foo/ | huge_mv

相比例3.1,例3.2生成了文件的md校验散列文件和名字备份,符合给自己留条后路的哲学。
另外可以根据自己的需要无限制的扩展功能。

方法4 : 终极解决方案,重新编译内核

首先使用这个方案之前要谨慎,因为涉及到修改内核源代码,在生产环境中还是要斟酌一下并做好测试。
另外,这个方法是从根本上解决的,一劳永逸的 
这也是开放源码的好处之一 
首先在内核源码中找到 include/linux/binfmts.h文件 ,搜索到以下字段:
/*
* MAX_ARG_PAGES defines the number of pages allocated for arguments
* and envelope for the new program. 32 should suffice, this gives
* a maximum env+arg of 128kB w/4KB pages!
*/
#define MAX_ARG_PAGES 32

修改 MAX_ARG_PAGES数值为 64 或者更高即可完善的解决参数受限问题。
然后 重新编译并启用新内核即可。

unable to execute /bin/mv: Argument list too long的更多相关文章

  1. Ubuntu下修改权限时出现"unable to execute /bin/chmod: Argument list too long"

    Ubuntu14.04下修改权限时出现"unable to execute /bin/chmod: Argument list too long"   ,大概意思是卧槽,你这参数也 ...

  2. CentOS中输入yum报错:sudo: unable to execute /bin/yum: No such file or directory

    今天尝试更新了下虚拟机CentOS中的python版本后. 运行“yum”命令,就报错:“sudo: unable to execute /bin/yum: No such file or direc ...

  3. E0264 Unable to execute '"/usr/bin/codesign" ...'

    E0264 Unable to execute '"/usr/bin/codesign" ...' http://docwiki.embarcadero.com/RADStudio ...

  4. 安装vmtools Error: Unable to execute "/usr/bin/vmware-uninstall-tools.pl.

    Error: Unable to execute "/usr/bin/vmware-uninstall-tools.pl. 安装vmware tools错误解决办法 很多朋友都在用vmwar ...

  5. Unable to execute dex: GC overhead limit exceeded

    Android打包时下面的错误: Unable to execute dex: GC overhead limit exceeded GC overhead limit exceeded 解决的方法: ...

  6. /bin/rm: Argument list too long解決方法

    rm.cp.mv是unix下面常用到的檔案處理指令,當我們需要刪除大量的log檔案,如果檔案數太多就會出現此訊息[/bin/rm: Argument list too long]解決方式如下: 例如要 ...

  7. 【转】Unable to execute dex: Java heap space 解决方案(如何为eclipse.int 添加内存)

    原文网址:http://blog.csdn.net/zengyangtech/article/details/7003379 欢迎转载,转载请注明 http://blog.csdn.net/zengy ...

  8. 【Android】Tips for Android developer: “Conversion to Dalvik format failed: Unable to execute dex: null”

    Androiddeveloper, I have met a strange problem when I want use a third party jar, it remained me tha ...

  9. 模拟器 Unable to execute simctl install Error 117

    ios 模拟器 Unable to execute '"/usr/bin/xcrun" simctl install  "/Users/tt/PAServer/scrat ...

随机推荐

  1. Spring的JDBC的使用(配置和CRUD)

    导包: Spring的JDBC模板的使用 一.默认连接池 创建数据库 create database spring4; use spring4; create table account(id int ...

  2. http error 502.5

    原文地址:https://www.cnblogs.com/loui/p/7826073.html 在部署网站时遇到的各种问题,通过检索找到了解决方案,感谢!!记录一下以免忘记.. 解决方法:把IIS的 ...

  3. maven项目使用mybatis+mysql

    1.添加依赖,在pom.xml中添加 <!--mybatis核心包--> <dependency> <groupId>org.mybatis</groupId ...

  4. esxi 主机用户功能说明

    1.  root 用户 默认情况下,每个 ESXi 主机都有一个具有管理员角色的 root 用户帐户.该 root 用户帐户可用于本地管理,并可用于将主机连接到 vCenter Server. 此公共 ...

  5. 【pwnable.kr】 uaf

    目测是比较接近pwnable的一道题.考察了uaf(use after free的内容),我觉得说白了就是指针没有初始化的问题. ssh uaf@pwnable.kr -p2222 (pw:guest ...

  6. 【pwnable.kr】blackjack

    又一道pwnable nc pwnable.kr 9009 读题找到源代码在:http://cboard.cprogramming.com/c-programming/114023-simple-bl ...

  7. 五、JavaScript之点击按钮调用相关JavaScript函数

    一.代码如下 二.执行之后 三.点击按钮之后,内容被改变

  8. vue学习(六)异步组件加载

    异步组件加载 首先准备-----简单的框架搭出来 <!DOCTYPE html> <html lang="zh-CN"> <head> < ...

  9. UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)

    题意:问k小时后,第A~B行一共有多少个红气球. 分析:观察图可发现,k小时后,图中最下面cur行的红气球个数满足下式: (1)当cur <= POW[k - 1]时, dfs(k, cur) ...

  10. 19 ~ express ~ 文章的增加 , 查看 ,修改 ,删除

    一,前台 1,添加文章 /views/admin/content_add.html {% extends 'layout.html' %} {% block main %} <ol class= ...