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.

Terminal window
tar [options] [tarfile] [files/directories...]
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
ParameterDescription
tarfileName of the archive file (if applicable)
files/directories…Files or directories to be archived
Terminal window
tar -cvf archive.tar file1 file2

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

Terminal window
tar -xvf archive.tar

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

Terminal window
tar -czvf archive.tar.gz directory

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

Terminal window
tar -xzvf archive.tar.gz

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

Terminal window
tar -tvf archive.tar

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

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.

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

Terminal window
tar -xf file.tar

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?

Section titled “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?

Section titled “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

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?

Section titled “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?

Section titled “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?

Section titled “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
  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