Saturday, June 4, 2022

[SOLVED] Bash- do only if the directory size is greater than a variable

Issue

I have a directory that contains a lot of directories. I want to loop through each of the directories (inside the current directory) and do some processes only if the directory size is less than a specific value. I prepared a bash script, but I am not able to understand what is the error in it.

#!/bin/bash
SIZE=1933130072
for dir in */; do
    CHECK="`du -shb "$dir" | cut -f1`"
    if [$CHECK -lt $SIZE];then
        echo "do rest of the steps"
    fi
done

Solution

Thanks @Shawn the website helped. It was throwing error because of lack of space in if statement. I am pasting the updated code below.

#!/bin/bash
SIZE=1933130072
for dir in */; do
    CHECK="`du -shb "$dir" | cut -f1`"
    if [ $CHECK -lt $SIZE ] ;then
        echo "do rest of the steps"
    fi
done


Answered By - rmj
Answer Checked By - Marie Seifert (WPSolving Admin)