Bash 使用Shell脚本自动将.tar文件提取到当前目录

Bash 使用Shell脚本自动将.tar文件提取到当前目录,bash,shell,loops,extract,tar,Bash,Shell,Loops,Extract,Tar,我对bash完全陌生,目前正在编写一个脚本,该脚本将在一个大目录中循环,并将找到的任何.tar文件提取到它的当前位置 我正在使用以下脚本: for a in in /home/davidwright/attachments/*/*.tar do echo "extracting $x" tar -xvf $x done 当前文件正在提取,但正在提取到脚本所在的位置。我需要在.tar的当前目录中提取它 这个解决方案可能很简单,但我一辈子都想不出来。 谢谢 您可以尝试: -C, --dir

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

我正在使用以下脚本:

for a in in /home/davidwright/attachments/*/*.tar
do
  echo "extracting $x"
  tar -xvf $x
done
当前文件正在提取,但正在提取到脚本所在的位置。我需要在.tar的当前目录中提取它

这个解决方案可能很简单,但我一辈子都想不出来。 谢谢

您可以尝试:

-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

工作完美。我没想到用这个名字。谢谢