All commands tested on Mac OS Monterey / Jul 02, 2024

How to create a tar archive without the parent directory

Create a tar archive called my-directory.tar of my-directory/ without the parent directory.

You can use this short tar command.

              
                  tar -C my-directory -cvf my-directory.tar .
              
          

Or this more verbose tar command.

              
                  tar --cd my-directory --create --verbose --file my-directory.tar .
              
          

Make sure you include the trailing ., as this tells tar to include everything from the root of the directory once it's done the cd.


Options explained

  1. -C, --cd or --directory tells tar to change (cd) into the directory before creating, listing, extracting, appending to etc. the archive
  2. -c or --create tells tar to create a new archive
  3. v or --verbosetells tar to produce verbose output
  4. f or --filetells tar that the next parameter will be the tar file to create, extract, append to, list, etc.

More commands for creating tar archives