Compress files from Mac OS X terminal command line

以前都是使用 Finder 來操作,發現對具有「重覆性高」的操作而言實在很浪費人力,應該要使用 command line 來提升效率。

zip 用法:

Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete OS files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
  -q   quiet operation              -v   verbose operation/print version info
  -c   add one-line comments        -z   add zipfile comment
  -@   read names from stdin        -o   make zipfile as old as latest entry
  -x   exclude the following names  -i   include only the following names
  -F   fix zipfile (-FF try harder) -D   do not add directory entries
  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -y   store symbolic links as the link instead of the referenced file
  -e   encrypt                      -n   don't compress these suffixes
  -h2  show more help

This adds the file file to the archive file.zip:

zip file.zip file

Of course, to add more files, just add them as arguments to the command. Check out man zip for more options.

Often, you’ll want to skip including those pesky .DS_Store files, for example compressing the whole folder folder into folder.zip:

zip -vr folder.zip folder/ -x "*.DS_Store"

If you want to make a zip without those invisible Mac resource files such as “_MACOSX” or “._Filename” and .ds store files, use the “-X” option in the command so:

zip -r -X archive_name.zip folder_to_compress

To extract

unzip archive_name.zip

關於絕對路徑的問題

Create zip file and ignore directory structure

Using -j won’t work along with the -r option.
So the work-around for it can be this:

cd path/to/parent/dir/;
zip -r complete/path/to/name.zip ./* ;
cd -;

Or in-line version

cd path/to/parent/dir/ && zip -r complete/path/to/name.zip ./* && cd -

you can direct the output to /dev/null if you don’t want the cd - output to appear on screen


TAR.GZ

To compress

tar -zcvf archive_name.tar.gz folder_to_compress

To extract

tar -zxvf archive_name.tar.gz

TAR.BZ2

To compress

tar -jcvf archive_name.tar.bz2 folder_to_compress

To extract

tar -jxvf archive_name.tar.bz2

GZ

Without the tar

To extract

gunzip archivename.gz

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *