Bird
Raised Fist0
Linux CLIscripting~10 mins

tar (create and extract archives) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - tar (create and extract archives)
Start
↓
Choose Action
↓
Create Archive
↓
Run tar with -c
↓
Archive Created
↓
End
The flow shows choosing to create or extract an archive using tar, running the command, then finishing.
Execution Sample
Linux CLI
tar -cvf archive.tar file1.txt file2.txt

tar -xvf archive.tar
First command creates an archive named archive.tar with two files; second command extracts files from archive.tar.
Execution Table
StepCommandActionOutputResult
1tar -cvf archive.tar file1.txt file2.txtCreate archive named archive.tarfile1.txt file2.txtarchive.tar created with 2 files
2tar -xvf archive.tarExtract files from archive.tarfile1.txt file2.txtfile1.txt and file2.txt extracted
3EndNo more commandsProcess complete
💡 All files archived and extracted successfully, process ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
archive.tardoes not existcreated with file1.txt and file2.txtexistsexists
file1.txtexistsexistsexistsexists
file2.txtexistsexistsexistsexists
Key Moments - 3 Insights
Why do we use -c to create and -x to extract?
The -c option tells tar to create an archive (see Step 1 in execution_table), while -x tells tar to extract files from an archive (see Step 2). Using the wrong option will not perform the intended action.
What does the -v option do?
The -v option makes tar show the files being processed (visible in Output column in Steps 1 and 2). It helps you see progress but is optional.
Why specify the archive file name after -f?
The -f option tells tar the name of the archive file to create or extract. Without it, tar uses a default or errors out. This is shown in the Command column in Steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table Step 1. What does the output show?
AThe names of files added to the archive
BThe contents of the archive file
CAn error message
DThe size of the archive
💡 Hint
Check the Output column in Step 1 showing file1.txt and file2.txt
At which step are files extracted from the archive?
AStep 1
BStep 2
CStep 3
DNone
💡 Hint
Look at the Action column for Step 2 in execution_table
If we omit the -v option, what changes in the output?
AThe archive is not created
BFiles are not extracted
CNo file names are shown during create or extract
DThe archive file name changes
💡 Hint
Compare Output columns in Steps 1 and 2; -v controls verbosity
Concept Snapshot
tar command creates or extracts archives.
Use -c to create, -x to extract.
Use -f to specify archive file name.
-v shows files being processed.
Example: tar -cvf archive.tar files
Example: tar -xvf archive.tar
Full Transcript
This lesson shows how to use the tar command to create and extract archive files. First, you choose whether to create or extract. To create, use tar with -c (create), -v (verbose), and -f (file) options, followed by the archive name and files to include. The command lists files as it adds them. To extract, use tar with -x (extract), -v, and -f options, followed by the archive name. The command lists files as it extracts them. The process ends when all files are archived or extracted. Key points: -c creates, -x extracts, -v shows progress, -f names the archive file. This helps you bundle files into one archive or unpack them easily.

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

  1. 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.
  2. Step 2: Analyze the command effect

    The command creates a new archive file named archive.tar containing the contents of the folder/ directory.
  3. Final Answer:

    Creates a tar archive named archive.tar containing the folder directory -> Option C
  4. 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

  1. Step 1: Identify extraction and compression flags

    -x extracts, -v shows progress, -z handles gzip compression, and -f specifies the file.
  2. 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.
  3. Final Answer:

    tar -xvzf backup.tar.gz -> Option A
  4. 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

  1. Step 1: Understand the flags

    -t lists contents of an archive, -v shows details, and -f specifies the archive file.
  2. Step 2: Predict the command output

    The command lists the files inside archive.tar with details, without extracting them.
  3. Final Answer:

    Lists file1.txt and file2.txt with details without extracting -> Option D
  4. Quick Check:

    -t lists contents, -v shows details [OK]
Hint: -t lists archive contents, -v adds details [OK]
Common Mistakes:
  • Thinking it extracts files instead of listing
  • Assuming -t and -v cannot be combined
  • Confusing -t with -x
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

  1. Step 1: Verify command syntax

    The flags -c (create), -v (verbose), -z (gzip), -f archive.tar.gz are correct, with filename immediately after -f.
  2. 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.
  3. Final Answer:

    The folder/ directory does not exist -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    tar -czvf project.tar.gz --exclude='*.log' project/ -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard