tar archives
Updated November 1, 2026
tar gathers a tree of files into one archive, keeping names, permissions and structure.
tar czf a.tar.gz -C ~ project # create (c), gzip (z), to a file (f)
tar tzf a.tar.gz # list (t) without extracting
tar xzf a.tar.gz -C ~/restore # extract (x) into a directorySample output
$ tar czf a.tar.gz -C ~ project
$ tar tzf a.tar.gz
project/
project/src/
project/src/main.c
project/README.md
$ tar tvzf a.tar.gz
drwxr-xr-x student/student 0 2026-07-26 03:48 project/
drwxr-xr-x student/student 0 2026-07-26 03:48 project/src/
-rw-r--r-- student/student 13 2026-07-26 03:48 project/src/main.c
-rw-r--r-- student/student 6 2026-07-26 03:48 project/README.mdThe letters are easy: c is create, t is list, x is extract; z is gzip, f is file, -C changes directory. tar tzf is handy to peek inside an archive before unpacking.
Common mistakes
An absolute path gets trimmed. tar czf c.tar.gz /home/student/project prints the warning tar: Removing leading ... from member names about the leading slash, and the members go in as home/student/project/README.md. Extract that archive into out and you get out/home/student/project/README.md, not out/project. That is why the examples use -C ~ project: change directory first, then name a relative path.
tar xzf overwrites without a word. If the target directory already holds project/README.md with your edits, extraction drops the archived version on top of it: no question, no warning, exit code 0. Extract into an empty directory and compare afterwards.
gzip: stdin: not in gzip format means the file is named .tar.gz but there is no gzip inside. tar then adds tar: Child returned status 1 and tar: Error is not recoverable: exiting now, exit code 2. A plain .tar under the wrong name looks like this, and so does an HTML error page downloaded instead of the archive: file fake.tar.gz answers ASCII text for that download.
Used in
You need this in the lesson Archives: tar and gzip.