【发布时间】:2019-06-30 08:16:01
【问题描述】:
我有这段代码可以扫描文件夹并将每个文件夹中的所有文件移动到一个新文件夹中。
我怎样才能做到只移动每个第 N 个文件?
#!/bin/bash
# Save this file in the directory containing the folders (bb in this case)
# Then to run it, type:
# ./rencp.sh
# The first output frame number
let "frame=1"
# this is where files will go. A new directory will be created if it doesn't exist
outFolder="collected"
# print info every so many files.
feedbackFreq=250
# prefix for new files
namePrefix="ben_timelapse"
#new extension (uppercase is so ugly)
ext="jpg"
# this will make sure we only get files from camera directories
srcPattern="ND850"
mkdir -p $outFolder
for f in *${srcPattern}/*
do
mv $f `printf "$outFolder/$namePrefix.%05d.$ext" $frame`
if ! ((frame % $feedbackFreq)); then
echo "moved and renamed $frame files to $outFolder"
fi
let "frame++"
done
很确定我需要编辑 for f in *${srcPattern}/* 行,但不确定语法是否正确
【问题讨论】:
-
在 bash 中,参数扩展是在文件名扩展之前完成的,所以你的模式,你可以用更紧凑的形式写成
*$srcPattern/*扩展到所有目录中的所有非隐藏条目,名称在ND850。请注意,如果您没有至少一个匹配此模式的条目,这将无法正常工作。 -
编号应该保持不变,即以 5 为增量(1, 6, 11, 16, ...)还是应该是连续的(1, 2, 3, 4, . ..)?另外,请引用您的变量。
-
输出编号应该是连续的
-
就像我需要一个 if 语句:如果 f 模数 5 = 0 .... mv $f .....
-
打印出偶尔信息的行是对帧号进行取模。实际副本需要类似的东西。
gap=8;if ! ((frame % $gap)); then; cp $frame $outfolder;fi
标签: bash shell scripting directory