Linux Shell Script that calculates the space occupied by files?

Status
Not open for further replies.

emailaatif786

Always Questioning?
Linux Shell Script that calculates the space occupied by files?
Reply me wth a Linux Shell Script that calculates the space occupied by files?
There can be three levels:

Level 1: Write a script which takes in directory names as arguments and display total space occupied by files.

Level 2: If directory doesn’t exist, then display a warning message “Warning: Directory does not exist…”

Level 3: If directory doesn’t have access permissions then display message “Warning: Directory not accessible…”

Anyone please reply.
 

enjoy

Journeyman
It will be a simple wrapper around "du". Why dont you start writing one and ask issues that you face, instead of asking for a readymade stuff.
 

desiibond

Bond, Desi Bond!
du -ka <directory>

this command should give you size of directory and it's content.
when you pass the directory name as argument to the script, it will be stored in $0 environment vriable and you can use this in the script. all you need to write is a simple if loop.
 
OP
emailaatif786

emailaatif786

Always Questioning?
du -ka <directory>

this command should give you size of directory and it's content.
when you pass the directory name as argument to the script, it will be stored in $0 environment vriable and you can use this in the script. all you need to write is a simple if loop.

A request,
Please write the 'If Loop' for me.

I do not know much of Linux. I am a basic user of Linux. I do not know accurately how to make shell script.
 

azherdigit

Right off the assembly line
First of all IF is a conditional statement not a loop.

Evene im new to shell scripting but here you go!

if [ $FS = "TRUE" ]
then{
command 1 or Function_call;
command 2 or Function_call;
.
.
.
}
else{
command 3 or Function_call;
command 4 or Function_call;
}
fi

#where $FS is a variable and can hold any value depending on the script usage
 

krishnandu.sarkar

Simply a DIGITian
Staff member
I think the syntax wud be

if [ $FS -eq "TRUE" ]
then
command1 / Func()
else
command2 / FUnc()
fi

I think the '{', '}' and ';' doesn't req and '-eq' in place of '='

According to desiibond ur script shud be

if [ -z $0 ]
then
echo "Enter a directory"
else
du -ka $0
fi

Try this. And correct me if I'm wrong.
 
Last edited:

azherdigit

Right off the assembly line
Thanks for correcting me. As i said even im new to shell scripting.

I included the blocks '{' '}' in case there are multiple commands to be executed in a IF statement. And you r right ';' is not required.

we can use = as well as -eq as long as it is in double quotes i guess.

here is a syntax for IF-then-else structure taken from a shell scripting book

In the​
if-then statement, you only have one option of whether or not a command is successful.
If the command returns a non-zero exit status code, the bash shell just moves on to the next
command in the script. In this situation, it would be nice to be able to execute an alternate set
of commands. That’s exactly what the
if-then-else statement is for.
The
if-then-else statement provides another group of commands in the statement:

if​
command

then​
commands​
else​
commands​
fi​
If the command in the​
if statement line returns with an exit status code of zero, the commands
listed in the
then section are executed, just as in a normal if-then statement. If the
command in the
if statement line returns a non-zero exit status code, the bash shell executes
the commands in the
else section.
Now you can modify the test script to look like this:

$ cat test4
#!/bin/bash
# testing the else section
testuser=badtest
if grep $testuser /etc/passwd
then
echo The files for user $testuser are:
ls -a /home/$testuser/.b*
else
echo "The user name $testuser doesn’t exist on this system"
fi
$ ./test4
The user name badtest doesn’t exist on this system
$​

 

krishnandu.sarkar

Simply a DIGITian
Staff member
^^Ya u r rite.....But a small mistake, we can use '=' in case of only string test but not others. As in the case of book "test string1=string2". And in case u need to execute multiple commands u don't need '{', '}'. The 'if' part is terminated then 'else' is found and 'else' part is terminated when 'fi' is found. So we don't need '{', '}' in shell scrips.
 
Status
Not open for further replies.
Top Bottom