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

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

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 create zip archive with files with only .txt extension in Linux using terminal

I have directory "Documents" with these files:
file1.txt.
file2.txt
index.html
index.php
script.pl
I want to create zip archive named files.zip with only .txt extension files using terminal. How can I do these?
At the basic level, if you are in the same directory that your files are in, you can do :
zip files.zip *txt
And if you want to zip the files with .txt extention, by giving the absolute path, if they are in Documents directory, which will create files.zip in the current directory you are in:
zip files.zip /the/path/to/Documents/*txt
If you also want this zipped file to be in Documents folder, you should specify it as:
zip /the/path/to/Documents/files.zip /the/path/to/Documents/*txt
An add-on to the existing answer, if you are adding to an existing zip archive, be careful if you have identically named entries in the zip archive & the inpath. From man zip:
Command format. The basic command format is
zip options archive inpath inpath ...
where archive is a new or existing zip archive and inpath is a directory or file path optionally including wildcards. When given the
name of an existing zip archive, zip will replace iden‐
tically named entries in the zip archive (matching the relative names as stored in the archive) or add entries for new names. For
example, if foo.zip exists and contains foo/file1 and
foo/file2, and the directory foo contains the files foo/file1 and foo/file3, then:
zip -r foo.zip foo
or more concisely
zip -r foo foo
will replace foo/file1 in foo.zip and add foo/file3 to foo.zip. After this, foo.zip contains foo/file1, foo/file2, and foo/file3, with
foo/file2 unchanged from before.
So if before the zip command is executed foo.zip has:
foo/file1 foo/file2
and directory foo has:
file1 file3
then foo.zip will have:
foo/file1 foo/file2 foo/file3
where foo/file1 is replaced and foo/file3 is new.
Give a try to:
zip only-txt.zip `find . -name "*.txt"`
This will create a zip file named only-txt.zip including all the *.txt files located within the directory you run the command, notice that this will search for *.txt files recursively in all subfolders of the dir

Zip folder exclude some folders

I'm trying to backup my www-folder but hidden folders like .config inside www are added to the backup. I want to exclude the folder "backups" and all folders (and files) starting with a dot.
The problem is that it copies all the hidden folders like .config to the zip-file.
Current code:
zip -r /var/www/backups/site/$(date +\%Y-\%m-\%d-\%H-\%M).zip /var/www -x "*backups*" "*.*" "*/.*"
This should work for you.
zip -r --exclude=*backups* --exclude=*/.* /var/www/backups/site/$(date +\%Y-\%m-\%d-\%H-\%M).zip /var/www
Use a linux find command with an exclude flag, then pipe it into zip.
The following command will exclude all paths under the current directory containing the keywords "backups" or files with "/." in the path and then pipe the files into zip.
find . | grep -v "\(backups\|/\.\)" | xargs zip archive.zip

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

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 -#

How to zip all EXCEPT a few, specified directories?

I want to use zip on most of the directories in my home directories but there are a few specific directories (let's call them dir1 and dir2) with contents that I do not want zip to add to my subsequent archive file. I tried
zip foo.zip ./* -x#exclude.lst
with exclude.lst containing:
./dir1/*
./dir2/*
but zip still is including the contents of those directories. How can I have zip skip the contents of those specific directories?
I tried this using just:
dir1/*
in the file and found that dir2/ gets included but dir1/ and things under it don't.

Resources