Store the name of an extracted file to a temp file

What would be the best way to store the name of an extracted file from a tar to a text file?

I want to extract one file from a tar and store the name of the extracted file to a temp file.

tar -xvf tar_file.tar file_to_be_extracted

It depends on the command used to create the tar archive. If

tar tf tar_file.tar | grep filename

produces something like: ./filename or ./dir1/dir2/filename then if you use EXACTLY what tar tf showed, this will work - I picked one arbitrary name: ./filename

cd /tmp
tar xf /path/to/tar_file.tar  ./filename
mv ./filename  /path/to/tempfilename

if tar tf shows some name of the file without a leading dot ex: /path/filename
Then the /path directory has to be writable by your process. And /path/filename should not exist because tar will overwrite it.

then try:

tar xf /path/to/tar_file.tar /path/filename
mv /path/filename /path/to/tempfilename
1 Like