linux - 循环附加到 tar 文件

标签 linux shell

我有一个目录,大概有 6 个文件。

team1_t444444_jill.csv
team1_t444444_jill.csv
team1_t444444_jill.csv
team1_t999999_jill.csv
team1_t999999_jill.csv
team1_t111111_jill.csv
team1_t111111_jill.csv

我希望能够根据文件的 t 编号对每个文件进行 tar 处理,因此 t444444 应该有自己的包含所有相应 csv 的 tar 文件。然后 t999999 应该有自己的等等......总共应该动态创建三个 tar 文件

for file in $bad_dir/*.csv; do
  fbname=`basename "$file" | cut -d. -f1`  #takes the pathfile off, only shows xxx_tyyyyy_zzz.csv
  t_name=$(echo "$fbname" | cut -d_ -f2)   #takes the remaning stuff off, only shows tyyyyy

  #now i am stuck on how to create a tar file and send email
  taredFile = ??? #no idea how to implement

  (cat home/files/hello.txt; uuencode $taredFile $taredFile) | mail -s "Failed Files" $t_name@hotmail.com

最佳答案

应该执行您想要的操作的最简单的脚本编辑可能是这样的。

for file in $bad_dir/*.csv; do
  fbname=`basename "$file" | cut -d. -f1`  #takes the pathfile off, only shows xxx_tyyyyy_zzz.csv
  t_name=$(echo "$fbname" | cut -d_ -f2)   #takes the remaning stuff off, only shows tyyyyy

  tarFile=$t_name-combined.tar
  if [ ! -f "$tarFile" ]; then
      tar -cf "$tarFile" *_${t_name}_*.csv
      { cat home/files/hello.txt; uuencode $tarFile $tarFile; } | mail -s "Failed Files" $t_name@hotmail.com
  fi
done

使用基于输入文件名唯一位的 tar 文件名。然后在创建文件和发送电子邮件之前检查该文件是否存在(防止多次创建文件和多次发送电子邮件)。

利用文件可通配的事实,从我们看到的第一个文件开始使用 tar 将它们全部归档。

您还会注意到我将 (commands) 替换为 { commands; 在管道中。 () 强制一个子 shell 但管道本身也是如此,因此(在这种情况下)没有理由(在这种情况下)手动强制一个 extra 子 shell 只是为了分组效果.

关于linux - 循环附加到 tar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29545365/

相关文章:

c - 如果我们不关闭管道 : Linux/C 中使用过的一端会发生什么

shell - 如何将命令行中本地 `curl`的结果一行传到远程服务器?

linux - 将文本添加到多个文件夹中的多个文件的开头

linux - 如何检测shell脚本输出的行号

linux - 从不断更新的目录中读取更新

同时为 2 个文件夹编写 Linux bash 脚本

Java看门狗监控

Python - 文件名中的空格

c - 检索库运行时

linux - 处理器空闲时通知