Compress a file or folder by using the bash script

When it is required to transfer a large file or folder of multiple files and folders then it is better to create the compressed file of that file and folder to reduce the size of the file and folder. It makes it easier to transfer files from one location to another location. Many commands exist in Linux to compress the file or folder, such as `tar`, `zip`, `gzip`, etc. The `tar` command is used in this tutorial for compressing files or folders. The full form of the `tar` command is ‘Tape Archive’ and the compressed file of one or more files and folders can be created easily by using this command. This command can be used to decompress any compressed file. The purpose of the different options of this command and the way of compressing any file or folder by using this command in a bash script have been shown in this tutorial.

Syntax:

The syntax of the `tar command is given below.

tar [option] [Compress_Filename] [Original_Filename]

This command has many options to compress a file, decompress a file, show the list of the files of the compressed file, etc. The purpose of the different options of this command is described below.

Option Purpose
-c or –create It is used to compress the file or folder.
-f or –file=Compresed_Filename It is used to compress a file by specifying the compressed file name.
-t or –list It is used to print the list of all files and folders of the compressed file.
-u or –update It updates the compressed file by adding new files or folders in the archive.
-x or –extract It is used to decompress one or more files and folders from the compressed file.
-v or –verbose It is used to display the verbose information of this command.

Compress the File or Folder by using the `tar` command:

Create a bash file with the following script that will take the file or folder name from the user and it will create a compressed file with the `tar` extension if the file or folder name exists in the current location. Here, the ‘-d’ option has been used in the `if` condition to check the value taken from the user is a directory and exists, and the ‘-e’ option has been used in the `if` condition to check the value taken from the user is file and exists. The compressed file will be created if the `if` condition returns true, otherwise, the error message will be printed.


#!/bin/bash
#Take the file or folder name that will be compressed
echo -n "Enter the file or folder name to compress:"
read name
#Check the file or folder exists or not
if [[ -d "$name"  ||  -e "$name" ]]; then
    #Read filename without extension and
    #add .tar extension to create the compressed file
    filename="${name%.*}.tar"
    #Create the compressed file by using the `tar` command
    tar --create --file="$filename" "$name"
    #Print the success message
    echo "The compressed file is created successfully."
else
    #Print the error message
    echo "File or Folder name does not exist: $name"
fi

Conclusion:

The use ‘–create’ option of the `tar` command has been shown in the script. You can use other options to generate the compressed file. I hope, you will be able to compress any type of file or folder after reading this tutorial properly.