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.
Related
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.
another newbie question..please bear with me.
I have a multiple .tar.gz files that contain the same XX.log file ( Named the same in each .tar.gz file ).
I need to extract only that specific XX.log file from each .tar.gz file and then append them in on list file named DataByDate.csv
I've tried multiple ways to accomplish this in one line:
zcat /tmp/jhoney/DATA.2015-10-09* | tar --extract --file=XX.log | perl -lne '/.{0,0}2015-10-09.{0,30}/ $$ print $&' >/tmp/jhoney/DataByDate.csv
This returns the error :
tar: XX.log: Cannot open:No such file or directory
tar: Error is not recoverable: existing now.
Any idea's?
You need to read man tar. I think you need something more like this:
for t in /tmp/jhoney/DATA.2015-10-09*;do tar -zxOf $t XX.log |
perl -lne '/.{0,0}2015-10-09.{0,30}/ && print $&';done >/tmp/jhoney/DataByDate.csv
Also, {0,0} doesn't seem to make sense. And if you really meant "append", the redirect should maybe be >> instead of just >.
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?
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.
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.