111

I'm new to Mac OS X and am not sure how to do this:

I have three directories. I want to create a tar/zip file of them so that I can attach them to an email.

Any ideas?

5 Answers 5

173

OSX ships with tar. From the Terminal, you can do this:

tar czf archive_folder_name.tar.gz folder_to_copy

Replace archive_folder_name.tar.gz with whatever you want to call the newly created archive and folder_to_copy with whatever is the name of the folder you want to archive. (It's common to end the archive name with .tar.gz or .tgz, but that's not required.)

8
  • 4
    Personally, I'd use tar cjf for bzip instead of gzip (Smaller files) but that's just a personal preference I suppose. Commented Sep 24, 2009 at 23:45
  • 4
    If you're going for smaller files, then you'd not be after either of them; you could just use p7zip (7z).
    – Jeremy L
    Commented Sep 25, 2009 at 1:29
  • 1
    I tend to go for gzip first, maybe out of habit. To be honest, in most cases I'm not especially worried about trimming the size of the archive itself. It's a reasonable thing to care about, but given the huge size of most machines' hard drives, I don't find myself worrying about space much. If anything, the main reason that I bundle items any more is simply to improve the time factor when copying. OSX in particular seems to take exponentially long to copy lots of even very small files (like source directories) if you don't archive them.
    – Telemachus
    Commented Sep 25, 2009 at 11:45
  • 1
    Probably just leave the code to unzip the files here to save a google search tar -xvf archive_folder_name.tar.gz
    – piie
    Commented Jun 14, 2021 at 16:36
  • Suppose, I have a.txt, b.txt files in folder_to_copy folder. If I untar after creating archive_folder_name.tar.gz file, then I get the contents inside archive_folder_name folder. But, I don't want this. I just want a.txt, b.txt files after untaring archive_folder_name.tar.gz. How could I do this?
    – Hasan
    Commented Aug 5, 2021 at 5:14
17

If you are looking for a GUI solution, simply use the compress command fron the contextual menu.

If you are interested in command-line solutions, several options are possible.

You may of course use the tar command.

tar -zcvf archive.tar.gz folder

But if you are sharing archives, some people may prefer a zip file, that you could create with the zip command

zip -r archive.zip folder
4

I think I found the option:

Right-click on the folder that you want to compress.

Compress option is available.

3
  • This will zip the file with the built in Archive Utility. If you want to use tar you end up having to use the command line. (See Telemachus's answer for details on the command line)
    – Chealion
    Commented Sep 25, 2009 at 1:22
  • 1
    Archive Utility creates zip archives, not tar gzip.
    – Jeremy L
    Commented Sep 25, 2009 at 1:30
  • 2
    I think Sagar has the best answer. The poster says "tar/zip", but that doesn't necessarily mean he's looking for a .tar.gz file, he's just looking for some way to compress a directory into a single archive so it can be attached to an email message.
    – Michael H.
    Commented Sep 25, 2009 at 20:07
0

Well, other than GUI you can use console (or install some kind of compressing software like StuffixExpander or something like that if you don't like the default one or need more formats)

tar --help

tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.

1
  • 2
    tar -cf archive.tar foo bar only creates an archive, but doesn't compress the contents. To compress you should add for example the -z (gzip) or -j (bzip2) to actually compress the files.
    – Koen.
    Commented Jan 6, 2017 at 22:16
-1

I downloaded guitar otherwise known as GUI tar, this allows you to use a gui to tar files instead of going into the terminal. To create a zip you can double press or right click the folder or file and press compress.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .