File size check, Linux

Issue

File size check

file_path="/home/d-vm/"
cd $file_path
# file_path directory
file=($(cat video.txt | xargs ls -lah | awk '{ print $9}'))
# get name in video.txt --- two files for example VID_141523.mp4 VID_2_141523.mp4
minimumsize=1
actualsize=$(wc -c <"$file")
if [ $actualsize -ge $minimumsize ]; then
    echo $file size $actualsize bytes
else
    echo error $file size 0 bytes
fi

VID_141523.mp4 file corrupted during conversion. its size 0 bytes

Script output—- error VID_20220709_141523.mp4 size 0 bytes

video.txt

  • VID_141523.mp4
  • VID_2_141523.mp4

How to add this construct to the loop ?
It should check all files in the list video.txt

Solution

You probably mean – "how to loop instead of this: file=($(cat video.txt | xargs ls -lah | awk '{ print $9}'))" (which is pretty horrible by itself (*)).

You should just use a while loop:

cat video.txt | while read file; do
   # something with $file
done

Also, please use stat -c%s FILE instead of wc (which, BTW, also takes a file – you can use wc -c FILE instead of using input redirection), as that looks just at the file system information to check the size, instead of loading and counting each byte.

(*) doing ls -lah and then awk '{print$9}' is the same as just doing ls, but there are other issues with this code that is very not bash idiomatic.

Answered By – Guss

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published