Zip specific files in directory - linux

Is there a way I a can zip specific files in a folder.
For example /tmp/data/test/
In the test directory it list
images(in this folder are .tiff images)
test.csv
test.pdf
I want to zip up just images folder and the test.csv file without the test.pdf and /tmp/data/test being included. The zip file will only have /images and test.csv
zip -j test.zip /tmp/data/test/images /tmp/data/test/test.csv
(but this only gives me the csv)
zip -r test.zip /tmp/data/test/images /tmp/data/test/test.csv
(This gives me everything when I only need just images folder the test.csv)

Related

Zip a folder named dash

Zip folder which contain a folder named - , it can't be zip
folder structure
~/-
~/b.txt
~/-/a.txt
command
cd ~
zip -r abc.zip .
will stuck
~# zip -r abc.zip .
adding: -
I used strace (read(0)), it seems zip treat the folder as a -(standard input).
how can zip folder which contain a folder which name is dash(-)
You can create zip on this way:
zip -r file.zip -/* b*
I know, its not so elegant but it work.
Or you can use tar on such way:
tar cvf file.tar .

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

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

Using Cygwin to zip individual files in a folder and zipping the entire folder

i am trying to zip individual files in a folder. i can zip the directory using
tar -zcf <filename> <location to place the file after zipping>
please help...
I do it like this
tar acf foo.tar.gz README.txt bar.txt dog.txt
ref

Zip including hidden files

In Linux I can zip all(except hidden files) in current directory by doing:
zip 1.zip *
But how do I include the hidden files?
EDIT: The correct way is zip -r 1.zip .
The commands shown in my previous answer below are incorrect because they also include the parent directory.
Have you tried this:
zip yourfile.zip sourcedir/* .*
or you in your case
zip 1.zip * .[^.]*
It should include all hidden files also.
Or you can add more simple
zip 1.zip ./
Just to be sure it is not forgotten since this is a forum for developers and a good number of us use git.
An easy way to get only what you want in the zip is to use git archive -o filename.zip branch
If you want to zip all files (+hidden files)
Kindly using: zip -r namefiles.zip .
The "." is all files in folder.
zip -r namefiles.zip "folder will zip"
On macOS 10.15.7 I had to separatelly add all dot leading files (\.*) and rest of the files (*):
zip -r file.zip \.* *
if you don't have rights to save zip file in current dir you can go to dir where you have rights and type
zip -r 1.zip /path/to/source/dir/.
However when if in .../some_dir you type
unzip 1.zip
then your files will be decompress into .../some_dir/path/to/source/dir/
zip -r 1.zip .* -x "../*"
Just doing zip -r 1.zip .* will include the parent folder as well so the trick is to exclude the parent folder using -x "../*"
If you'd like to save some subdirectory of the current directory recursively with hidden and regular files just type
$ zip -r backup_subdirectory.zip backup_subdirectory/. backup-subdirectory/*
And for unzipping:
$ unzip backup_subdirectory.zip
Or even simpler by using tar for creating an archive:
$ tar czvf backup_subdirectory.tar.gz backup_subdirectory/
And for extracting all files from the archive:
$ tar xzvf backup_subdirectory.tar.gz

Resources