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

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.

Related

Unix. Parse file with full paths to SHA256 checksums files. Run command in each path/file

I have a file file.txt with filenames ending with *.sha256, including the full paths of each file. This is a toy example:
file.txt:
/path/a/9b/x3.sha256
/path/7c/7j/y2.vcf.gz.sha256
/path/e/g/7z.sha256
Each line has a different path/file. The *.sha256 files have checksums.
I want to run the command "sha256sum -c" on each of these *.sha256 files and write the output to an output_file.txt. However, this command only accepts the name of the .sha256 file, not the name including its full path. I have tried the following:
while read in; do
sha256sum -c "$in" >> output_file.txt
done < file.txt
but I get:
"sha256sum: WARNING: 1 listed file could not be read"
which is due to the path included in the command.
Any suggestion is welcome
#!/bin/bash
while read in
do
thedir=$(dirname "$in")
thefile=$(basename "$in")
cd "$thedir"
sha256sum -c "$thefile" >>output_file.txt
done < file.txt
Modify your code to extract the directory and file parts of your in variable.

Bypass `-C` flag when unzipping tar archive

I have a .tar.gz archive with the following structure:
opt/client/py/utils/mappings/templates/config.json
opt/
opt/mag/
opt/mag/sw/
opt/mag/sw/apps/
opt/mag/sw/apps/service/
opt/mag/etc/
opt/mag/etc/service.environment
opt/mag/etc/service.toml
And this .tar.gz archive is extracted using this command:
tar -zxf ../service.tar.gz -C opt/mag/ --strip-components=2
Thee issue is this will extract the opt/client/py/utils/mappings/templates/config.json file inside opt/mag so it will become: opt/mag/py/utils/mappings/templates/config.json which is obviously wrong. Notice the removal of opt/client/ by --strip-components=2.
The issue is that I cannot change the unzip command
I've tried to hack it around by inserting 2 dummy directories to bypass --strip-components=2 something like this:
dummy1/dummy2/opt/client/py/utils/mappings/templates/config.json
opt/
opt/mag/
opt/mag/sw/
opt/mag/sw/apps/
opt/mag/sw/apps/service/
opt/mag/etc/
opt/mag/etc/service.environment
opt/mag/etc/service.toml
This way --strip-components=2 will remove dummy1/dummy2 and the config file will be extracted to /opt/mag/opt/client/py/utils/mappings/templates/config.json which is still wrong, because that -C opt/mag will force the extraction inside opt/mag.
Given the fact that I cannot change the unzip command is there anyway to bypass the -C switch or some way to hack it around?
Also, I cannot edit or move files on the container where this archive is extracted

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.

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

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