185k views
0 votes
Write a Bash script that takes the name of a file or directory as an argument, 1. reports if it is a directory or a file, and if the user has read, write and execute permission on the file or directory, and 2. 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. 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. Read their manual for how to use them.

1 Answer

6 votes

Answer:

see explaination

Step-by-step explanation:

#!/bin/bash

mkdir sampleDir #I am just creating sample Directory

touch sampleFile #Creating sampleFile

echo 'Adding some information' >> sampleFile

cat sampleFile

echo "Listing the contents of the current directory:"

ls

file_size_kb=`du -k sampleFile | cut -f1` #Stores file size in KB, 1024KB = 1MB

echo $file_size_kb

if [ -d $1 ]; then

echo "It is a directory"

if [ -w $1 ]; then

echo "It has write permission"

fi

if [ -x $1 ]; then

echo "It has execute permission"

fi

if [ -r $1 ]; then

echo "It has read permission"

fi

if [ $file_size_kb -lt 1024 ]; then

echo "Small file"

elif [ $file_size_kb -gt 1024 ]; then

echo "Large file"

elif [ $file_size_kb -le 1024 && $file_size_kb -ge 1024 ]; then

echo "Medium file"

fi

elif [ -f $1 ]; then

echo "It is a file"

if [ -w $1 ]; then

echo "It has write permission"

fi

if [ -x $1 ]; then

echo "It has execute permission"

fi

if [ -r $1 ]; then

echo "It has read permission"

fi

if [ $file_size_kb -lt 1024 ]; then

echo "Small file"

elif [ $file_size_kb -gt 1024 ]; then

echo "Large file"

elif [ $file_size_kb -le 1024 && $file_size_kb -ge 1024 ]; then

echo "Medium file"

fi

fi

Check attachment for output and screenshot

Write a Bash script that takes the name of a file or directory as an argument, 1. reports-example-1
Write a Bash script that takes the name of a file or directory as an argument, 1. reports-example-2
User Pkavanagh
by
4.3k points