Hot answers tagged

279

No. Unlike zip, gzip functions as a compression algorithm only. Because of various reasons some of which hearken back to the era of tape drives, Unix uses a program named tar to archive data, which can then be compressed with a compression program like gzip, bzip2, 7zip, etc. In order to "zip" a directory, the correct command would be tar -zcvf ...


129

You can just use *; there is no need for *.*. File extensions are not special on Unix. * matches zero or more characters—including a dot. So it matches foo.png, because that's zero or more characters (seven, to be exact). Note that * by default doesn't match files beginning with a dot (neither does *.*). This is often what you want. If not, in bash, if you ...


125

Typically one uses tar to create an uncompressed archive and either gzip or bzip2 to compress that archive. The corresponding gunzip and bunzip2 commands can be used to uncompress said archive, or you can just use flags on the tar command to perform the uncompression. If you are referring specifically to the Zip file format, you can simply use the zip and ...


55

You've explained the situation very well. The final piece to the puzzle is that unzip can handle wildcards itself: http://www.info-zip.org/mans/unzip.html ARGUMENTS file[.zip] ... Wildcard expressions are similar to those supported in commonly used Unix shells (sh, ksh, csh) and may contain: * matches a sequence of 0 or more ...


49

unzip -j "myarchive.zip" "in/archive/file.txt" -d "/path/to/unzip/to" Enter full path for zipped file, not just the filename. Be sure to keep the structure as seen from within the zip file. This will extract the single file file.txt in myarchive.zip to /path/to/unzip/to/file.txt.


46

You can extract just the text to standard output with the -p option: unzip -p myarchive.zip path/to/zipped/file.txt >file.txt This won't extract the metadata (date, permissions, …), only the file contents. That's the price to pay for the convenience of not having to move the file afterwards. Alternatively, mount the archive as a directory and just ...


45

tar stores relative paths by default. GNU tar even says so if you try to store an absolute path: tar -cf foo.tar /home/foo tar: Removing leading `/' from member names If you need to extract a particular folder, have a look at what's in the tar file: tar -tvf foo.tar And note the exact filename. In the case of my foo.tar file, I could extract ...


36

You can do that using unzip -Zt zipname which prints a summary directly about the archive content, with total size. Here is an example on its output: unzip -Zt a.zip 1 file, 14956 bytes uncompressed, 3524 bytes compressed: 76.4% Then, using awk, you can extract the number of bytes: unzip -Zt a.zip | awk '{print $3}' 14956 Finally, put it in a for loop ...


34

Install zip and use zip -r foo.zip . You can use the flags -0 (none) to -9 (best) to change compressionrate Excluding files can be done via the -x flag. From the man-page: -x files --exclude files Explicitly exclude the specified files, as in: zip -r foo foo -x \*.o which will include the contents of foo in ...


31

You can use this loop in bash: for i in */; do zip -r "${i%/}.zip" "$i"; done i is the name of the loop variable. */ means every subdirectory of the current directory, and will include a trailing slash in those names. Make sure you cd to the right place before executing this. "$i" simply names that directory, including trailing slash. The quotation marks ...


28

You will need to join them first. You may use the common linux app, cat as in the example below: cat test.zip* > ~/test.zip This will concatenate all of your test.zip.001, test.zip.002, etc files into one larger, test.zip file. Once you have that single file, you may run unzip test.zip "How to create, split, join and extract zip archives in Linux" ...


23

Try: unzip "/path/to/archive.zip" "in/archive/folder/\*" -d "/path/to/unzip/to"


22

The Linux unzip utility doesn't really support multipart zips. From the manual: Multi-part archives are not yet supported, except in conjunction with zip. (All parts must be concatenated together in order, and then zip -F (for zip 2.x) or zip -FF (for zip 3.x) must be performed on the concatenated archive in order to ...


22

The difference between those two commands is the quoted * character. If you call a command in a shell and use the * character for an argument, the shell itself will evaluate the argument. See this example: $ ls file1.zip file2.zip file3.zip file4.txt Now with a *: $ ls *.zip file1.zip file2.zip file3.zip The shell evaluates the wildcard and builds ...


19

If you type unzip -l <zipfile>, it prints a listing of files within the zip, with their uncompressed sizes, then the total uncompressed size of all of them. This is human-readable output, but you can get a machine-readable number using unzip -l <zipfile> | tail -n1 | awk '{ print $1 }'. To get a total size: total=0 for file in *.zip; do # or ...


18

The gzip command will not recursively compress a directory into a single zip file, when using the -r switch. Rather it will walk that directory structure and zip each file that it finds into a separate file. Example before $ tree dir1/ dir1/ |-- dir11 | |-- file11 | |-- file12 | `-- file13 |-- file1 |-- file2 `-- file3 now run the gzip command $ ...


17

You can use the command archivemount to mount archives such as .tar.gz. $ ls files.tgz mnt/ $ archivemount files.tgz mnt $ ls mnt file1 file2 [... Perform desired read/write operations on the archive via mnt/ ...] $ umount mnt [... Any changes are saved to the archive ...] See the man page for archivemount for more info. It's often times not ...


15

0040000 is the traditional value of S_IFDIR, the file type flag representing a directory. The type uses the top 4 bits of the 16-bit st_mode value, 0100000 is the value for regular files. The high 16 bits of the external file attributes seem to be used for OS-specific permissions. The Unix values are the same as on traditional unix implementations. Other ...


15

unzip -l lists the size of each file and prints a final line with their sum. So you can loop through the zip files and add up the output of unzip -l "$zip" | awk 'END {print $1}' or of unzip -Zt "$zip" | awk 'END {print $3}'. For a shell loop, unzip -Zt may be a little faster: total=0 for z in *.zip; do set $(unzip -Zt -- "$z") total=$((total + $3)) ...


14

Well, when it comes to distributing files for a variety of operating systems, I'd recommend 7-zip. Usually in the package p7zip, you'll get the 7z and 7za command, with which you can create your own 7z archives. 7za can also decompress standard (pkzip) zip archives (and create them as well with the -tzip switch). Compressing: 7za a archive.7z file1 file2 ...


13

You can zip files up (in compressed format) with: tar -zcvf myfile.tgz . which will do the current directory. Replace . with other file names if you want something else. To unzip that file, use: tar -zxvf myfile.tgz That's assuming of course that you have a tar capable of doing the compression as well as combining of files into one. If not, you can ...


13

My issue was because I was trying to use "unzip" on MAC OSX which cannot handle things zipped with PKZIP. I was able to brew install p7zip and unzip using the command 7za x some_file.zip. I originally found the solution in this article: need-pk-compat-v4-5-can-do-v2-1


12

Ok, let's apply the unix philosophy. What are the components of this task? Text search: you need a tool to search text in a file, such as grep. Recursive: you need a tool to go looking for files in a directory tree, such as find. Archives: you need a tool to read them. Most unix programs operate on files. So to operate easily on archive components, you ...


12

You can list the content of the archive and then pass the list to rm using xargs Example for a tarball (test it without the rm first): tar tfz archive.tar.gz | xargs rm -rf


12

You can use the unzip utility with the -v flag: unzip -v files.zip Archive: files.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 0 Stored 0 0% 11-23-2011 15:02 00000000 file1 0 Stored 0 0% 11-23-2011 15:02 00000000 file2 -------- ...


12

Try: for f in `ls *.zip`; do echo "$f: "; unzip -l $f | grep <file_name>; done


11

You can try to find the relevant files with find: find /usr/something -maxdepth 1 -user antoine You can then use -exec to create a zip file from the results of find: find /usr/something -maxdepth 1 -user antoine -exec zip /tmp/file.zip {} + leave out the maxdepth if you want to recurse.


10

By name You can generate the list of files in the archive and delete them, though this is annoyingly fiddly with archivers such as unzip or 7z that don't have an option to generate a plain list of file names. Even with tar, this assumes there are no newlines in file names. tar tf foo.tar | while read -r file; do rm -- "$file" done unzip -l foo.zip | awk ' ...


10

unzip <target-zip-file> '<folder-to-extract/*>' -d <destination-path> works fine on EL 6


10

You can use GNU parallel to do that as it can limit the number of elements to a job as well as provide a job number (for a unique zip archive name): $ touch $(seq 20) $ find . ! -name "*.zip" -type f -print0 | parallel -0 -N 5 zip arch{#} {} adding: 1 (stored 0%) adding: 10 (stored 0%) adding: 11 (stored 0%) adding: 12 (stored 0%) adding: 13 ...



Only top voted, non community-wiki answers of a minimum length are eligible