When creating tar archives of the current directory, you might notice all paths include a leading "./" prefix. While this might seem harmless, it can cause issues when:
- Extracting specific files from the archive
- Deploying to servers with strict path requirements
- Comparing directory structures
The default behavior of tar is to preserve the exact directory structure, including the relative path reference. When you run:
tar czf dist.tgz --exclude=".gitignore" .
tar interprets the "." as "./" and maintains this prefix for all files in the archive.
Here are three reliable methods to create archives without the leading "./":
Method 1: Using -C to change directory
tar czf dist.tgz -C . .
The -C .
tells tar to change to the current directory first, then archive its contents.
Method 2: Explicitly listing files
tar czf dist.tgz * .[!.]*
This includes all visible files and hidden files (except "." and ".."). Note: May miss some deeply hidden files.
Method 3: Using --transform (GNU tar only)
tar czf dist.tgz --transform='s,^\./,,r' --exclude=".gitignore" .
The transform flag performs regex substitution during archiving.
Always check your archive contents before deployment:
tar tzf dist.tgz | head -5
You should see clean paths without "./" prefixes.
This is particularly important for Docker builds where you want clean paths:
tar czf - -C . . | docker build -
When creating tar archives using commands like:
tar czf dist.tgz --exclude=".gitignore" .
You'll notice all paths in the archive start with ./
. While this might seem harmless, it causes real issues during deployment:
- Extracting specific files requires including the prefix
- Some deployment tools fail to recognize paths correctly
- Creates inconsistency between local and server paths
The leading ./
appears because we're archiving the current directory (the dot at command's end). Tar preserves the relative path structure by default.
Here are three reliable approaches to create clean archives:
1. Using -C (change directory) Option
tar czf dist.tgz -C . --exclude=".gitignore" .
This changes the working directory before processing files.
2. Star Notation Trick
tar czf dist.tgz --exclude=".gitignore" *
Using wildcard instead of dot includes files without path prefixes. Note this excludes hidden files.
3. Transform Option (GNU tar)
tar czf dist.tgz --exclude=".gitignore" \
--transform='s,^\./,,' .
The transform flag performs regex substitution on paths.
Always verify the archive contents before deployment:
tar tzf dist.tgz | head -5
Should show clean paths without leading ./
.
For complex deployments, combine multiple techniques:
tar czf release.tar.gz \
--exclude=".git*" \
--exclude="node_modules" \
--transform='s,^\./,,' \
-C /project/path .