参数列表太长 - Unix [英] Argument list too long - Unix

查看:27
本文介绍了参数列表太长 - Unix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此脚本将按日期对文件进行排序,然后将前 2500 个文件移动到另一个目录.
当我运行下面的脚本时,系统提示参数列表太长 msg.任何人都可以帮助我增强脚本?谢谢

This scripts will sort the files by date then move the first 2500 files to another directory.
When I run below scripts, system prompt out Argument list too long msg. Anyone can help me enhance the scripts ? Thanks

NUM_OF_FILES=2500
FROM_DIRECTORY=/apps/data01/RAID/RC/MD/IN_MSC/ERC/in
DESTINATION_DIRECTORY=/apps/data01/RAID/RC/MD/IN_MSC/ERC/in_load

if [ ! -d $DESTINATION_DIRECTORY ]  
        then  
                echo "unused_file directory does not exist!"  
        mkdir $DESTINATION_DIRECTORY   
        echo "$DESTINATION_DIRECTORY directory created!"  
else   
        echo "$DESTINATION_DIRECTORY exist!"    
fi  


echo "Moving $NUM_OF_FILES oldest files to $DESTINATION_DIRECTORY directory"  

ls -tr  $FROM_DIRECTORY/MSCERC*.Z|head -$NUM_OF_FILES |
    xargs -i sh -c "mv {} $DESTINATION_DIRECTORY"  

推荐答案

你没有说,但我认为这是问题发生的地方:

You didn't say, but I assume this is where the problem occurs:

ls -tr  $FROM_DIRECTORY/MSCERC*.Z|head -2500 | 
    xargs -i sh -c "mv {} $DESTINATION_DIRECTORY"  

(您可以通过在脚本顶部添加set -x"来验证它.)

(You can verify it by adding "set -x" to the top of your script.)

问题是内核有一个固定的最大命令行总长度给一个新进程,你在 ls 命令中超过了这个长度.您可以通过不使用通配符而是使用 grep 来解决它:

The problem is that the kernel has a fixed maximum size of the total length of the command line given to a new process, and your exceeding that in the ls command. You can work around it by not using globbing and instead using grep:

ls -tr  $FROM_DIRECTORY/ | grep '/MSCERC*.Z$' |head -2500 | 
    xargs -i sh -c "mv {} $DESTINATION_DIRECTORY"  

(grep 使用正则表达式而不是 globs,所以模式看起来有点不同.)

(grep uses regular expressions instead of globs, so the pattern looks a little bit different.)

这篇关于参数列表太长 - Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆