问题描述
此脚本将按日期对文件进行排序,然后第2500个文件移动到另一个目录。结果
当我运行下面的脚本,系统提示出参数列表太长味精。任何人都可以帮助我提升脚本?谢谢
NUM_OF_FILES = 2500
FROM_DIRECTORY = /应用/ DATA01 / RAID / RC / MD / IN_MSC / ERC /中
DESTINATION_DIRECTORY = /应用/ DATA01 / RAID / RC / MD / IN_MSC / ERC / in_load如果[! -d $ DESTINATION_DIRECTORY]
然后
回声unused_file目录不存在!
MKDIR $ DESTINATION_DIRECTORY
回声$ DESTINATION_DIRECTORY目录中创建!
其他
回声$ DESTINATION_DIRECTORY存在!
科幻
回声移动$ NUM_OF_FILES旧的文件到$ DESTINATION_DIRECTORY目录LS -TR $ FROM_DIRECTORY / MSCERC * .Z |头 - $ NUM_OF_FILES |
xargs的-i SH -cMV {} $ DESTINATION_DIRECTORY
您不说,但我认为这是哪里出现问题:
LS -tr $ FROM_DIRECTORY / MSCERC * .Z |头-2500 | \\
xargs的-i SH -cMV {} $ DESTINATION_DIRECTORY
(您可通过添加设置-x你的脚本的顶部验证它。)
问题是,内核给一个新的进程的命令行的总长度的一个固定的最大大小,你超过了在 LS
命令。你可以解决它不使用通配符,而是使用的grep
:
LS -tr $ FROM_DIRECTORY / | grep的/MSCERC\\*\\.Z$'|头-2500 | \\
xargs的-i SH -cMV {} $ DESTINATION_DIRECTORY
(的grep
使用常规的前pressions而不是水珠,这样的模式看起来有点不同。)
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"
(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.)
这篇关于参数列表太长 - 的Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!