-1

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
rmj
  • 119
  • 3
  • Start by running your script through https://shellcheck.net – Shawn Jun 01 '22 at 06:18
  • Spaces are important delimiters in shell syntax; don't add or omit them unless you know it's one of the places where they're optional. See ["Why should there be spaces around `[` and `]` in Bash?"](https://stackoverflow.com/questions/9581064) and ["Why equal to operator does not work if it is [not\] surrounded by space?"](https://stackoverflow.com/questions/4977367) and ["Why does Zsh complain of my variable assignment as 'command not found'?"](https://superuser.com/questions/1669091) and... – Gordon Davisson Jun 01 '22 at 06:54

1 Answers1

0

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
rmj
  • 119
  • 3
  • See https://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts, it explains why you should use `$( du ... )` instead of backticks. – Nic3500 Jan 22 '23 at 05:24