How to create json with file name, size and version in a directory?

I want to create json holding file name, size and version of files inside a directory. Here version number is a string written inside every file (each file is a lua file containing function like

function M.version() return "1.0"
end

will tel the version number) I need a bash script that will create json consisting of file name, size and it's corresponding version(taken from inside the file)

eg: I've some lua files, at "/home/anudeep/source" are afile.lua bfile.lua,cfile.lua,dfile.lua,efile.lua,ffile.lua,gfile.lua and inside every lua file there's version function like

function M.version() return "1.0" end

or

function gfile.version() return "1.0" end

my json file shuld be file_info.json containing

{"avaible_files": [ ["afile.lua",2056,"1.0"], ["bfile.lua",948,"2.0"], ["cfile.lua",1054,"1.0.1"], ["dfile.lua",3085,"3.0"], ["efile.lua",9685,"1.0.0"], ["efile.lua",6985,"1.0.2"], ["ffile.lua",9996,"3.1.0"], ["gfile.lua",785,"2.0.0"] ]
}

I don't know much about shell/bash scripting and satrted learning. In that process, tried this command to get file name and version

grep -rs -A1 version .| awk '/return/ {print $ response is ./afile.lua-"1.0"

getting this respone

./bfile.lua-"2.0" ./cfile.lua-"1.0.0.1" ./dfile.lua-"3.0" ./efile.lua-"1.0.0" ./ffile.lua-"1.0.2" ./gfile.lua-"2.0.0"

But can't able to write script to create json. Small help also appriciated thanks in advance

2

1 Answer

I'm still not sure exactly what you are asking for, however you could consider something like

#!/bin/bash
shopt -s nullglob
chunk=1000
for f in *.txt; do size=$(stat -c %s "$f") parts=$(( size / chunk + (size % chunk > 0) )) vers=$(grep -sm1 -Po 'return\s+\K\S+' "$f") printf '["%s",%d,%s,%d]\n' "$f" "$size" "$vers" "$parts"
done | jq -n '{"available_files": [inputs]}'

You could incorporate the actual split command into the same loop, as split -b "$chunk" -d "$f" "$f"

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like