Skip to content

file

A library of file related functions.

lb_remove_extension filename

Return a file name with the file extension removed.

Parameters:
Name Type Description Default
filename string The filename from which to remove the file extension Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

Note

The filename can also be a file path.

Note

The returned filename excludes all parts of the file extension.

lb_get_extension filename

Return a file extension from a filename.

Parameters:
Name Type Description Default
filename string The filename from which to get the file extension Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

Note

The filename can also be a file path.

Note

The returned file extension includes all parts of the file extension.

lb_get_filename filepath

Return a filename from a file path.

Parameters:
Name Type Description Default
filepath string The filepath from which to get the filename Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

lb_get_dir_path filepath

Return a directory path from a file path.

Parameters:
Name Type Description Default
filepath string The filepath from which to get the directory path Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

lb_get_abs_path filepath

Return an absolute path from a relative file path.

Parameters:
Name Type Description Default
filepath string The relative filepath from which to get the absolute path Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

lb_get_mime_type filepath

Return the mime type of a file.

Parameters:
Name Type Description Default
filepath string The filepath from which to get the mime type Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

lb_count dirpath

Return the number of specific files or files and directories in a dirpath glob.

Parameters:
Name Type Description Default
dirpath glob The dirpath glob from which to get the number of specific files or files and directories Required
Examples:
Files and directories count
lb_count ~/Downloads/*
Directories count
lb_count ~/Downloads/*/
Specific files count
lb_count ~/Downloads/*.jpg
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

Note

dirpath must not be in quotes in order for the glob to evaluate properly.

Note

This function returns the number of files and directories with the /* glob pattern.

lb_count_files "dirpath"

Return the number of files or files in a dirpath glob.

Parameters:
Name Type Description Default
dirpath glob The dirpath glob from which to get the number of files Required
Examples:
Files count
lb_count_files "~/Downloads/*"
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

Note

dirpath must be in quotes in order for the glob to be passed properly.

lb_get_lines filepath

Return the number of lines in a file.

Parameters:
Name Type Description Default
filepath string The filepath from which to get the number of lines Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

lb_extract filepath marker1 marker2

Return the text between two line markers in a file.

Parameters:
Name Type Description Default
filepath string The filepath from which to extract the lines Required
marker1 string The line of which to start the extraction Required
marker2 string The line of which to end the extraction Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}

Note

marker1 and marker2 are not included in the return value.

Note

marker1 and marker2 need to be exact matches of the lines.

Note

Only the the lines between the first instances that match marker1 and marker2 are returned.

lb_get_parent_dir_name path

Return the name of the parent folder of either a file or a directory.

Parameters:
Name Type Description Default
path string The filepath or directory path from which to get the name of the parent folder Required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/file"

function do_file() {
  printf "file\n"

  file="file.tar.gz"
  printf "filename: %s\n" "${file}"

  s=$(lb_get_extension "${file}")
  printf "lb_get_extension: %s\n" "${s}"

  s=$(lb_remove_extension "${file}")
  printf "lb_remove_extension: %s\n" "${s}"
}

function do_path() {
  printf "path\n"

  path="../test/test_files/file1"
  printf "rel path: %s\n" "${path}"

  path=$(lb_get_abs_path "${path}")
  printf "lb_get_abs_path: %s\n" "${path}"

  s=$(lb_get_filename "${path}")
  printf "lb_get_filename: %s\n" "${s}"

  s=$(lb_get_dir_path "${path}")
  printf "lb_get_dir_path: %s\n" "${s}"

  s=$(lb_get_mime_type "${path}")
  printf "lb_get_mime_type: %s\n" "${s}"

  s=$(lb_get_lines "${path}")
  printf "lb_get_lines: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*/"
  printf "dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files and dirs path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count files and dirs: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*.tar.gz"
  printf "specific files path: %s\n" "${s}"

  # shellcheck disable=SC2086
  s=$(lb_count ${s})
  printf "lb_count specific files: %s\n" "${s}"

  s="$(lb_get_dir_path "${path}")/*"
  printf "files path: %s\n" "${s}"

  s=$(lb_count_files "${s}")
  printf "lb_count files: %s\n" "${s}"

  s=$(lb_extract "${path}" "marker1" "marker2")
  printf "lb_extract: %s\n" "${s}"

  printf "filepath: %s\n" "${path}"
  s=$(lb_get_parent_dir_name "${path}")
  printf "lb_get_parent_dir_name: %s\n" "${s}"
}

function main() {
  do_file
  do_path
}

main "${@}"
Show source code in file
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

# Remove file extension
function lb_remove_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1%%.*}"
}

# Get file extension
function lb_get_extension() {
  lb_check_args_num "${@}" 1
  printf '%s\n' "${1#*.}"
}

# Get the file name
function lb_get_filename() {
  lb_check_args_num "${@}" 1
  basename "${1}"
}

# Get the directory path
function lb_get_dir_path() {
  lb_check_args_num "${@}" 1
  dirname "${1}"
}

# Get the absolute path
function lb_get_abs_path(){
  lb_check_args_num "${@}" 1
  s=$(readlink -f "${1}")
  printf '%s\n' "${s}"
}

function lb_get_mime_type(){
  lb_check_args_num "${@}" 1
  file -b --mime-type "${1}"
}

function lb_get_lines() {
  lb_check_args_num "${@}" 1
  test -f "${1}" || (lb_fail "file does not exist"; return 1)
  count=0
  while IFS= read -r _; do
    ((count++))
  done < "${1}"
  printf '%s\n' "$count"
}

function lb_count(){
  ((${#@} == 0)) && lb_fail "expected at least 1 arg but got 0"
  printf '%s\n' "$#"
}

function lb_count_files(){
  lb_check_args_num "${@}" 1
  count=0
  for i in ${1}; do
    [ -f "${i}" ] && ((count++))
  done
  printf '%s\n' "${count}"
}

function lb_extract(){
  lb_check_args_num "${@}" 3
  test -f "${1}" || (lb_fail "file does not exist"; exit 1)
  extract=
  a=()
  while IFS=$'\n' read -r line; do
    [[ $extract && $line != "${3}" ]] &&
      a+=("${line}")
    [[ $line == "${2}" ]] && extract=1
    [[ $line == "${3}" ]] && extract=
  done < "${1}"
  printf '%s\n' "${a[@]}"
}

function lb_get_parent_dir_name(){
  lb_check_args_num "${@}" 1
  basename "$(dirname "${1}")"
}