AI Hints
Home›Linux Tutorial›Archives
Linux · Lesson 88 of 120

Archives with tar

Packing many files into one.

⏱ 2 min read▶ Video at 1:57:44🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

To send a whole folder to someone, or back it up, it's much easier to pack it into a single file first. On Linux, the classic tool for that is tar.

Archives with tar
From the video: Archives with tar
  • Send or back up a folder
  • Pack it into one file
  • tar

Creating an archive

Tar dash CVF means create, verbose, and file. Create a new archive, list each file as it's added, and write it to the file name that comes next. Here we pack the whole Documents folder into documents dot tar. LS dash L shows the new archive.

Terminal
tar -cvf documents.tar Documents
Documents/
Documents/cv.txt
Documents/report.txt
ls -l documents.tar
-rw-r--r-- 1 sam sam 10240 Jan  5 09:00 documents.tar

Looking inside, and unpacking

Tar dash TF lists what's inside, without unpacking. And dash XF extracts it. Here we make a new folder, restore, go into it, and extract the archive there. The Documents folder appears, with both files.

Terminal
tar -tf documents.tar
Documents/
Documents/cv.txt
Documents/report.txt
mkdir restore && cd restore
tar -xf ../documents.tar
ls Documents
cv.txt  report.txt

Remembering the letters

Tar's letters look scary, but there are only a few to remember. C to create, X to extract, T to list. F for the file name, which must come last in the group. And V for verbose, if you want to see each file. Remember, create, extract, table of contents.

LetterMeans
ccreate an archive
xextract
tlist the contents
fthe archive file name
vverbose: show each file

Summary

Tar dash CF creates an archive, dash TF lists it, and dash XF extracts it. Next, making archives smaller, with compression.

Key takeaways
  • tar -cf a.tar folder
  • tar -tf a.tar
  • tar -xf a.tar
Next up: Compressing Files — gzip and zip