Write a Bash script that takes the name of a file or directory as an argument an
ID: 3757027 • Letter: W
Question
Write a Bash script that takes the name of a file or directory as an argument and if it is a file, reports the size of the file and the category of the file based on its size. If the file size is greater than 1MB (1048576B), the file is a large file; if the file size is less than or equal to 1MB (1048576B) and the file size is greater than 100KB (102400B), the file is a medium file; otherwise, it is a small file.
Tips: Use a sequence of if statements on the file name or file size to determine the information.
To get the file size, use command du –b and command cut.
Explanation / Answer
/*If you have any query do comment in the comment section else like the solution*/
read filenameordir
if [[ -d $filenameOrDir ]] then
echo "$filenameOrDir is a directory"
elif [[ -f $filenameOrDir ]] then
size=du –b $filename;
if [ size -gt 1048576 ]
then
echo "Large file";
elif [ size -lt 1048576 || size -eq 1048576 ]
if [ size -gt 102400 ]
then
echo "Medium file"
fi
else
echo "Small file"
fi
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.