Reputation: 47061
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!
Upvotes: 0
Views: 550
Reputation: 246774
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
Upvotes: 0
Reputation: 212238
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.)
Upvotes: 0
Reputation: 161664
du -b file
It will print file size and name. Use cut
to get size only:
du -b file | cut -f 1
Upvotes: 3