Argument list too long - Unix

后端 未结 4 1970
时光说笑
时光说笑 2020-12-07 01:35

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. Any

4条回答
  •  有刺的猬
    2020-12-07 02:07

    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"  
    

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

    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 uses regular expressions instead of globs, so the pattern looks a little bit different.)

提交回复
热议问题