extract a file from xz file - tar

I have a huge file file.tar.xz containing many smaller text files with a similar structure. I want to quickly examine a file out of the compressed file and have a glimpse of files content structure. I don't have information about names of the files within the compressed file. Is there anyway to extract a single file out given the above the above scenario?
Thank you.
EDIT: I don't want to tar -xvf file.tar.xz.

Based on the discussion in the comments, I tried the following which worked for me. It might not be the most optimal solution, the regex might need some improvement, but you'll get the idea.
I first created a demo archive:
cd /tmp
mkdir demo
for i in {1..100}; do echo $i > "demo/$i.txt"; done
cd demo && tar cfJ ../demo.tar.xz * && cd ..
demo.tar.xz now contains 100 txt files.
The following lists the contents of the archive, selects the first file and stores the path within the archive into the variable firstfile:
firstfile=`tar -tvf demo.tar.xz | grep -Po -m1 "(?<=:[0-9]{2} ).*$"`
echo $firstfile will output 1.txt.
You can now extract this single file from the archive:
tar xf demo.tar.xz $firstfile

Related

How to create tar file with 7zip

I'm trying to create a tar file on windows using 7zip.
Most of the documents I found said to do something like this:
7z a -ttar -so dwt.tar dwt/
But when I tried to run it I got this error:
Command Line Error:
I won't write compressed data to a terminal
I'm currently using 7-Zip [64] 16.04
Any idea?
On Linux:
tar cf - <source folder> | 7z a -si <Destination archive>.tar.7z
from here
On Windows:
7za.exe a -ttar -so archive.tar source_files | 7za.exe a -si archive.tgz
from here.
I managed to do that making simply, with 7zip installed:
Right click on the folder you want to compress
Choose -7zip/add to file
Once there, on the new screen, on file type, you can choose 7z/tar/wim/zip
Choose tar, and there you go :)
From the manpage:
-so Write data to stdout (e.g. 7z x -so directory.tar.7z | tar xf -)
It does what you told it to. 7z can guess archive format from the file extension so it's enough to use
7z a archive.tar input/
To further compress as gzip you can use a pipe and a combination of stdin and stdout flags like in Tu.Ma.'s answer.

Tar does not exclude

I use
tar hczf t.tar.gz * --exclude="./test1"
where test1 is the name of a directory to exclude files from being tarred.
Unfortunately, tar still includes those directories. How can I have tar exclude directories?
The * that specifics "all files in the current directory" should be the last item on your cmd-line
tar --exclude="./test1" hczf t.tar.gz *
#--------------------------^-> tarFileName
#------------------------->f (for file)
This illustrates why the --excl... can't go inbetween hczf t.tar.gz.
The f option expects a filename right after it. So we have moved --excl... to the first option.
IHTH

How could I untar all .tar files in a directory to folders based on filename of each .tar?

I could do this for .zip files in the folder using the command below:
for f in "!"; do unzip -d "${f%*.zip}" "$f"; done
The above command extracts all .zip files in a given folder to subfolders, having content and name of respective .zip files.
But I couldn't find a command that would do the same for .tar files. Please help.
Btw, I am trying to do this on a remote server using WinSCP/putty. So, I cannot use a GUI software. I need a command, thus the question.
After a bit of fiddling I came up with for f in $(find -maxdepth 1 | grep .tar); do mkdir ${f%.tar}; tar -xaf $f -C ${f%.tar} ; done appears to work, so long as the file name does not contain any spaces. I assume you wanted the directory from foo.tar to be named foo (no file extension). If you want the directory to be named foo.tar (with file extension) then try using for f in $(find -maxdepth 1 | grep .tar); do mkdir $f ; tar -xaf $f -C $f ; done.
IIRC, the remote access client Cyberduck can handle compressed files in a GUI - so you can try that if you're fine with a GUI solution.

How to tar my C++ files on a linux server?

I know this is a simple question, but I'm not sure why the tar process isn't working and I can't find a definitive answer on here. When doing the tar command:
tar -cvjf<assign2comp.tar.bz2> <assign2.cpp header.cpp header.h>
I'm getting the error, Missing name for redirect.
Our professor shows this code as the example so I'm not sure what I'm doing wrong.
tar -cvjf<filename.tar.bz2> <files you want in archive>
Then to extract:
tar -jxvf filename.tar.bz2
So I want to archive the assign2.cpp header.cpp and header.h files then test the extract command to make sure I can access them.
Thanks for the help!
drop the < and > characters from commands. e.g. tar -cvjf assign2comp.tar.bz2 assign2.cpp header.cpp header.h
When you were told to use <file> it means to write your filename there, without < and >. So:
tar -cvjf assign2comp.tar.bz2 assign2.cpp header.cpp header.h
This is a common convention on Unix: <file> means a filename is required, [host] means a host is optional, and so on.

Unpack tar.gz folder to part of filename

I have a file dagens_130325.tar.gz containing the folder dagens. In one folder I have hundreds of these daily info. I would like to unpack dagens_130325.tar.gz/dagens to 130325 with all the files inside. Then 130326 etc.
Is there a way to do it?
Not sure this is the right stack where to ask this kind of question, however try with
tar -zxvf dagens_130325.tar.gz -C /tmp/130325 dagens
This way, the folder dagens for the archive dagens_130325.tar.gz is going to be extracted into /tmp/130325. However, note that the target folder must exist, otherwise the command will fail
So, supposedly you have 4 archives in the form dagens_1.tar.gz, dagens_2.tar.gz, ..., you can write an extract.sh file containing
#!/bin/bash
for i in {1..4}
do
mkdir /tmp/$i
FILE="dagens_$i.tar.gz"
tar -zxvf $FILE -C /tmp/$i dagens
done
Having this file the execute permission, being in the same folder as your archives and executing it should produced the result you asked.
This was the solution I came up with in the end
#!/bin/bash
search_dir=/yourdir/with/tar.gz
for entry in "$search_dir"/*.tar.gz
do
substring=$(basename "$entry")
echo $substring
sub2=${substring:7:6}
tar -xvzf $substring
rm -rf $sub2
mv dagens $sub2
done
use
#!/bin/bash
for file in dagens_*.tar.gz
do
from=${file%_*} #removes chars after _
to=${file#*_} #removes chars before _
to=${to%.t*} #removes chars after .t (.tar.gz)
tar -zxf $file --show-transformed --transform "s/$from/$to/"
done

Resources