如何将第N个文件写入新文件夹

时间:2019-02-06 11:27:47

标签: bash shell scripting directory

我有这段代码可以扫描文件夹,并将每个文件夹中的所有文件移动到一个新文件夹中。

我如何做到只有第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}/*行,但是不确定语法是否正确

2 个答案:

答案 0 :(得分:0)

ID A1 A2 score1 7.5 15.0 score2 75.0 150.0 score3 750.0 1500.0 之后尝试使用此命令代替您的mv命令:

do

它将移动if ! ((frame % 5)); then a=$((frame / 5)); mv $f `printf "$outFolder/$namePrefix.%05d.$ext" $a` fi = 5,10,依次类推到frame$outFolder/$namePrefix.00001.$ext,依此类推

答案 1 :(得分:0)

如果列出的ND850文件夹中的文件是连续的(即,填充的帧号),并且文件夹本身是按顺序排列的,则下面的代码应该起作用。

#!/bin/bash

# Maintain a counter, and the output frame number
let "frame=1"
let "outframe=1"

outFolder="collected"

# frequency
gap=5

namePrefix="ben_timelapse"

#new extension (uppercase is so ugly)
ext="jpg"

srcPattern="ND850"

echo "Copying and renaming 1 in every $gap files"

mkdir -p "$outFolder"
for f in *${srcPattern}/*
do
if ! ((frame % $gap)); then
    outfile=`printf "$outFolder/$namePrefix.%05d.$ext" $outframe`
    cp $f "$outfile"
    echo "copied $f to $outfile"
    let "outframe++"
fi
let "frame++"
done