Linux: Adding named files to a zip archive, from a pipe - linux

Is it possible to use something like:
command.exe | zip >> archive.zip
command2.exe | zip >> archive.zip
...and end up with two named files inside one zip archive.
This way, if at all possible, would be neater than having temp files.

Create two named pipes in a new dir (with mkfifo), pipe the output of the commands to these two pipes and then zip the dir.
mkdir tmp
mkfifo tmp/1.out
mkfifo tmp/2.out
command1.exe > tmp/1.out
command2.exe > tmp/2.out
zip -FI -r tmp.zip tmp/
EDIT: Added the FI flag to zip, which does make this possible. The only caveat is that you need zip 3.0 for this to work. Tar:ing FIFO:s is not implemented (according to tar devs) because you need the file size in advance in order to write it to the TAR header.

Use fuse, fuze-zip rather.

Related

Sort files according to their filetype

After an HD problem and some work, I have a bunch of files with names like "f1234", "f1235", etc.
My goal is to sort this files according to their filetype. For example, I want to move all the PDF files in the "pdfs" directory.
For one file, I can do : "file f1234", and if it's a PDF, I can "mv f1234 pdfs/". But I have thousands of file... Can you help me with a bash or zsh command for sort all the PDF in one pass ? Thanks
The hard part here is reliably turning the output of file into a directory name. I think probably the best candidate for that is the mime-type of the file rather than the human readable output of file. I'd use something like:
mkdir sorted
for f in f*
do
d=$(file -b --mime-type "$f" | tr / -)
mkdir -p "sorted/$d"
mv "$f" "sorted/$d/"
done
Obviously I'd test that out a bit before running it on your files, but something pretty close to that should work.

bash: using unzip to change filename within archive

Given the following archive file20150101.zip, which contains only one file, file1115.txt, how would I go about renaming the file inside to match the name of the archive (file1115.txt to file20150101.txt)?
I've tried, unsuccessfully, to use unzip -u (most likely due to a misunderstanding of how it should work).
I doubt you can do this directly. You probably need to recreate the zip archive.
syncname() {
zip=$1
bare=${1%.zip}
new=$bare.txt
unzip "$zip"
mv file*.txt "$new"
rm "$zip"
zip "$zip" "$new"
}
syncname file20150101.zip
If you wanted to be more robust, handle zips with multiple files, etc. you could work on parsing the date stamp in the original name (with date) and finding the matching .txt file for that stamp.

print content of more than one file in a zip archive

I have some zip files that are really large and I want to print them without extracting first. I am using zcat and zless to do that and then I redirect the output to a different application. When my zip file contains more than one text file I receive the following error:
zcat tweets.zip >a
gzip: tweets.zip has more than one entry--rest ignored
How can I do what I want with zip files that contain more than one text file?
You can do this to output a file without extracting:
$ unzip -p <zip_file> <file_to_print>
For example:
$ unzip -p MyEar.ear META-INF/MANIFEST.MF
As cur4so mentioned you can also list all files using:
$ unzip -l <zip_file>
Use the -p option of unzip to pipe the output. Multiple files are concatenated. The -c option does the same thing, but includes the file name in front of each file.
If you just want to see a list of files in your zip archive use:
unzip -l tweets.zip
if you want to extract just some file:
unzip tweets.zip file-of-interest-as-it-is-pointed-in-the-archive
if you want something else, could you clarify your question?

Combine files in one

Currently I am in this directory-
/data/real/test
When I do ls -lt at the command prompt. I get like below something-
REALTIME_235000.dat.gz
REALTIME_234800.dat.gz
REALTIME_234600.dat.gz
REALTIME_234400.dat.gz
REALTIME_234200.dat.gz
How can I consolidate the above five dat.gz files into one dat.gz file in Unix without any data loss. I am new to Unix and I am not sure on this. Can anyone help me on this?
Update:-
I am not sure which is the best way whether I should unzip each of the five file then combine into one? Or
combine all those five dat.gz into one dat.gz?
If it's OK to concatenate files content in random order, then following command will do the trick:
zcat REALTIME*.dat.gz | gzip > out.dat.gz
Update
This should solve order problem:
zcat $(ls -t REALTIME*.dat.gz) | gzip > out.dat.gz
What do you want to happen when you gunzip the result? If you want the five files to reappear, then you need to use something other than the gzip (.gz) format. You would need to either use tar (.tar.gz) or zip (.zip).
If you want the result of the gunzip to be the concatenation of the gunzip of the original files, then you can simply cat (not zcat or gzcat) the files together. gunzip will then decompress them to a single file.
cat [files in whatever order you like] > combined.gz
Then:
gunzip combined.gz
will produce an output that is the concatenation of the gunzip of the original files.
The suggestion to decompress them all and then recompress them as one stream is completely unnecessary.

How to check for an exploding zip file in bash?

I have a bash shell script that unzips a zip file, and manipulates the resulting files. Because of the process, I expect all the content I am interested to be within a single folder like so:
file.zip
/file
/contentFolder1
/contentFolder2
stuff1.txt
stuff2.txt
...
I've noticed users on Windows typically don't create a sub folder but instead submit an exploding zip file that looks like:
file.zip
/contentFolder1
/contentFolder2
stuff1.txt
stuff2.txt
...
How can I detect these exploding zips, so that I may handle them accordingly? Is it possible without unzipping the file first?
If you want to check, unzip -l will print the contents of the zip file without extracting them. You'll have to massage the output a bit, though, since it's printing all sorts of additional crud.
Unzip to a directory first, and then remove the extra layer if the zip is not a bomb.
tempdir=`mktemp -d`
unzip -d $tempdir file.zip
if [ $(ls $tempdir | wc -l) = 1 ]; then
mv $tempdir/* .
rmdir $tempdir
else
mv $tempdir file
fi
I wouldn't try to detect it. I'd just force unzip to do what I want. With InfoZip:
$ unzip -j -d unzip-output-dir FileFromUntrustedSource.zip
-j makes it ignore any directory structure within the file, and -d tells it to put files in a particular directory, creating it if necessary.
If there are two files with the same name but in different subdirectories, the above command will make unzip ask if you want to overwrite the first with the second. You can add -o to force it to overwrite without asking, or -f to only overwrite if the second file is newer.

Resources