0

The codes are below, I want to check some files whose file size is less than 410Bytes:

for file in *; do
    if [[ "$file" =~ ^dataset([0-9]+)$ && `du -b $file/${BASH_REMATCH[1]}_conserv.png` -lt 410 ]]; then
    cd $file
    $some_commands
    cd ..
    fi
done

However, when I run this script, it complains like this:

less_than_410.bash: line 2: [[: 13605   dataset4866/4866_conserv.png: syntax error in expression (error token is "dataset4866/4866_conserv.png")

Does anyone have ideas about how to fix this? Thanks!

3 Answers 3

3
du -b file

It will print file size and name. Use cut to get size only:

du -b file | cut -f 1
1
0

Rather than using such a complex expression, just do:

find . -maxdepth 1 -regex '.*/dataset[0-9]+' -size -410c

(Note that -maxdepth and -regex are both gnu extensions to find. Since your question is tagged linux, those options are probably available.)

0

Use bash extended globbing to restrict the file loop:

shopt -s extglob
for file in dataset+([0-9]); do
    num=${file#dataset}
    (( $(stat -c %s "$file/${num}_conserv.png") >= 410 )) && continue
    do stuff here
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.