0
0
Linux CLIscripting~5 mins

tar (create and extract archives) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to bundle many files into one file to move or save space. The tar command helps you pack files into an archive and also unpack them later.
When you want to send multiple files as one package to a friend or colleague.
When you need to back up a folder with many files into a single file.
When you want to compress files to save disk space or transfer time.
When you receive a tar archive and want to see or extract its contents.
When you want to combine files before uploading or downloading them.
Commands
This command creates a tar archive named archive.tar from the folder named 'folder'. The flags mean: c=create, v=verbose (show files), f=file (name of archive).
Terminal
tar -cvf archive.tar folder
Expected OutputExpected
folder/ folder/file1.txt folder/file2.txt
-c - Create a new archive
-v - Show progress with file names
-f - Specify archive file name
This command lists the contents of the archive.tar file without extracting it. Flags: t=list, v=verbose, f=file.
Terminal
tar -tvf archive.tar
Expected OutputExpected
drwxr-xr-x user/user 0 2024-06-01 12:00 folder/ -rw-r--r-- user/user 1234 2024-06-01 12:00 folder/file1.txt -rw-r--r-- user/user 5678 2024-06-01 12:00 folder/file2.txt
-t - List archive contents
-v - Show detailed info
-f - Specify archive file name
This command extracts all files from archive.tar into the current directory. Flags: x=extract, v=verbose, f=file.
Terminal
tar -xvf archive.tar
Expected OutputExpected
folder/ folder/file1.txt folder/file2.txt
-x - Extract files from archive
-v - Show extracted files
-f - Specify archive file name
This command creates a compressed archive using gzip compression. Flags: c=create, z=gzip compress, v=verbose, f=file.
Terminal
tar -czvf archive.tar.gz folder
Expected OutputExpected
folder/ folder/file1.txt folder/file2.txt
-c - Create archive
-z - Compress with gzip
-v - Show files
-f - Archive file name
This command extracts a gzip compressed archive. Flags: x=extract, z=gzip decompress, v=verbose, f=file.
Terminal
tar -xzvf archive.tar.gz
Expected OutputExpected
folder/ folder/file1.txt folder/file2.txt
-x - Extract files
-z - Decompress gzip
-v - Show files
-f - Archive file name
Key Concept

If you remember nothing else from tar, remember: use -c to create archives and -x to extract them, always with -f to specify the archive file.

Common Mistakes
Forgetting the -f flag when creating or extracting archives
tar will try to read from or write to a default device or stdin/stdout, causing errors or unexpected behavior
Always include -f followed by the archive file name to tell tar which file to use
Trying to extract a compressed archive without the -z flag
tar won't decompress gzip files without -z, so extraction fails or produces garbage
Add -z when working with .tar.gz files to handle gzip compression properly
Using -c (create) and -x (extract) together in one command
These flags do opposite actions and cannot be combined; tar will error out
Use either -c to create or -x to extract in a single command, not both
Summary
Use 'tar -cvf archive.tar folder' to create an archive from a folder.
Use 'tar -tvf archive.tar' to list contents of an archive without extracting.
Use 'tar -xvf archive.tar' to extract files from an archive.
Add -z flag to compress or decompress gzip archives (.tar.gz).