Unix / Linux: Tar Command Remove a Directory From a Tar Ball

To remove a directory from a tar ball in Unix/Linux using the tar command, you can use the --exclude option to exclude the directory from the archive. Here’s how to do it:

  1. Open the terminal and navigate to the directory where the tar ball is located.
  2. Use the tar command with the --exclude option to exclude the directory you want to remove. For example, if you want to remove a directory called “dir_to_remove” from a tar ball called “myarchive.tar”, you would run the following command:
tar -cvf myarchive.tar --exclude=dir_to_remove dir_to_archive

This will create a new tar ball called “myarchive.tar” that does not include the “dir_to_remove” directory.

  1. Optionally, you can delete the original tar ball with the directory you wanted to remove:
rm myarchive.tar

Note that the --exclude option uses globbing, so you can use wildcards to exclude multiple directories. For example, to exclude all directories that start with “test_”, you would use the following command:

tar -cvf myarchive.tar --exclude='test_*' dir_to_archive

Leave a Comment