hometools

Get a Sorted Array of File Names with jq

April 2026
#!/bin/bash

find ./samples -type f -maxdepth 1 -exec basename {} \; \
  | sort \
  | jq -Rs \
  'split("\n") | map(select(length > 0)) | {files: .}' \
  > output.json
output.json
{
  "files": [
    "alfa.txt",
    "bravo.txt",
    "charlie.txt",
    "delta.txt",
    "echo.txt"
  ]
}