How to tar an extension from a certain directory?

Im trying to make a tar file with only .txt file from a specific directory

tar -cvf test.tar *.txt

I have that part and tested it correctly but dont know where to put the path part of the command. I tried different placements...

How does this work for you?

find ./my_directory -name "*.txt" | tar -cf my_archive.tar -T -

What did you try? Where and how did it fail?
What do you get if you specify /path/to/target/*txt ?

1 Like

Thank you RudiC, I was using /path/path/"*.txt" and /path/path/*.txt
never used just *txt :o

--- Post updated at 06:11 PM ---

now I have a second issue with untar the tar file. I made a test directory and tried to untar my tar file

tar -xvf testtar.tar -C /empty
tar: /guest: Cannot open: No such file or directory

The directory exist and I have also tried;

tar -C /empty -xvf testtar.tar

How did you produce your tar file?
What is its contents? tar -tf testtar.tar

OK, so I can create the tar file and have only the selected file extensions I want put when I try and untar the file, I get an error..

tar -cvf ~/test/test/test.tar --wildcards ~/test/test/test/*.txt && tar -C ~/test/test/folder -xvf ~/test/test/test.tar

I can make the tar file but extracting the tar file to a different directory doesnt work with my code

--- Post updated at 07:35 PM ---

@MadeInGermany

tar -cvf ~/path/of/tar/test.tar --wildcards ~/path/of/files/*.txt 

when I use

tar -tf test.tar

it list all the .txt files from the path I put in my tar file

The ~ expands to an absolute path. If your tar is not GNU tar, then the leading / is not stripped off. And a relocation during extraction is impossible.
I would use cd and let the shell expand the wildcards *.txt (without quotes. I think that tar handles wildcards (in quotes) only at extraction, anyway).

(cd ~/path/of/files && tar -cvf ~/path/of/tar/test.tar *.txt)

Now tar -tf test.tar shows the files without path. So you can extract at any location you want, using cd or the -C option.

1 Like

So can I keep the original command for creating the tar and use the �cd� for the extraction? The problem with using full path is it is homework and when tested by instructor it is run in his environment and and not sure if they have same directories. May be home/instructor and not home/student...

Homework?
According to the rules of this forum you must put a homework question in the sub forum "Homework and Emergencies" -> "Homework & Coursework Questions".
You find it further down the The UNIX and Linux Forums - Free Tech Support main page.