I want to find a tar file which is presented in any directory and un-archive it in a one line command. I am able to find it separately from home directory. but only can extract it from directory where it is located. How can I do both in a single line using pipe?

find -name any.tar ; tar xf any.tar I tried with this.. I can able to find this any.tar file from any directory separately. And tar xf is also extracting the file sparately but only in the dir where any.tar is located. I want run both command in a single line which can find the any.tar and extract it when the command is compiled from home directory.

I am entirely new to Unix please help me out.Thank you.

  • Show us what you can do. If you show that you made an effort you will get more help. Also it may help us to see what you are trying to do, and prevent down votes. Commented Sep 6, 2014 at 22:08
  • Fix the question, instead of adding comments to explain itp Commented Sep 7, 2014 at 9:02

3 Answers 3


This might help

find ./ -name '*.tar' -exec sh -c 'dir=$(dirname "$0"); tar -xvf "${0}" -C "${dir}"; done' {} \;

From man page of tar

-C, --directory DIR
       change to directory DIR

I found this page searching for the same question, the suggestion didn't directly work for me, but tweaking it a little bit worked for my use case:

find . -name "*.tar" -exec sh -c 'tar xvf {} -C $(dirname {})' \;

First is find:

To find all tar files in a directory you will need find $directory -iname "*.tar" Note: -iname is a gnu extension, so if you are not using gnu you will have to do -name *.tar -o -name *.TAR this is almost equivalent.

Now to add your working tar command:

find $directory -iname "*.tar" -print0 | xargs -0 --max-args=1 tar xf

This will extract all the tar files into the current working directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .