参数列表太长 - Unix
该脚本将按日期对文件进行排序,然后将前 2500 个文件移动到另一个目录。
当我运行下面的脚本时,系统提示 Argument list too long msg。任何人都可以帮助我增强脚本吗?谢谢
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"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你没有说,但我认为这就是问题发生的地方:(
你可以通过在脚本顶部添加“set -x”来验证它。)
问题是内核有一个固定的最大大小给新进程的命令行长度,并且超过了 ls 命令中的长度。您可以通过不使用通配符而使用
grep
来解决这个问题:(grep
使用正则表达式而不是通配符,因此模式看起来有点不同。)You didn't say, but I assume this is where the problem occurs:
(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 usinggrep
:(
grep
uses regular expressions instead of globs, so the pattern looks a little bit different.)更改
执行如下操作:
这使用 find 创建带有修改时间戳的文件列表,按时间戳排序,然后在将输出传递到
head
和xargs
编辑
另一种变体,应该与非 GNU utils 一起使用
Change
do something like the following:
This uses find to create a list of files with modification timestamps, sorts by the timestamp, then removes the unneeded field before passing the output to
head
andxargs
EDIT
Another variant, should work with non GNU utils
首先创建要处理的文件的备份列表。然后逐行读取备份文件并修复它。例如
First of create a backup list of the files to be treated. Then read the backup file line-by-line and heal it. For example
解决此问题的快速方法是更改为 $FROM_DIRECTORY,以便您可以使用(较短的)相对路径引用文件。
cd $FROM_DIRECTORY &&
ls -tr MSCERC*.Z|head -2500 |xargs -i sh -c "mv {} $DESTINATION_DIRECTORY"
如果您有太多匹配的文件,这也不是完全万无一失的。
A quick way to fix this would be to change to $FROM_DIRECTORY, so that you can refer the files using (shorter) relative paths.
cd $FROM_DIRECTORY &&
ls -tr MSCERC*.Z|head -2500 |xargs -i sh -c "mv {} $DESTINATION_DIRECTORY"
This is also not entirely fool-proof, if you have too many files that match.