How to create tar file with 7zip - tar

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.

Related

command line to convert all .docx in a directory (and subdirectories) to text file and write new files

I would like to convert all .docx files in a directory (and subdirectories) to text files from the command line (so I can use grep after on these files). I found this
unzip -p tutu.docx word/document.xml | sed -e 's/<\/w:p>/\n/g; s/<[^>]\{1,\}>//g; s/[^[:print:]\n]\{1,\}//g'
here which works well but it sends the file in the terminal. I would like to write the new text file (.txt for instance) in the same directory as the .docx file. And I would like a script to do this recursively.
I have this, using antiword, that do what I want for .doc files but it doesn't work for .docx files.
find . -name '*.doc' | while read i; do antiword -i 1 "${i}" >"${i/doc/txt}"; done
I tried to mix both but without success... A command line that would do both at the same time would be appreciated!
Thank you
You can use pandoc to convert docx files. It doesn't support .doc files so you will need both pandoc and antiword.
Reusing your while loop:
find . -name '*.docx' | while read i; do pandoc --from docx --to plain "${i}" >"${i/docx/txt}"; done
The following script..
converts all docx files in the directory where you run it, recursively (adapt . in find . to your wished starting point)
writes the txt files to where it found the docx file
Bash script:
find . -name "*.docx" | while read file; do
unzip -p $file word/document.xml |
sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g' > "${file/docx/txt}"
done
Afterwards you can run the grep like this:
grep -r "some text" --include "*.txt" .

How do I untar a tar file containing the tar program?

I have downloaded tar-latest.tar.gz from gnu. I have unzipped it; now I have to untar it. But how can I do that, when the only tar program I have is the one in tar-latest?
Use the command
tar -xvf filename.tar
Reference : http://www.tecmint.com/18-tar-command-examples-in-linux/
You can download tar for windows not packed in tar archive from http://gnuwin32.sourceforge.net/packages/gtar.htm
Binaries Setup 1331695 3 October 2003
http://gnuwin32.sourceforge.net/downlinks/tar-bin.php
or tar for ms-dos (exe): http://ftp.gnu.org/gnu/tar/tar-1.12.msdos.exe
or package which includes tar: http://unxutils.sourceforge.net/
or any freeware archiver capable of opening tar archives: 7-zip.org, peazip.org
(Best and easier way is 7-zip.org or peazip.org)

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.

extract a file from xz file

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

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.

Resources