• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Gzip / Ten Linux Commands on “tar” and “gzip” You Need to Know

Ten Linux Commands on “tar” and “gzip” You Need to Know

August 27, 2011 by cmdlinetips

Admit it. Almost every time you deal with a tar” file or “gzip” file, you wonder what was the command that I used last time to “——“. You can fill the blanks with “Creating a new tar file, how to gzip a tar file, how to unzip a file, how to get a peek of what is in the tar file, and so on. Here are the ten useful linux commands to deal with tar and gzip files. “Tar” in a tar file simply means a tap archives and gzip is specific way compress files.

How to Create a Tar file?

Creating a archive of multiple files using tar command is simple. Use the tar command with -c and -f options as

tar -cf myFirst.tar file1.txt file2.txt file3.txt

where “-c” tells to create a new tar (archive) file and “-f” tell to use the file. A more convenient way to create a tar file is to use verbose “-v” option. The verbose “-v” option give more details about the archiving process by listing the files it uses and often help us to make sure we have created a tar file of what we needed.

How to Create a gzipped tar File?

Creating tar file help us organize our file. However, a more handy feature is is to compress the “tar” archive and use less space to store the archive. To create an archive and compress using the gzip algorithm, use

tar -czf files.tar.gz file1 file2

where the option “-c” tells to create a new tar (archive) file and “-z” tells to compress the archive using the compression program gzip. If you want to create a archive and compress it through bzip2, use the option “-j” instead of “-z”. For example

tar -cjf files.tar.bz2 file1 file2

will create bz2 file of the archive.

How to Extract a compressed (gzipped/bzipped) tar file?

To extract a tar file that is compressed with gzip, use

tar -xzf files.tar.gz

Similarly, if you want to extract a archive file compressed with bzip2, use

tar -xjf files.tar.gz

How to Extract a compressed (gzipped/bzipped) tar file in to Specific Directory?

Extracting a compressed tar file saves the extracted files in the current working directory. It will create a mess if you have archived a large number of files. One option is to extract the archive in a separate directory given by use. To extract a compressed archive in to a directory

tar -xzf file.tar.gz -C myDir

Remember the myDir should exists in the directory.

How to Get Peek of What is Inside a Compressed tar File?

To just see what files are in a compressed tar file, use the tar command with the option “-t” as

tar -tf myfiles.tar.gz

How to Extract a Specific File From a compressed (gzipped/bzipped) tar File?

Often you just want to extract a specific file instead of extracting all the files from a compressed tar file. The command

tar -xzf all.tar.gz wantThisFile.txt

will extract the file “wantThisFile.txt” from the gzipped tar file.

How to Update a tar file with a new file?

To update an archive, with a newer version of file use the option “-u” as

tar -uf newFileVersion.txt myfiles.tar.gz

The new version of the file will replace the existing file only if the modification date is newer.

How to Create a Gzipped Tar file of a Directory?

If you want to gzip and tar a whole directory use tar as

tar -zvcf myDir.tar.gz  path_2_dir

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

create and read gzip file in pythonHow to Read a gzip File in Python? Default ThumbnailHow to Count the Number of lines, Words, and, Characters in a Text File from Terminal? Default ThumbnailHow to Save Pandas Dataframe as gzip/zip File? Default ThumbnailBasic Linux/Mac OS X Commands that Help You to Get Started

Filed Under: Gzip, Linux Tips, tar + gzip, Tar Archive Tagged With: compressed tar, gzip, Linux Tips, tar + gzip, tar archive

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version