【问题标题】:Extract .tar File to Current Directory Automatically with Shell Script使用 Shell 脚本自动将 .tar 文件提取到当前目录
【发布时间】:2013-06-11 12:23:43
【问题描述】:

我对 bash 世界完全陌生,我目前正在编写一个脚本,该脚本将遍历一个大目录并将找到的任何 .tar 文件提取到当前位置。

我正在使用以下脚本:

for a in in /home/davidwright/attachments/*/*.tar
do
  echo "extracting $x"
  tar -xvf $x
done

目前文件提取正常,但它正在提取到我的脚本的位置。我需要它在 .tar 的当前目录中提取。

解决方案可能很简单,但我一辈子都想不通。 谢谢!

【问题讨论】:

    标签: bash shell loops extract tar


    【解决方案1】:

    你可以试试:

    -C, --directory DIR
                  change to directory DIR
    

    $(dirname "$x") 用于文件目录

    for x in in /home/davidwright/attachments/*/*.tar
    do
      echo "extracting $x"
      tar -xvf "$x" -C "$(dirname "$x")"
    done
    

    【讨论】:

    • 完美运行。没想到要使用目录名。谢谢!
    猜你喜欢
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多