Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux tar Include Hidden Files? What to Know

· 5 min read · Home Tech

In this article
  1. Why hidden files can be missed
  2. Reliable ways to archive a directory
  3. Archiving selected files with patterns
  4. Check what went into the archive
  5. Common problems and important exceptions
  6. Frequently Asked Questions

Yes. Linux tar includes hidden files when they are inside a directory you archive; names beginning with a dot are not excluded just because they are hidden. The common surprise is shell expansion: a command such as tar -cf backup.tar * usually does not pass hidden entries to tar, because the shell expands * before tar runs.

Why hidden files can be missed

On Unix-like systems, a filename is conventionally hidden when it begins with a period, as in .config or .bashrc. This is a naming convention, not a special file type or a permission setting. The tar program can archive such files normally.

The issue is usually the shell, not tar. Before starting a command, the shell expands wildcard patterns such as * into matching names. In common shells, a bare * does not match names beginning with a dot. So this command may omit hidden files and directories located in the current directory:

tar -cf backup.tar *

By contrast, when you give tar a directory name, it traverses that directory and archives its contents, including dotfiles, unless you explicitly exclude them:

tar -cf home-backup.tar my-directory

That command stores the directory as my-directory in the archive and includes entries such as my-directory/.config. The distinction—passing expanded filenames versus asking tar to traverse a directory—is the key to understanding most missing-file reports.

Reliable ways to archive a directory

If you want everything in a directory, including hidden entries, archive the directory itself. For example, to create a gzip-compressed archive of a project directory:

tar -czf project.tar.gz project/

To archive the current directory’s contents while keeping paths relative to that directory, change to it and use . as the input:

tar -czf ../project.tar.gz -C project .

The . argument tells tar to start at the current directory. Its traversal includes dotfiles, so this is a practical way to capture the whole tree without relying on wildcard behavior. The -C project option changes the directory for the archive operation; paths in the archive are rooted at . rather than including project/.

Choose the form that suits how you want files to appear when extracted. Archiving project/ creates a top-level project directory; archiving . from inside the project records relative paths under ..

Archiving selected files with patterns

When you need only selected entries, remember that wildcard expansion may happen in the shell. One option in Bash is to enable the dotglob shell option, which makes * match hidden names as well:

shopt -s dotglob
tar -cf backup.tar *

This changes how Bash expands patterns in the current shell. It is not a tar option, and it is not portable to shells that do not support Bash’s shopt. Also, patterns can match . and .. under some shell settings or pattern forms, so avoid broad patterns you have not checked.

For repeatable or complex selections, use a file list. For example, from the directory to archive:

find . -print0 | tar --null -T - -czf ../selected.tar.gz

find . starts at the current directory and normally includes hidden entries. The NUL separator (-print0 and --null) safely handles filenames containing spaces, tabs, or newlines. GNU tar supports these options; check your system’s tar documentation if portability to other implementations matters. For a simple full-directory backup, using the directory name or . is usually easier.

Check what went into the archive

Do not assume an archive contains the intended files just because the command completed. List its contents before relying on it:

tar -tf backup.tar

For a compressed gzip archive, tar -tzf backup.tar.gz lists entries. Look for representative hidden paths such as .config/settings or .env. You can also extract into a temporary directory and verify the result before replacing or deleting the originals.

A useful distinction is between a hidden entry in the source and a hidden entry in the archive. Tar preserves the filename, including its leading period; it does not make a file hidden or visible by changing its name. Whether a program displays that name depends on the program’s own listing rules.

Common problems and important exceptions

  • Using * and assuming it means everything: In common shell configurations, it excludes dotfiles. Archive a directory or use a deliberate, shell-appropriate method instead.
  • Confusing hidden files with excluded files: Tar options such as --exclude can omit matching paths, including hidden ones. Check the full command for exclusions and patterns.
  • Archiving only a directory’s visible children: A command such as tar -cf a.tar dir/* passes the expanded visible matches, rather than the directory itself. Use tar -cf a.tar dir to archive the tree.
  • Permissions or unreadable files: Tar may report errors if the user running it cannot read files or traverse directories. A command can still produce an archive after warnings, so review the exit status and output rather than treating file presence as guaranteed.
  • Files changing during the backup: A running application may modify configuration or data while tar reads it. For important backups, use an application-aware backup procedure or a consistent filesystem snapshot where available.

Hidden does not mean secret or protected. If an archive contains files such as .env, it may contain passwords, API keys, or other sensitive data. Protect the archive with appropriate access controls, store it carefully, and use encryption when the backup’s destination or handling requires it. Tar itself does not encrypt archive contents.

Frequently Asked Questions

Does tar include .hidden files by default?
Yes, when tar receives a directory to traverse, it includes hidden entries within that directory by default. A shell wildcard such as * may prevent those entries from being passed to tar in the first place.

Does tar -czf backup.tar.gz * include dotfiles?
Usually not in Bash and many common shell configurations: the shell expands * without matching names beginning with a dot. Use tar -czf backup.tar.gz directory to archive a directory tree, or use a suitable method to supply the desired file list.

How do I tar everything in the current directory, including hidden files?
Run tar -czf ../backup.tar.gz -C . . from the directory, or use tar -czf ../backup.tar.gz . there. The second form also archives the current directory’s contents with paths beginning at .; choose the archive path outside the directory to avoid including the archive as it is being created.

Does tar include .git and .env?
Yes, if they are inside a directory you archive and no exclusion rule removes them. They may contain useful project metadata or sensitive credentials, respectively, so inspect the archive contents and consider whether each belongs in the backup.

How can I tell whether hidden files are in my tar archive?
List the entries with tar -tf archive.tar, or use tar -tzf archive.tar.gz for a gzip-compressed archive. Check for names beginning with a dot and for any expected hidden directories.