Reputation: 41
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?
Upvotes: 2
Views: 8889
Reputation: 7819
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.
Upvotes: 0
Reputation: 26925
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
Upvotes: -1
Reputation: 909
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
Upvotes: 5