I am currently trying to bundle certain files in my current directory into a tar archive, and then make it so the archive is in the parent of my current directory. I'm wondering if anyone could explain to me how to do this. I understand how to bundle the files I need, but I am confused as to how I can make it so the archive is in the parent.

Currently I am using the command:

$ tar -tvf something.tar $( find -name "*.txt")

as I need to bundle files with the .txt extension.

  • 1
    By the way, the question and answer assume GNU findutils is used, because it omits the (POSIX-required) parameter before -name. – Thomas Dickey Sep 11 '18 at 1:03
  • @ThomasDickey I'm going to assume that the command worked as the answer was accepted (it also worked in the environment where I tested) but I went ahead and added the find command with the parameters before -name at the end of my answer as you've made a good point. – Nasir Riley Sep 11 '18 at 12:05

You can do it one of a few ways:

tar cvf ../something.tar $( find -name "*.txt")

That will make it so that the files are put into something.tar in the directory right above.

A more long-winded way is this:

tar cvf /path/to/something.tar $( find -name "*.txt")

That uses the direct path to where you want to put the .tar file.

If you want actual compression:

tar cvfz something.tar.gz $( find -name "*.txt")

The z will put it into a .gz archive.

Basically, you want to use cvf or cfvz as the c will compress the contents into an actual archive. Using t as in your tvf command is to send the contents of the archive to standard output without extracting it which won't work as you haven't creating the .tar archive yet.

You may also need to escape the * depending on your environment:

tar cvf ../something.tar $( find -name "\*.txt")

Another update for anyone who may need this in the future:

For anyone else who wants to use this, if you don't have findutils installed, it will be necessary to include the path that you are searching before -name in the find command:

tar cvf ../something.tar $( find . -name "\*.txt")

tar cvf ../something.tar $( find /path/to/directory -name "\*.txt")

The first searches in the current directory and the second searches using the path to the directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.