📖 Scenario: You have a folder with some important files. You want to bundle them into one archive file to save space or share easily. Later, you want to open that archive to get the files back.
🎯 Goal: Learn how to use the tar command to create an archive file from multiple files and then extract the files back from the archive.
📋 What You'll Learn
Create a tar archive from multiple files
Use a variable to store the archive file name
Extract files from the tar archive
Display the list of files extracted
💡 Why This Matters
🌍 Real World
Archiving files into a single file helps in backup, sharing, and saving disk space. The tar command is widely used in Linux systems for these tasks.
💼 Career
Knowing how to create and extract tar archives is essential for system administrators, developers, and anyone working with Linux servers or managing files efficiently.
Progress0 / 4 steps
1
Create sample files
Create three empty files named file1.txt, file2.txt, and file3.txt using the touch command.
Linux CLI
Hint
Use touch followed by the file names separated by spaces.
2
Set archive file name variable
Create a variable called archive_name and set it to myfiles.tar.
Linux CLI
Hint
Use archive_name=myfiles.tar without spaces around the equals sign.
3
Create the tar archive
Use the tar command with options -cf to create an archive named by $archive_name containing file1.txt, file2.txt, and file3.txt.
Linux CLI
Hint
Use tar -cf $archive_name file1.txt file2.txt file3.txt to create the archive.
4
Extract and list files from the archive
Use the tar command with options -xf to extract files from $archive_name. Then use ls to list the extracted files.
Linux CLI
Hint
Use tar -xf $archive_name to extract and ls file1.txt file2.txt file3.txt to list the files.
Practice
(1/5)
1. What does the tar -cvf archive.tar folder/ command do?
easy
A. Extracts files from archive.tar into the folder directory
B. Compresses the folder directory using gzip
C. Creates a tar archive named archive.tar containing the folder directory
D. Lists the contents of archive.tar without extracting
Solution
Step 1: Understand the flags used
The -c flag means create an archive, -v means verbose (show progress), and -f specifies the archive file name.
Step 2: Analyze the command effect
The command creates a new archive file named archive.tar containing the contents of the folder/ directory.
Final Answer:
Creates a tar archive named archive.tar containing the folder directory -> Option C
Quick Check:
tar -cvf = create archive [OK]
Hint: -c means create archive, -f specifies filename [OK]
Common Mistakes:
Confusing -c (create) with -x (extract)
Thinking -v compresses files
Assuming it extracts files instead of creating
2. Which of the following is the correct syntax to extract a gzip compressed tar archive named backup.tar.gz?
easy
A. tar -xvzf backup.tar.gz
B. tar -xvf backup.tar.gz
C. tar -cvf backup.tar.gz
D. tar -czf backup.tar.gz
Solution
Step 1: Identify extraction and compression flags
-x extracts, -v shows progress, -z handles gzip compression, and -f specifies the file.
Step 2: Match flags to the command
To extract a gzip compressed archive, use tar -xvzf filename. tar -xvzf backup.tar.gz matches this syntax.
Final Answer:
tar -xvzf backup.tar.gz -> Option A
Quick Check:
Extract gzip archive = -xvzf [OK]
Hint: -xvzf extracts gzip compressed tar files [OK]
Common Mistakes:
Omitting -z when extracting gzip archives
Using -c (create) instead of -x (extract)
Confusing -v (verbose) as mandatory
3. What will be the output of the command tar -tvf archive.tar if archive.tar contains files file1.txt and file2.txt?
medium
A. Shows an error because -t cannot be combined with -v
B. Extracts file1.txt and file2.txt to current directory
C. Creates a new archive named archive.tar
D. Lists file1.txt and file2.txt with details without extracting
Solution
Step 1: Understand the flags
-t lists contents of an archive, -v shows details, and -f specifies the archive file.
Step 2: Predict the command output
The command lists the files inside archive.tar with details, without extracting them.
Final Answer:
Lists file1.txt and file2.txt with details without extracting -> Option D
4. You run tar -cvzf archive.tar.gz folder/ but get an error. What is the likely cause?
medium
A. The order of flags is incorrect; -z must come before -c
B. The folder/ directory does not exist
C. The -f flag must be last and followed by the filename
D. You cannot combine -v and -z flags
Solution
Step 1: Verify command syntax
The flags -c (create), -v (verbose), -z (gzip), -f archive.tar.gz are correct, with filename immediately after -f.
Step 2: Determine error cause
The syntax is valid, so the likely cause is that folder/ does not exist, causing a "Cannot stat: No such file or directory" error.
Final Answer:
The folder/ directory does not exist -> Option B
Quick Check:
tar create error = missing source directory [OK]
Hint: tar create errors usually from missing source files/directories [OK]
Common Mistakes:
Assuming flag order syntax error
Overlooking if source directory exists
Confusing with permission issues
5. You want to create a compressed archive of the project/ folder, but exclude all .log files inside it. Which command achieves this?
hard
A. tar -czvf project.tar.gz --exclude='*.log' project/
B. tar -czvf project.tar.gz project/ --exclude='*.log'
C. tar -cvzf project.tar.gz project/ --exclude='*.log'
D. tar -xvzf project.tar.gz --exclude='*.log' project/
Solution
Step 1: Understand exclusion and compression flags
The --exclude='*.log' option excludes files matching the pattern. -c creates, -z compresses with gzip, -v shows progress, and -f specifies the archive filename.
Step 2: Check correct flag order and placement
The exclude option must come before the folder argument. tar -czvf project.tar.gz --exclude='*.log' project/ correctly places --exclude='*.log' before project/ and uses correct flags.
Final Answer:
tar -czvf project.tar.gz --exclude='*.log' project/ -> Option A
Quick Check:
Exclude before folder, use -czvf for compressed archive [OK]
Hint: Put --exclude before folder, use -czvf to create compressed archive [OK]
Common Mistakes:
Placing --exclude after folder argument
Using -x (extract) instead of -c (create)
Mixing flag order or misspelling exclude
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.