Bash查找和移动从cvs文件数据解析的文件

Bash查找和移动从cvs文件数据解析的文件,bash,csv,find,Bash,Csv,Find,我们有一个文件目录,需要根据cvs文件中的文件名信息重命名并移动到另一个目录。 cvs文件的内容为: A10324687,ahgk1ff0023 A10324698,cru1ff0129 源文件夹具有以下层次结构: /Server/Archive/A/AHGK1/AHGK1FF0001_0200/ahgk1ff0023.pdf /Server/Archive/B/BFKR3/BFKR3FF0001_0200/bfkr3ff0018.ai /Server/Archive/B/BFKR3

我们有一个文件目录,需要根据cvs文件中的文件名信息重命名并移动到另一个目录。 cvs文件的内容为:
A10324687,ahgk1ff0023
A10324698,cru1ff0129

源文件夹具有以下层次结构:

/Server/Archive/A/AHGK1/AHGK1FF0001_0200/ahgk1ff0023.pdf    
/Server/Archive/B/BFKR3/BFKR3FF0001_0200/bfkr3ff0018.ai  
/Server/Archive/B/BFKR3/BFKR3FF0001_0200/bfkr3ff0018.pdf  
/Server/Archive/B/BFKR3/BFKR3LC0001_0200/bfkr3lc0018.pdf  
/Server/Archive/C/CRU1/CRU1FF0001_0200/cru1ff0129.ai  
/Server/Archive/C/CRU1/CRU1FF0001_0200/cru1ff0129.pdf
脚本的结果将移动和重命名

ahgk1ff0023.pdf to: /Server/Art/A10324687_ahgk1ff0023.pdf
文件名和路径中的字符数不同。 因此,我认为有必要使用一个find函数,从具有basename减去扩展名的csv文件中输入内容。 以下脚本在本地文件上运行良好,但在文件上运行时会爬网 来自网络共享。有什么建议吗

#!/bin/bash
date +"DATE: %a %m/%d/%Y  TIME: %r  Auto" >> /Volumes/Serve/Users/Admin/Logs/SKU.log
date +"DATE: %a %m/%d/%Y  TIME: %r  Auto" >> /Volumes/Serve/Users/Admin/Logs/SKU_pdf.log
date +"DATE: %a %m/%d/%Y  TIME: %r  Auto" >> /Volumes/Serve/Users/Admin/Logs/Stamped_PDFs.log
date +"DATE: %a %m/%d/%Y  TIME: %r  Auto" >> /Volumes/Serve/Users/Admin/Logs/Removed_PDFs.log
#Directory for source files to br moved and renamed
src=/Volumes/Serve/Archive
#Directory in which the files are move to
dest=/Volumes/Serve/Art/
IFS=','
while read new old; do
  ai=$(find $src -name $old.ai)
  pdf=$(find $src -name $old.pdf)
  #Following varible is used to stamp the pdf file with the "New" number from the csv input
  pdfout="$dest$new"_"$(basename $pdf)"
  stamper=/Users/Admin/Desktop/TMP/stamp.pdf
  ait=$(find $src -name $old.ait)
  eps=$(find $src -name $old.eps)
  tiff=$(find $src -name $old.tiff)
  tif=$(find $src -name $old.tif)
  psd=$(find $src -name $old.psd)
  fh8=$(find $src -name $old.fh8)
  fh9=$(find $src -name $old.fh9)
  noExt=$(find $src -name $old)

  #This is where the pdf files get stamped with the "New" number
  echo $new" -> "$old" -> "$pdfout$(basename $pdf) >> /Volumes/Serve/Users/Admin/Logs/SKU_pdf.log
  gs -q -sDEVICE=pdfwrite -o $stamper -c "<< /PageSize [792 612]  >> setpagedevice 18 586 moveto /Helvetica-Bold_Italic findfont 14 scalefont setfont ("$new") show"
  pdftk $pdf stamp $stamper output $pdfout verbose | grep .pdf$ >> /Volumes/Serve/Users/Admin/Logs/Stamped_PDFs.log
  rm -f -v $pdf >> /Volumes/Serve/Users/Admin/Logs/Removed_PDFs.log

  #The following moves the remaining file types if they exist.
  mv -v $ai $dest${new}'_'${old}.ai >> /Volumes/Serve/Users/Admin/Logs/SKU.log
  mv -v $ait $dest${new}'_'${old}.ait >> /Volumes/Serve/Users/Admin/Logs/SKU.log
  mv -v $eps $dest${new}'_'${old}.eps >> /Volumes/Serve/Users/Admin/Logs/SKU.log
  mv -v $tiff $dest${new}'_'${old}.tiff >> /Volumes/Serve/Users/Admin/Logs/SKU.log
  mv -v $tif $dest${new}'_'${old}.tif >> /Volumes/Serve/Users/Admin/Logs/SKU.log
  mv -v $psd $dest${new}'_'${old}.psd >> /Volumes/Serve/Users/Admin/Logs/SKU.log
  mv -v $fh8 $dest${new}'_'${old}.fh8 >> /Volumes/Serve/Users/Admin/Logs/SKU.log
  mv -v $fh9 $dest${new}'_'${old}.fh9 >> /Volumes/Serve/Users/Admin/Logs/SKU.log
  mv -v $noExt $dest${new}'_'${old} >> /Volumes/Serve/Users/Admin/Logs/SKU.log
done < /Users/Admin/Desktop/List.csv
#/bin/bash
日期+“日期:%a%m/%d/%Y时间:%r Auto”>>/Volumes/service/Users/Admin/Logs/SKU.log
日期+“日期:%a%m/%d/%Y时间:%r Auto”>>/Volumes/service/Users/Admin/Logs/SKU\u pdf.log
日期+“日期:%a%m/%d/%Y时间:%r Auto”>>/Volumes/service/Users/Admin/Logs/Stamped_PDFs.log
日期+“日期:%a%m/%d/%Y时间:%r Auto”>>/Volumes/service/Users/Admin/Logs/Removed_PDFs.log
#要br的源文件的目录已移动并重命名
src=/Volumes/service/Archive
#文件移动到的目录
目的地=/数量/服务/艺术/
如果s=','
边读新老;做
ai=$(查找$src-name$old.ai)
pdf=$(查找$src-name$old.pdf)
#以下变量用于在pdf文件上标记csv输入的“新”编号
pdfout=“$dest$new”\$(basename$pdf)”
stamper=/Users/Admin/Desktop/TMP/stamp.pdf
ait=$(查找$src-name$old.ait)
eps=$(查找$src-name$old.eps)
tiff=$(查找$src-name$old.tiff)
tif=$(查找$src-name$old.tif)
psd=$(查找$src-name$old.psd)
fh8=$(查找$src-name$old.fh8)
fh9=$(查找$src-name$old.fh9)
noExt=$(查找$src-name$old)
#这是pdf文件加盖“新”编号的地方
echo$new“->”$old“->“$pdfout$(basename$pdf)>>/Volumes/service/Users/Admin/Logs/SKU\u pdf.log
gs-q-sDEVICE=pdfwrite-o$stamper-c“>setpagedevice 18 586 moveto/Helvetica-Bold_Italic findfont 14 scalefont setfont($new”)show”
pdftk$pdf stamp$stamper输出$pdfout verbose | grep.pdf$>>/Volumes/service/Users/Admin/Logs/Stamped|PDFs.log
rm-f-v$pdf>>/Volumes/service/Users/Admin/Logs/Removed_PDFs.log
#下面将移动剩余的文件类型(如果存在)。
mv-v$ai$dest${new}''${old}.ai>>/Volumes/service/Users/Admin/Logs/SKU.log
mv-v$ait$dest${new}''${old}.ait>>/Volumes/service/Users/Admin/Logs/SKU.log
mv-v$eps$dest${new}'${old}.eps>>/Volumes/service/Users/Admin/Logs/SKU.log
mv-v$tiff$dest${new}''${old}.tiff>>/Volumes/service/Users/Admin/Logs/SKU.log
mv-v$tif$dest${new}''${old}.tif>>/Volumes/service/Users/Admin/Logs/SKU.log
mv-v$psd$dest${new}'${old}.psd>>/Volumes/service/Users/Admin/Logs/SKU.log
mv-v$fh8$dest${new}''${old}.fh8>>/Volumes/service/Users/Admin/Logs/SKU.log
mv-v$fh9$dest${new}''${old}.fh9>>/Volumes/service/Users/Admin/Logs/SKU.log
mv-v$noExt$dest${new}''${old}>>/Volumes/service/Users/Admin/Logs/SKU.log
完成
这可能适合您:

dest=/Path/To/File
sed 's|\(.*\),\(.*\)|/\\<\2\\>/s!.*\\(\2.*\\)$!mv -v \&\\t'$dest'/\1_\\1!p|' file.csv |
sed -nf - file_of_files.txt
mv -v /Server/Archive/A/AHGK1/AHGK1FF0001_0200/ahgk1ff0023.pdf  /Path/To/New/File/A10324687_ahgk1ff0023.pdf
mv -v /Server/Archive/C/CRU1/CRU1FF0001_0200/cru1ff0129.ai      /Path/To/New/File/A10324698_cru1ff0129.ai
mv -v /Server/Archive/C/CRU1/CRU1FF0001_0200/cru1ff0129.pdf     /Path/To/New/File/A10324698_cru1ff0129.pdf
dest=/Path/To/File
sed的| \(.*),\(..\)\/\\/s*\\(\2.*\\)$!mv-v\&\\t'$dest'/\1\\\1!p |'file.csv|
sed-nf-file\u of_files.txt
mv-v/Server/Archive/A/AHGK1/ahgk1ff001_0200/ahgk1ff0023.pdf/Path/To/New/File/A10324687_ahgk1ff0023.pdf
mv-v/Server/Archive/C/CRU1/cru1ff001_0200/cru1ff0129.ai/Path/To/New/File/A10324698_cru1ff0129.ai
mv-v/Server/Archive/C/CRU1/CRU1FF001_0200/cru1ff0129.pdf/Path/To/New/File/A10324698_cru1ff0129.pdf
这将获取一个
csv
文件,并生成在文件文件中移动文件的命令


注意:@shelleter已经指出,您需要重构代码,使用尽可能少的
find
来收集所有数据。一旦你有了一个文件列表,使用
sed
grep
过滤出相关文件就容易多了。然后,您可以使用
csv
文件和此列表生成命令。祝你好运

将查找减少为以所有文件名为目标的一次运行,即
find$src-name$old.psd-name$old.tif….
在读取文件名时将其发送到
|;是否在*.pdf中使用大小写${filename})…;*。tif);;esac
。如果您希望在文件名中包含空格或其他非字母数字,请参阅关于-print0选项以查找和xargs。这里有100条关于这个组合的帖子。祝你好运