There are about 10,000 files in the directory some of which are .fits and some of which are .tar. Now, I need to untar only the .tar files. Although running an if condition seems reasonable, the untar command "xvf" only works on a per-file basis, which defeats the use of a loop.


2 Answers 2


the untar command "xvf" only works on a per-file basis

which makes the use of loop necessary:

for file in ./*.tar; do tar xvf "$file"; done

Actually with find you can do the job without loops. Like this:

find -iname '*.tar' -maxdepth 1 -exec tar xvf '{}' \;

The logic is like this:

  • -iname matches files with extension .tar (case insensitive), and
  • -maxdepth 1 limits the matching range to depth 1 (so that ./*/*.tar and the like won't get matched)
  • finally -exec performs the tar xvf command on the matched files (note that ; and {} need escaping; refer to find(1) for more information)

You must log in to answer this question.

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