If you want to store all the files in folder
under their name without a directory indication, you can use the -j
option.
zip -j folder/artifact.zip folder/artifact1.app folder/artifact2.ext2 folder/artifact3.ext3
or
zip -j folder/{artifact.zip,artifact1.app,artifact2.ext2,artifact3.ext3}
If you have files in subdirectories, they won't have any directory component in the archive either, e.g. folder/subdir/foo
will be stored in the archive as foo
.
But it would probably be easier to change to the directory. It's almost exactly the same amount of typing as the brace method aboe. If you do that, if you include files in subdirectories, they'll have their relative path from the directory you changed into, e.g. folder/subdir/foo
will be stored as subdir/foo
.
(cd folder && zip artifact.zip artifact1.app artifact2.ext2 artifact3.ext3)
For interactive use, you lose shell completion this way, because the shell doesn't realize that you'll be in a different directory when you run the zip
command. To remedy that, issue the cd
command separately. To easily go back to the previous directory, you can use pushd
and popd
.
pushd folder
zip artifact.zip artifact1.app artifact2.ext2 artifact3.ext3
popd
If you want full control over the paths stored in a zip file, you can create a forest of symbolic links. Unless instructed otherwise, zip
doesn't store symbolic links.
With GNU awk
:
gawk '
BEGINFILE{search = "@code prefix." substr(FILENAME, 3, length(FILENAME) - 6)}
index($0, search)' ./*.py.txt
Would report the matching lines.
To print the file name and matching line, change index($0, search)
to
index($0, search) {print FILENAME": "$0}
Or to print the file name only:
index($0, search) {print FILENAME; nextfile}
Replace FILENAME
with substr(FILENAME, 3)
to skip outputting the ./
prefix.
The list of files is lexically sorted. The ones whose name starts with .
are ignored (some shells have a dotglob
option to add them back; with zsh
, you can also use the (D)
glob qualifier).
Best Answer
It is in general not possible to search for content within a compressed file without uncompressing it one way or another. Since zipgrep is only a shellscript, wrapping unzip and egrep itself, you might just as well do it manually:
If you need just the list of matching zip files, you can use something like:
This way you are only decompressing to stdout (ie. to memory) instead of decompressing the files to disk. You can of course try to just
grep -a
the zip files but depending on the content of the file and your pattern, you might get false positives and/or false negatives.