Bash Archiving and Compressing Files Exercises
1.
Create a Tar Archive:
Write a Bash script that creates a tar archive of a directory named "workarea".
Code:
#!/bin/bash
# Create a tar archive of the directory "workarea"
tar -cvf workarea.tar workarea
# Check if the tar archive was created successfully
if [ $? -eq 0 ]; then
echo "Tar archive 'workarea.tar' created successfully."
else
echo "Failed to create tar archive 'workarea.tar'."
fi
Output:
workarea/ Tar archive 'workarea.tar' created successfully.
Explanation:
In the exercise above,
This script uses the "tar" command to create a tar archive named "workarea.tar" of the directory named "workarea". The -c option is used to create a new archive, -v is used for verbose output (optional), and -f specifies the name of the archive file. The script then checks the exit status of the tar command to determine if the archive was created successfully or not and prints an appropriate message.
2.
Extract a Tar Archive:
Write a Bash script that extracts the contents of a tar archive named 'workarea.tar' into a directory named "extracted_files".
Code:
#!/bin/bash
# Extract the contents of 'workarea.tar' into the directory 'extracted_files'
tar -xf workarea.tar -C extracted_files
# Check if the extraction was successful
if [ $? -eq 0 ]; then
echo "Extraction successful. Files extracted to 'extracted_files' directory."
else
echo "Extraction failed."
fi
Output:
Extraction successful. Files extracted to 'extracted_files' directory.
Explanation:
In the exercise above,
- tar -xf workarea.tar -C extracted_files: This command extracts (-x) the contents of the 'workarea.tar' archive into the directory specified by -C option, which is 'extracted_files'.
- if [ $? -eq 0 ]: This checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a message showing that the extraction was successful. Otherwise, it prints a message indicating the extraction failed.
3.
Create a Gzip Compressed Archive:
Write a Bash script that creates a gzip compressed archive of a directory named "workarea".
Code:
#!/bin/bash
# Create a gzip compressed archive of the directory "workarea"
tar -czf workarea.tar.gz workarea
# Check if the compression was successful
if [ $? -eq 0 ]; then
echo "Compression successful. Archive created: workarea.tar.gz"
else
echo "Compression failed."
fi
Output:
Compression successful. Archive created: workarea.tar.gz
Explanation:
In the exercise above,
- tar -czf workarea.tar.gz workarea: This command creates a gzip compressed archive (-z) of the directory named "workarea". The -c option indicates creating an archive, -f specifies the filename of the archive, and "workarea" is the directory to be archived.
- if [ $? -eq 0 ]: This checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a message indicating that compression was successful and provides the name of the created archive. Otherwise, it prints a message indicating compression failed.
4.
Extract a Tar Archive:
Write a Bash script that extracts the contents of a tar archive named "workarea.tar.gz" into a directory named "extracted_files".
Code:
#!/bin/bash
# Extract the contents of the tar archive "workarea.tar" into the directory "extracted_files"
tar -xzf workarea.tar.gz -C extracted_files/
# Check if the extraction was successful
if [ $? -eq 0 ]; then
echo "Contents of 'workarea.tar.gz' extracted successfully into 'extracted_files' directory."
else
echo "Failed to extract contents of 'workarea.tar.gz'."
fi
Output:
Contents of 'workarea.tar.gz' extracted successfully into 'extracted_files' directory.
Explanation:
In the exercise above,
The Bash script extracts the contents of the tar archive "workarea.tar.gz" into the directory "extracted_files". It then checks if the extraction was successful by verifying the exit status of the "tar" command. If the exit status is 0, it prints a success message; otherwise, it prints a failure message.
5.
Create a Zip Archive:
Write a Bash script that creates a zip archive of a directory named "workarea".
Code:
#!/bin/bash
# Create a zip archive of the directory "workarea"
zip -r workarea.zip workarea
# Check if the zip operation was successful
if [ $? -eq 0 ]; then
echo "Zip archive 'workarea.zip' created successfully."
else
echo "Failed to create zip archive 'workarea.zip'."
fi
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh adding: workarea/ (stored 0%) Zip archive 'workarea.zip' created successfully.
Explanation:
In the exercise above,
- zip -r workarea.zip workarea: This command creates a zip archive named "workarea.zip" containing the contents of the "workarea" directory. The -r option is used to recursively include all files and subdirectories within "workarea".
- if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints the message "Zip archive 'workarea.zip' created successfully." Otherwise, it prints "Failed to create zip archive 'workarea.zip'."
6.
Extract a Zip Archive:
Write a Bash script that extracts the contents of a zip archive named "workarea.zip" into a directory named "new_dir".
Code:
#!/bin/bash
# Create a zip archive of the directory "workarea"
zip -r workarea.zip workarea
# Check if the zip operation was successful
if [ $? -eq 0 ]; then
echo "Zip archive 'workarea.zip' created successfully."
else
echo "Failed to create zip archive 'workarea.zip'."
fi
Output:
updating: workarea/ (stored 45%) Zip archive 'workarea.zip' created successfully.
Explanation:
In the exercise above,
- zip -r workarea.zip workarea: This command creates a zip archive named "workarea.zip" containing the contents of the "workarea" directory. The -r option is used to recursively include all files and subdirectories within "workarea".
- if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints the message "Zip archive 'workarea.zip' created successfully." Otherwise, it prints "Failed to create zip archive 'workarea.zip'."
7.
Create a Tar Archive Excluding Specific Files:
Write a Bash script that creates a tar archive of a directory named "source_directory", excluding files with a ".txt" extension.
Code:
#!/bin/bash
# Create a tar archive of the directory "source_directory" excluding files with a ".txt" extension
tar -cf source_directory.tar --exclude="*.txt" source_directory
# Check if the tar operation was successful
if [ $? -eq 0 ]; then
echo "Tar archive 'source_directory.tar' created successfully."
else
echo "Failed to create tar archive 'source_directory.tar'."
fi
Output:
Tar archive 'source_directory.tar' created successfully.
Explanation:
In the exercise above,
The script creates a tar archive of the directory "source_directory" while excluding files with a ".txt" extension. After creating the archive, it checks the exit status ($?) to determine whether the operation was successful or not and prints an appropriate message.
8.
Compress Multiple Files into a Single Gzip Compressed File:
Write a Bash script that compresses multiple files (e.g., file1.txt, file2.txt) into a single gzip compressed file named "compressed_files.tar.gz".
Code:
#!/bin/bash
# Compress multiple files into a single gzip compressed file named "compressed_files.tar.gz"
tar -czf compressed_files.tar.gz file1.txt file2.txt
# Check if the compression operation was successful
if [ $? -eq 0 ]; then
echo "Files compressed successfully into 'compressed_files.tar.gz'."
else
echo "Failed to compress files into 'compressed_files.tar.gz'."
fi
Output:
Files compressed successfully into 'compressed_files.tar.gz'.
Explanation:
In the exercise above,
- tar -czf compressed_files.tar.gz file1.txt file2.txt: This command creates a gzip compressed tar archive named "compressed_files.tar.gz" containing files file1.txt and file2.txt. The -c option creates a new archive, the -z option compresses the archive with gzip, and the -f option specifies the output file name.
- if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints the message "Files compressed successfully into 'compressed_files.tar.gz'." Otherwise, it prints "Failed to compress files into 'compressed_files.tar.gz'."
9.
Extract Specific Files from a Tar Archive:
Write a Bash script that extracts specific files (e.g., file1.txt, file2.txt) from a tar archive named "archive.tar.gz" into a directory named "extracted_files".
Code:
#!/bin/bash
# Extract specific files from the tar archive "archive.tar.gz" into the directory "extracted_files"
tar -xzf 'compressed_files.tar.gz' file1.txt file2.txt -C extracted_files/
# Check if the extraction was successful
if [ $? -eq 0 ]; then
echo "Files extracted successfully into 'extracted_files' directory."
else
echo "Failed to extract files."
fi
Output:
Files extracted successfully into 'extracted_files' directory.
Explanation:
In the exercise above,
- tar -xzf archive.tar.gz file1.txt file2.txt -C extracted_files/: This command extracts the specified files (file1.txt and file2.txt) from the tar archive "archive.tar.gz" using the -x (extract), -z (gzip), and -f (file) options. The -C option specifies the directory where the files will be extracted (in this case, "extracted_files/").
- if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints the message "Files extracted successfully into 'extracted_files' directory." Otherwise, it prints "Failed to extract files."
10.
Compress a Directory into a Zip Archive with Password Protection:
Write a Bash script that compresses a directory named "workarea" into a zip archive named "archive.zip" with password protection.
Code:
#!/bin/bash
# Prompt the user to enter a password
read -s -p " Input password for zip archive: " password
echo
# Compress the directory "workarea" into a zip archive "archive.zip" with password protection
zip -r --encrypt --password "$password" archive.zip workarea
# Check if the zip operation was successful
if [ $? -eq 0 ]; then
echo "Zip archive 'archive.zip' created successfully with password protection."
else
echo "Failed to create zip archive 'archive.zip'."
fi
Output:
Input password for zip archive: adding: workarea/ (stored 30%) Zip archive 'archive.zip' created successfully with password protection.
Explanation:
In the exercise above,
- read -s -p "Input password for zip archive: " password: This command prompts the user to enter a password for the zip archive. The -s option is used to hide the input.
- zip -r --encrypt --password "$password" archive.zip workarea: This command compresses the directory "workarea" into a zip archive named "archive.zip" with password protection.
- -r option is used to recursively include all files and directories within the "workarea" directory.
- --encrypt option enables encryption for the zip archive.
- --password "$password" specifies the user's password.
- The script then checks the exit status of the zip operation. If the exit status is 0 (indicating success), it prints "Zip archive 'archive.zip' created successfully with password protection." Otherwise, it prints "Failed to create zip archive 'archive.zip'."
Bash Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/bash-script-exercises/archiving-and-compressing-files.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics