119k views
4 votes
1. Write a bash script to create 3 files of different size greater than 1700 kb and less than 1800 kb . The 2. Content of the file must be text characters. File content generated by your script can be characters of your choice. 3. Archive the 3 files created into a single file using the TAR command. (Show command, Note file size) 4. Gzip each of the 3 files individually (note file size) 5. Gzip the TAR file created in step 2. (Note file size) 6. Place results from actions 3, 4, 5 in a readme file that you submit along with your script. Compression values noted in your readme file will be checked against files generated by your script, therefore your accuracy will be verified.

User Anand Kore
by
5.5k points

1 Answer

2 votes

Answer:

See explaination

Step-by-step explanation:

Bash Script:

#!/bin/bash

echo "Creating files a.txt, b.txt, c.txt..."

#1

# Create a random number between 1700 to 1800. This is the Kb we are looking for

# And then multiply by 1000 (1Kb = 1000 bytes)

r=$(( ($RANDOM % 100 + 1700) * 1000 ))

# head -c specifies the number of chars to read from the input,

# which is /dev/urandom stream in this case.

# Ideally we should have just used r*1000 to specify number of bytes,

# but since we are pipelining it to base64 to get text chars,

# this conversion will happen in the ratio of 4/3.

head -c $r /dev/urandom | base64 > a.txt

# Hence, we trunctate the file to the exact (random number we generated * 1000) bytes

truncate -s $r a.txt

r=$(( ($RANDOM % 100 + 1700) * 1000 ));

head -c $r /dev/urandom | base64 > b.txt

truncate -s $r b.txt

r=$(( ($RANDOM % 100 + 1700) * 1000 ));

head -c $r /dev/urandom | base64 > c.txt

truncate -s $r c.txt

#3 Use tar command to create all_three.tar from a.txt, b.txt, c.txt

tar -cf all_three.tar a.txt b.txt c.txt

echo $(stat -c "%n: %s" all_three.tar)

#4 Gzip a.txt into a.gz (without deleting the original file) and so on

gzip -c a.txt > a.gz

gzip -c b.txt > b.gz

gzip -c c.txt > c.gz

echo $(stat -c "%n: %s " a.gz b.gz c.gz)

#5 Gzip a.txt, b.txt, c.txt into all_three.gz

gzip -c a.txt b.txt c.txt > all_three.gz

echo $(stat -c "%n: %s" all_three.gz)

#6 Gzip the all_three.tar we created in #3

gzip -c all_three.tar > all_three.tar.gz

echo $(stat -c "%n: %s" all_three.tar.gz)

# Check all files we created with ls command sorted by time in ascending order

ls -ltr a.txt b.txt c.txt all_three.tar a.gz b.gz c.gz all_three.gz all_three.tar.gz

User Kevin Dion
by
5.8k points