Create a Bash Script (UNIX) The script you create must use only relative paths f
ID: 3845991 • Letter: C
Question
Create a Bash Script (UNIX)
The script you create must use only relative paths for all paths in the code. You must assume that the script is being run from the ~ directory.
Create a shell script (~/Documents/cis90/sorter.sh) that loops through all items in a directory given by the user using the "read" builtin. The script must validate the input and use a loop to only allow the script to proceed after the validation is complete. It must validate that the directory given is a directory.
The script, while iterating over the items in the directory, should test the items to see if they are directories or files.
If they are directories then the script needs to loop over the directory and count the number of items inside of the directory (not recursively). The script needs to output both the name of the directory and the number of files (and folders) inside of it to the same line of a file named "~/sorter/directory_listing.txt".
If they are files they need to be examined for size using du with human readable output. Your script will have to write the size followed by a space then the file name to a new line of the file "~/sorter/files_and_sizes.txt".
After the loop finishes the files_and_sizes.txt file needs to be sorted human readable then output to a file ~/sorter/files_sorted.txt
The directory_listing.txt file needs to be sorted reverse by the number of items in the directory and output to a file named ~/sorter/dir_reverse.txt
You must upload the script file to this assignment AND leave it in the specified directory location.
All code must be thoroughly commented.
Explanation / Answer
NOTE: I have gsort command installed in my system which has the sort with -h option. If you have updated sort command with -h option you can replace it with that. Do a "man sort" to check if you have '-h' option available.
Code:
#!/bin/bash
# script name: sorter.sh
# removing the output files files_and_sizes.txt and directory_listing.txt if they already exist
test -e ~/sorter/files_and_sizes.txt && rm ~/sorter/files_and_sizes.txt
test -e ~/sorter/directory_listing.txt && rm ~/sorter/directory_listing.txt
# Taking directory as input from the user
echo "Enter the directory name: "
read dir
# if the given directory as input is a directory
if [ -d $dir ]
then
# getting all the files in input directory
files_in_dir=`ls $dir`
# changing to that directory
cd $dir
# Iterating through all files in a given directory
for file in $files_in_dir
do
# if file is a regular file in the directory
if [ -f $file ]
then
# writing size information in human readable format to the file
du -h $file>>~/sorter/files_and_sizes.txt
fi
# if file is a directory
if [ -d $file ]
then
no_of_files=0
# iterating through all files in a directory
for filename in `ls $file`
do
# counting number of files in a directory
((no_of_files++))
done
# writing directory name and the number of files in it to the file
echo "$file $no_of_files">>~/sorter/directory_listing.txt
fi
done
# going back to previous directory
cd ..
# sorting the file files_and_sizes.txt by first column which is size information in human readable format
gsort -h -k1 ~/sorter/files_and_sizes.txt > ~/sorter/files_sorted.txt
# reverse sorting the file directory_listing.txt by second column which is number of items in a directory
gsort -k2 -nr ~/sorter/directory_listing.txt > ~/sorter/dir_reverse.txt
# if the user input is not a directory display the below message to user
else
echo "Given file $dir is not a directory. Please try again..."
fi
Execution and output:
Unix Terminal> ./sorter.sh
Enter the directory name:
tst
Given file tst is not a directory. Please try again...
Unix Terminal> ./sorter.sh
Enter the directory name:
test
Unix Terminal>
Unix Terminal> pwd
/Users/krishna/sorter
Unix Terminal> ls -trlh
total 32
-rw-r--r-- 1 krishna 1896053708 35B Jun 4 16:30 files_sorted.txt
-rw-r--r-- 1 krishna 1896053708 35B Jun 4 16:30 files_and_sizes.txt
-rw-r--r-- 1 krishna 1896053708 31B Jun 4 16:30 directory_listing.txt
-rw-r--r-- 1 krishna 1896053708 31B Jun 4 16:30 dir_reverse.txt
Unix Terminal> cat files_and_sizes.txt
4.0K testfile
2.8M x
8.0K y
5.6M z
Unix Terminal> cat files_sorted.txt
4.0K testfile
8.0K y
2.8M x
5.6M z
Unix Terminal> cat directory_listing.txt
an_dir 10
testdir 4
testdir2 1
Unix Terminal> cat dir_reverse.txt
an_dir 10
testdir 4
testdir2 1
Unix Terminal>