How to zip a directory in linux excluding a single file? - linux

I would like to zip a directory and I am able to do so with
zip -r zip_file_name directory
however, I would like exclude a single file in the directory from the zip file. How would I go about doing this?

Enter the directory which you want to zip. Then:
find . -not -name "file_to_exclude" | zip zip_file_name -#
The command above will create zip_file_name.zip in directory itself.
To create zip at a particular path, Enter the directory which you want to zip. Then:
find . -not -name "file_to_exclude" | zip ~/ParticularPath/zip_file_name -#
From linux man page for zip:
-# file lists. If a file list is specified as -# [Not on MacOS], zip takes the list of input files from standard input instead of from the command line. For example,
zip -# foo
will store the files listed one per line on stdin in foo.zip.
Under Unix, this option can be used to powerful effect in conjunction with the find command. For example, to archive all the C source files in the current directory and its subdirectories:
find . -name "*.[ch]" -print | zip source -#

Related

Shell - create zip files in directories without including subdirectories

I have a structure like this:
file1.jpg
file2.png
subfolder1
subfolder2
file3.jpg
...
now I would like to create a zip file via shell only of the files present in the root of the directory without taking into consideration the subdirectories and the files contained within them.
How can I do? I hope I have been clear enough
You can try something like this by using a find.
It will only find files that are in the current folder and zip it to a file called filename.zip
find . -maxdepth 1 -type f -exec zip filename.zip '{}' \;

How to zip VERY LONG list of files in environment variable: *** buffer overflow detected ***: zip terminated

I am copying files over from one machine to another. I am only interested in files that are more than N days old, so I have used find to create a list of filenames as follows:
DAYS_OLD=7
FILEs=`find /some/path -mtime -$DAYS_OLD | xargs`
Now I want to zip the files into one archive:
ZIPFILE='myfiles.zip'.
I run the following command:
zip -r $ZIPFILE "${FILEs}"
I get the following error:
* buffer overflow detected *: zip terminated
How can I zip the files (in the ${FILEs} environment variable) into a zip archive?
One way:
find /some/path -mtime -$DAYS_OLD | xargs zip -r $SOMEDIR/$ZIPFILE
Use find /some/path -mtime "$DAYS_OLD" -exec zip -r "$ZIPFILE" {} + to work with any valid file name, including those with newlines in their names.
Try with
zip $ZIPFILE -i#<(find /some/path -mtime -$DAYS_OLD)
the -i parameter tells zip to read the list from a file. The file will be simulated on the fly with the contents of the command.

How to zip a folder, but minus the files using zip command line

I am creating a zip file of my application tree, minus folders that have temporary files. For now I exclude the folders using -x option and manually created them with unzip. Is there a way with zip to exclude the files but include the folder (i.e. it would be an empty folder in the zip file?)
I am using
zip -r zipfile.zip . -x appsessions/\* workfolder/\*
but of course it excludes the folders and files in them. I would like to keep appsessions/ and workfolder/ in the zip file, but empty.
Give a try to this:
find . -type d -print | zip name.zip -#
Also check this answer: https://stackoverflow.com/a/13234936/1135424

Zip together all HTML files under current directory

I am looking to zip together *.html files recursively under the current directory.
My current command is:
zip all-html-files.zip *.html
But this doesn't work recursively. Nor does adding the -r option it seems. Can anybody advise? I want to zip all html files under the current directory, including those underneath subdirectories, but zip the HTML files only, not their file folders.
Thanks!
What about this?
find /your/path/ -type f -name "*.html" | xargs zip all_html_files.zip
looks for all .html files under the directory /your/path (change it for yours). Then, pipes the result to xargs, which creates the zip file.
To junk the paths, add -j option:
find /your/path/ -type f -name "*.html" | xargs zip -j all_html_files.zip
find . -name "*.html" -print | zip all-html-files.zip -#
Try
find . -type f -name "*.html" | xargs zip all-html-files
You can also say
find . -type f -name "*.html" | zip all-html-files -#
If you do not want to preserve the directory structure, specify the -j option:
find . -type f -name "*.html" | zip -j all-html-files -#
man zip says:
-# file lists. If a file list is specified as -# [Not on MacOS], zip
takes the list of input files from standard input instead of from the
command line. For example,
zip -# foo
will store the files listed one per line on stdin in foo.zip.
Under Unix, this option can be used to powerful effect in conjunction
with the find (1) command. For example, to archive all the C source
files in the current directory and its subdirectories:
find . -name "*.[ch]" -print | zip source -#
(note that the pattern must be quoted to keep the shell from expanding
it).
-j
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current directory).

how can i search for files and zip them in one zip file

I tried to search files and zip them with the following commmand
find . regexpression -exec zip {} \;
however it is not working. How can i do this?
The command you use will run zip on each file separately, try this:
find . -name <name> -print | zip newZipFile.zip -#
The -# tells zip to read files from the input. From man zip(1),
-# file lists. If a file list is specified as -# [Not on MacOS], zip takes the list of input files from standard input instead of from the command line.
Your response is close, but this might work better:
find -regex 'regex' -exec zip filname.zip {} +
That will put all the matching files in one zip file called filename.zip. You don't have to worry about special characters in the filename (like a line break), which you would if you piped the results.
You can also provide the names as the result of your find command:
zip name.zip `find . -name <name> -print`
This is a feature of the shell you are using. You can search for "backticks" to determine how your shell handles this.

Resources