Skip to content

tar MacOS command

The tar command on MacOS is a powerful tool for creating, viewing, and extracting tar archives. It allows you to combine multiple files into a single archive, making it easier to manage and transfer large amounts of data. With tar, you can compress files to save disk space and streamline backups. The command offers various options for customizing the archive creation process, such as specifying compression levels and including/excluding specific files or directories. By mastering the tar command, you can efficiently work with archives on your MacOS system.

tar Syntax:

Terminal window
tar [options] [tarfile] [files/directories...]

Options:

OptionDescription
-cCreate a new archive
-xExtract files from an archive
-vVerbose mode (show progress)
-fSpecify archive file name
-zCompress archive with gzip
-jCompress archive with bzip2
-tList the contents of an archive
-rAppend files to the end of an archive
-uUpdate an archive with new files
-kDo not overwrite existing files
-CChange to directory before operation

Parameters:

ParameterDescription
tarfileName of the archive file (if applicable)
files/directories…Files or directories to be archived

tar command Examples:

Create a tar archive

Terminal window
tar -cvf archive.tar file1 file2

Creates a tar archive “archive.tar” containing files “file1” and “file2”.

Extract a tar archive

Terminal window
tar -xvf archive.tar

Extracts the contents of the tar archive “archive.tar” in the current directory.

Create a gzipped tar archive

Terminal window
tar -czvf archive.tar.gz directory

Creates a gzipped tar archive “archive.tar.gz” of the specified directory.

Extract a gzipped tar archive

Terminal window
tar -xzvf archive.tar.gz

Extracts the contents of the gzipped tar archive “archive.tar.gz” in the current directory.

List contents of a tar archive

Terminal window
tar -tvf archive.tar

Displays the list of files and directories in the tar archive “archive.tar”.

Exclude files when creating a tar archive

Terminal window
tar -cvf archive.tar --exclude='*.log' directory

Creates a tar archive “archive.tar” of the specified directory, excluding all files with a “.log” extension.

How do I extract a tar file in MacOS?

To extract a tar file in MacOS, use the following command:

Terminal window
tar -xf file.tar

How do I create a tar archive in MacOS?

To create a tar archive in MacOS, use the following command:

Terminal window
tar -cf archive.tar file1 file2

How do I list the contents of a tar file in MacOS?

To list the contents of a tar file in MacOS, use the following command:

Terminal window
tar -tf file.tar

How do I add files to an existing tar archive in MacOS?

To add files to an existing tar archive in MacOS, use the following command:

Terminal window
tar -rf archive.tar newfile

How do I compress a tar archive in MacOS?

To compress a tar archive in MacOS, use the following command:

Terminal window
tar -czf archive.tar.gz file1 file2

How do I extract a specific file from a tar archive in MacOS?

To extract a specific file from a tar archive in MacOS, use the following command:

Terminal window
tar -xf archive.tar path/to/file

How do I extract a tar archive to a specific directory in MacOS?

To extract a tar archive to a specific directory in MacOS, use the following command:

Terminal window
tar -xf archive.tar -C /path/to/directory

How do I preserve file permissions when creating a tar archive in MacOS?

To preserve file permissions when creating a tar archive in MacOS, use the following command:

Terminal window
tar -cpf archive.tar --same-permissions file1 file2

Applications of the tar command

  1. Creating tar archives
  2. Extracting files from tar archives
  3. Adding files to existing tar archives
  4. Listing the contents of a tar archive
  5. Extracting files with specific patterns
  6. Compressing tar archives using various algorithms
  7. Combining multiple files or directories into a single tar archive
  8. Verifying the integrity of tar archives