我正在使用以下行将html文件从源目录复制到目标目录。在将文件移动到001.html, 002.html, 003.html
etc时如何重命名文件?
find ${SourceDir} -type f -regex ".*\.\(htm\|html\|xhtm\|xhtml\)" -exec mv {} "${TargetDir}" \;
您可以在循环中使用计数器,并使用shell参数扩展来获取文件扩展名。
以下内容可能对您有用:
i=0
while read -r file; do
fn=$(printf "%03d" $((++i))) # get incremental numbers: 001, 002, ...
mv "${file}" "${TargetDir}/${fn}.${file##*.}";
done < <(find ${SourceDir} -type f -regex ".*\.\(htm\|html\|xhtm\|xhtml\)")
如果您的外壳程序不支持进程替换,您可能会说:
i=0
for file in $(find ${SourceDir} -type f -regex ".*\.\(htm\|html\|xhtm\|xhtml\)"); do
fn=$(printf "%03d" $((++i))) # get incremental numbers: 001, 002, ...
mv "${file}" "${TargetDir}/${fn}.${file##*.}";
done
请注意,如果文件名包含奇怪的字符,这可能不起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句