Collect Files in a tar Archive
Collect Files in a tar Archive
The teacher needs to send a term's reports as one package rather than attach every file separately. An archive collects named entries and their data into a single file. tar is a common way to do that while retaining the directory structure needed to understand where each report belongs.
There is an easy distinction to miss: collecting files is not the same as compressing them. A plain tar archive can package a whole tree without using gzip or another compressor. Let's do the collection first and inspect what we packed.
What you’ll learn
- Bundle files into a tar archive and list its members.
- Distinguish archiving from compression.
Many entries can travel as one archive
Source tree
The term directory contains its class list.
term/students.txtArchive
tar -cf packages the entries and their data into one file.
term.tarInspect
tar -tf lists members without extracting them.
term/ and its entries
Bundle the term folder and list its members
Prepare the example
Use a new practice directory with tar installed. The first two lines prepare a term folder containing a student list. We place the archive outside that source folder so we are not asking the tool to include its own growing output in the input tree.
Bash · Linux terminal
mkdir term
printf 'Diya\n' > term/students.txt
tar -cf term.tar term
tar -tf term.tarExpected behavior; machine-dependent details
Entries for term and term/students.txt; order/format may vary.In tar -cf term.tar term, -c means create an archive. The -f option supplies the archive filename; term is the source entry to include. Its directory structure and child file become archive members. The archive is a package, not a pointer back to the working folder.
Then tar -tf term.tar lists those members without extracting them. Read the listed paths to check that you included the intended term tree. The listing format and ordering can vary, but the source directory and student-list member should be represented. There is no gzip option in the create command, so do not assume the package is compressed merely because it is an archive.
Package the source tree and choose compression
The tar command collects many files and directories into one archive. Archiving and compression are related but separate ideas: a plain .tar bundles files, while options such as -z add compression.
What is tar?
tar stands for "tape archive" — it was originally used for writing data to tape drives, but now it's a super useful command for bundling up files and folders into a single file, called a tarball (usually with a .tar extension).
Let's Create an Archive
Suppose you have a folder called project and you want to archive it.
tar -cvf project.tar project/
-c= create a new archive-v= verbose (shows the progress)-f= file name of the archive
This will create a file called project.tar in your current directory.
project/
project/file1.txt
project/file2.txt
project/images/logo.png
Want to Compress It Too?
No problem! Just add -z to compress the tarball using gzip.
tar -czvf project.tar.gz project/
Now your archive is smaller and easier to share!
List members and select an extraction destination
How to List Contents of a Tar File
Let’s say you want to peek inside without extracting:
tar -tvf project.tar
drwxr-xr-x user/user 0 2025-07-01 10:00 project/
-rw-r--r-- user/user 123 2025-07-01 10:00 project/file1.txt
-rw-r--r-- user/user 456 2025-07-01 10:00 project/file2.txt
How to Extract a Tar File
If someone sends you a tarball and you want to unpack it:
tar -xvf project.tar
Want to extract a .tar.gz file? Same idea:
tar -xzvf project.tar.gz
Extract to a Specific Directory
You can also specify where the files should go:
tar -xvf project.tar -C /home/yourname/Documents/
Common tar options used in these examples
-c: Create a new archive-x: Extract files-v: Verbose mode (show progress)-f: Archive file name-z: Usegzipto compress/decompress-t: List contents of an archive-C: Change to directory before extracting
Wrap-Up
Try it, then check the worked answer
Your task: Does tar -cf automatically gzip the archive?
Use a new practice directory with tar installed. The first two lines prepare a term folder containing a student list. We place the archive outside that source folder so we are not asking the tool to include its own growing output in the input tree.
Bash · complete example for this question
mkdir term
printf 'Diya\n' > term/students.txt
tar -cf term.tar term
tar -tf term.tarExpected behavior; machine-dependent details
Entries for term and term/students.txt; order/format may vary.Worked answer
No; it creates a tar archive without that compression flag.
tar -cf creates the tar package without requesting gzip compression. File suffixes are useful naming conventions, but the operation's options decide how data is produced. When you want a compressed tar archive, use the appropriate compressor option or a separate documented compression step.
Check your decision
An archive is not automatically compressed
Archive when you need several entries to travel together with their structure. Compress when you need an encoding that can reduce storage size. Check a package's member list before sharing or extracting it; a successful archive command alone does not tell you that you selected the right source tree.
Reaching the recap and completing the quizzes are separate milestones.