An archive is a simple process of storing a group of files or folders into one file. Sometimes we may need to create a compressed archive out of a single folder so that we can share it with others or for something else. In Linux, to ease the task, we can create a shell script to automate the process of creating an archive by simply providing the folder name only.
Using tar command:
Tar is an acronym for Tape Archive. It was initially created in January 1979 at AT&T Bell Laboratories by John Gilmore. The main purpose of creating a tar utility is to efficiently create one archive from many files. The tar command creates a single archive from many files. This newly created archive is also called a tarball. tar can be used to extract the archive also.
Syntax:
tar [option] [archive-filename] [file-or-folder-to-be-archived]
Here are some of the most used options:
Sr. no.
Options (short forms)
Options (expanded forms)
Description
1.
-c
–create
To create Archive
2.
-x
–extract
To extract one or more file(s) from the archive
3.
-f
–file=archive-name
To create an archive with a given filename/archive-name
4.
-t
–list
To list the names of all the files in the archive.
5.
-u
–update
To update the file in archives and add files to the end of the archive.
6.
-v
–verbose
To display Verbose Information.
Note: Here, archive-name should be replaced by the name that you want to give to the archive.
Code:
#!/bin/bash
# Here we are checking if the directory name
# is provided in the argument or not.
# -z will check for the null string
# and $1 will check if the positional argument
# is passed or not
if [ -z "$1" ]; then
# If the name of the folder was not specified
# in the argument
# Then the following message will be displayed
# to the screen
echo "Warning : Please provide the folder name as an argument"
exit 0
fi
# We need to verify whether the directory name
# entered by user really exists or not
# -d flag will be true if the directory name
# exists
if [ -d "$1" ]; then
# if directory control will enter
# creating a variable filename to hold the
# new file name i.e. new_archive current date
# it will end with the extension ".tar.bz2".
filename="new_archive $(date '+%d-%m-%y').tar"
# Using tar --create option to create the
# archive and --file to set the new filename
tar --create --file="$filename" "$1"
echo "Archive successfully created."
# if the folder name does not exists
# we will simply display the following message
# to the screen
else
echo "WARNING: Directory name doesn't exists: $1"
fi
Output:
Successfully Archived
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now !
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...