Take the 2-minute tour ×

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
    cd ..

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!

share|improve this question

3 Answers 3

up vote 3 down vote accepted
du -b file

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

du -b file | cut -f 1
share|improve this answer
Or just use stat -c %s file. –  Michał Politowski Nov 20 '12 at 7:54

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.)

share|improve this answer

Use bash extended globbing to restrict the file loop:

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

Your Answer


