Learn by reading through in order

tar — Archive and Extract

Bundle multiple files into a single uncompressed archive with tar -cf docs.tar docs, list its contents with tar -tf docs.tar, and restore them with tar -xf — illustrated and run hands-on in a browser terminal.

Bundle and List — tar -cf / tar -tf

tar bundles several files or directories into a single file. That single file is called a tar archive, conventionally named with a .tar extension. You bundle with tar -cf archive.tar target and check the contents with tar -tf archive.tar. -c means create, -t means list, and -f specifies the file name.

SyntaxEffect
tar -cf a.tar dirBundle dir into a single a.tar
tar -tf a.tarList the contents (files) inside a.tar
tar -xf a.tarExtract a.tar back into the original files
tar -xf a.tar -C destSet the extraction target directory to dest
Bundle with tar -cf, List with tar -tf
d/a.txtd/b.txtd/ (target to bundle)tar -cf d.tar dd.tar(bundled into one)Easy to share & storetar -tf d.tard/a.txtd/b.txtCheck contents-c creates-t lists
tar -cf d.tar d bundles d into a single d.tar, and tar -tf d.tar lists the included files without extracting them.
mkdir -p docs                 # Create the source directory
printf 'note A\n' > docs/a.txt   # Put two files inside
printf 'note B\n' > docs/b.txt
tar -cf docs.tar docs         # Bundle docs into docs.tar
tar -tf docs.tar              # List docs/ docs/a.txt docs/b.txt
# On a production server, tar -czf docs.tar.gz docs also gzips while bundling (this learning environment is uncompressed only)

This course learns tar with uncompressed archives

gzip compression such as tar -czf works on production servers like Ubuntu. This course learns how tar works with the uncompressed -cf / -tf / -xf (please don't run compression in this learning environment).

① Create the source directory with mkdir -p docs.

② Create two files inside with printf 'note A\n' > docs/a.txt and printf 'note B\n' > docs/b.txt.

③ Use tar's create option to bundle docs into a single file named docs.tar. (Bundling prints nothing to the screen.)

④ Run ls to confirm that docs.tar was created.

⑤ Use tar's list option to list the contents of docs.tar and confirm that docs/a.txt and docs/b.txt are included. (If you run it correctly, an explanation will appear.)

Linux console
0 / 6 completed
Loading Linux Terminal...

Extract — tar -xf and -C

To pull the original files back out of a tar archive, use tar -xf archive.tar. -x means extract. Run it as-is and it extracts into the current directory, but adding -C target extracts into the directory you specify. After extracting, check the pulled-out files with ls.

Extract with tar -xf (-C sets the target)
docs.tarContentsdocs/a.txt etc.Extract thesetar -xf docs.tarNo target specifieddocs/ in current dirtar -xf docs.tar-C out-C sets the targetdocs/ inside out/Extract as-isExtract into out
tar -xf docs.tar extracts into the current directory, while tar -xf docs.tar -C out extracts into out by specifying the target.
mkdir -p pack                 # Create the source directory
printf 'x\n' > pack/a.txt        # Put a file inside
tar -cf pack.tar pack         # Bundle pack into pack.tar
mkdir -p out                  # Prepare the extraction target
tar -xf pack.tar -C out       # Extract into out/
ls out                        # out/pack has been extracted

① Create the source directory with mkdir -p pack, and create one file inside with printf 'x\n' > pack/a.txt.

② Use tar's create option to bundle pack into pack.tar.

③ Run ls to confirm that pack.tar was created.

④ Prepare the extraction target directory with mkdir -p out.

⑤ Add the option that specifies the extraction target to tar's extract option, and extract pack.tar into out.

⑥ Run ls out to confirm that pack was extracted inside out.

Linux console
0 / 7 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1What happens when you run tar -cf docs.tar docs?

Q2Which command lists the contents of a docs.tar archive without extracting it?

Q3In tar -xf pack.tar -C out, what does -C out specify?