Complete Guide to Tar Archiving with File Lists

Nov 30, 2025 · Programming · 12 views · 7.8

Keywords: tar archiving | file lists | Linux commands | -T option | standard input

Abstract: This article provides an in-depth exploration of using tar command with file lists for archiving in Linux/Unix systems. It details the usage of -T option, analyzes its differences from traditional parameter passing methods, and demonstrates through practical code examples how to read file lists from standard input. The article also discusses compatibility issues across different tar implementations, offering practical archiving solutions for system administrators and developers.

Tar Archiving and File List Input

In Linux and Unix systems, tar (tape archive) is a widely used archiving tool for packaging multiple files into a single archive file. The traditional approach to using tar command involves directly specifying files to archive through command-line parameters:

tar -cvf archive.tar file1.txt file2.txt file3.txt

However, when dealing with a large number of files, the limitations of this approach become apparent. Command-line length restrictions may prevent specifying all files at once, and manually entering numerous filenames is prone to errors.

Core Functionality of -T Option

GNU tar provides the -T option (equivalent to --files-from), which allows reading the list of files to archive from a file. Suppose we have a file named mylist.txt with the following content:

file1.txt
file2.txt
file3.txt
file10.txt

We can use the following command to achieve file list-based archiving:

tar -cvf allfiles.tar -T mylist.txt

This command produces the same result as:

tar -cvf allfiles.tar file1.txt file2.txt file3.txt file10.txt

Technical Implementation Details

The -T option works by having the tar command read filenames line by line from the specified file, then adding these files to the archive. Each line should contain a single filename or path, supporting both relative and absolute paths.

Option parameter explanation:

Reading File Lists from Standard Input

In addition to reading from files, tar also supports reading file lists from standard input. This is particularly useful in pipeline operations:

find . -name "*.txt" -type f | tar -cvf textfiles.tar -T -

Here, -T - indicates reading the file list from standard input. The - character in Unix systems typically represents standard input or standard output.

Compatibility Considerations

It's important to note that the -T - syntax may not be supported in some tar implementations, particularly in minimal environments like BusyBox. In these cases, you might encounter errors such as tar: can't open '-': No such file or directory.

To ensure compatibility, consider the following alternatives:

find . -name "*.txt" -type f > filelist.txt
tar -cvf textfiles.tar -T filelist.txt

Or using temporary files:

find . -name "*.txt" -type f | tee filelist.txt | tar -cvf textfiles.tar -T filelist.txt

Practical Application Scenarios

File list-based archiving is particularly useful in the following scenarios:

  1. Large Project Builds: When archiving specific file types, generate file lists using find command first
  2. Incremental Backups: Generate file lists for backup by comparing file timestamps
  3. Selective Archiving: Dynamically generate file lists for archiving based on business logic
  4. Automation Scripts: Dynamically generate and archive file lists within scripts

Best Practice Recommendations

When using file lists for tar archiving, follow these best practices:

Error Handling and Debugging

Common errors when using the -T option include:

It's recommended to test file lists using the -t option before actual use:

tar -tvf archive.tar -T filelist.txt

This verifies the correctness of the file list without actually creating the archive file.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.