Home > Blockchain >  File size check, Linux
File size check, Linux

Time:07-11

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

CodePudding user response:

To read a file size stat is the best for use-case here. On a linux machine with GNU stat

$ stat --printf="%s" one
4

This can be looped like

#!/bin/bash
while read -r file ; do
    if [ "$(stat --printf="%s" "$file")" -gt 0 ]
        then
            echo "$file" yes
        else 
            echo "$file" no
    fi
done < video.txt

This is too complicated approach in my opinion. Just use find

$ touch zero
$ echo one > one
$ ll
total 8
-rw-r--r--  1 sverma  wheel     4B Jul 11 16:20 one
-rw-r--r--  1 sverma  wheel     0B Jul 11 16:20 zero

one is a 4 byte file. Use -size predicate. Here means greater than, -type f means only files

$ find . -type f -size  0 -print
./one

You can add a filter for names with something like

$ find . -name '*.mp4' -type f -size  0

CodePudding user response:

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.

  • Related