Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add get first/latest file in a directory #45

Open
vaxul opened this issue Nov 21, 2018 · 2 comments
Open

Add get first/latest file in a directory #45

vaxul opened this issue Nov 21, 2018 · 2 comments
Labels
enhancement New feature or request

Comments

@vaxul
Copy link

vaxul commented Nov 21, 2018

Hi,
first big thanks for the "bible".

I think it might helpful to add more file handling tips like "get filepath from first file in a directory" or "get the filepath for the last modified file in a directory".

Would someone mind to add good solutions to these?

Best regards

@dylanaraps dylanaraps added the enhancement New feature or request label Jan 4, 2019
@ghost
Copy link

ghost commented Oct 3, 2021

first_file() {
# Usage: first_file [DIR]
local "list" "file"
readarray -t list < <( printf -- '%s\n' "${1:-"${PWD}"}/"* )
for file in "${list[@]}"; do
  if [[ -f "${file}" ]]; then
    printf -- '%s\n' "${file}"
    return 0
  fi
done
return 1
}
last_file() {
# Usage: last_file [DIR]
local "list" "file" "list_files" "max"
readarray -t list < <( printf -- '%s\n' "${1:-"${PWD}"}/"* )
for file in "${list[@]}"; do
  if [[ -f "${file}" ]]; then
    list_files+=( "${file}" )
  fi
done
max="${#list_files[@]}"
((max--))
if [[ -n "${list_files[$max]}" ]]; then
  printf -- '%s\n' "${list_files[$max]}"
else
  return 1
fi
}
first_mdate_file() {
# Usage: first_mdate_file [DIR]
local "list" "file" "list_files" "num" "old_file"
readarray -t list < <( printf -- '%s\n' "${1:-"${PWD}"}/"* )
for file in "${list[@]}"; do
  if [[ -f "${file}" ]]; then
    list_files+=( "${file}" )
  fi
done
num=1
for file in "${list_files[@]}"; do
  if [[ "${file}" -nt "${old_file:-"${list_files[$num]}"}" ]]; then
    old_file="${file}"
  fi
  ((num++))
done
if [[ -n "${old_file:-"${list_files[0]}"}" ]]; then
  printf -- '%s\n' "${old_file:-"${list_files[0]}"}"
else
  return 1
fi
}
last_mdate_file() {
# Usage: last_mdate_file [DIR]
local "list" "file" "list_files" "num" "old_file"
readarray -t list < <( printf -- '%s\n' "${1:-"${PWD}"}/"* )
for file in "${list[@]}"; do
  if [[ -f "${file}" ]]; then
    list_files+=( "${file}" )
  fi
done
num=1
for file in "${list_files[@]}"; do
  if [[ "${file}" -ot "${old_file:-"${list_files[$num]}"}" ]]; then
    old_file="${file}"
  fi
  ((num++))
done
if [[ -n "${old_file:-"${list_files[0]}"}" ]]; then
  printf -- '%s\n' "${old_file:-"${list_files[0]}"}"
else
  return 1
fi
}

@Artoria2e5
Copy link

Artoria2e5 commented Aug 12, 2023

What's with the printf-readarray?

list=( "${1:-"${PWD}"}/"* )

works as well and is not fragile to newlines.


Regarding "last modified file", I doubt that's possible. There seems to be no way to replace stat.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants