您当前的位置:周俊奇博客 > 项目

shell脚本批量将文件复制到指定的文件夹下

时间:2022-08-23 10:01:38

由于线上文件比较多,选择特定的文件拿下线下进行语料标注,如果指定的文件数量太多,一个个复制就很麻烦。所以写一个shell脚本进行批量操作。

首先把需要下载的文件路径写入txt文件中,如果需要路径补全,则在每条路径上加上*号,这样就很简单cd到对应的目录下,(就这个小点,花费了我很久时间)

如图所示:

095d36ac3eaf9da6d9b1e5af5eeda1b1_2022082215275029.jpg

然后就是遍历txt文件进行路径下操作,

cat 2022-05-07_path.txt | while read line
do
#echo $line
dir=根目录/"$line"
echo $file_path
filelist=`ls $dir`
for file in $filelist
do
tmp=${file##*/} #文件名
file_no_suffix=${tmp%.*} #文件名不带后缀
suffix=${file##*.} #后缀
#echo $tmp
#echo $file_no_suffix
if [[ $suffix != $file_no_suffix ]] && [[ $suffix == 'pdf' ]]; then
if [[ $file_no_suffix != 'source_file_dec' ]] && [[ $file_no_suffix != 'source_file_origin' ]] && [[ $file_no_suffix != 'source_file' ]]; then
#echo $tmp
#echo $file_no_suffix
file_path=$dir/$tmp
echo cp $file_path $target_dir
cp $file_path $target_dir
fi
fi
done
done

其中就是对文件进行逻辑判断进行选择需要的文件名称进行复制

标签: shell