0

I’m having trouble getting a list of files into an array. arr=( * ) works when i’m in the directory in question, but arr=( path-to-dir/* ) dosen’t seem to work.

arr=( $(ls -1 path-to-dir) works, but from my understanding, using ls -1 is not a good idea.

What’s the best way to capture the files in a directory into an array if it’s not in your $PWD?

  • `arr=( path-to-dir/* )` seems ok, what's wrong with it? – oguz ismail Dec 30 '20 at 04:56
  • I just tried it again, but I think i see what’s going on. My filenames are all rc files so they begin with `.` – camwrangles Dec 30 '20 at 05:02
  • Looks like that method ignores files that begin with a `.` Do you know how to include them? – camwrangles Dec 30 '20 at 05:02
  • 1
    `arr=( path-to-dir/.* )`?? or `shopt -s dotglob` then `arr=( path-to-dir/* )` – David C. Rankin Dec 30 '20 at 05:05
  • I tried `arr=( path-to-dir/,* )` but it adds the `.` and `..` as the first two elements of the array. I can just skip these when I call the array, but I was hoping for a more elegant solution. – camwrangles Dec 30 '20 at 05:10
  • Also that solution skips all files without a `.` – camwrangles Dec 30 '20 at 05:11
  • ```declare -a rc_dir_output local rc_dir_output=( $rc_dir/.* ) declare -i rc_dir_output_length local rc_dir_output_length=${#rc_dir_output[@]} declare -a rc_files local rc_files=( ${rc_dir_output[@]: 2: $rc_dir_output_length} ) for rc in ${rc_files[@]} do rc_basename=${rc##*/} #==> remove path``` – camwrangles Dec 30 '20 at 06:47

0 Answers0