Bash: how to print filename of nth file in folder

I am getting a parsing error on the 773rd folder file. Is it possible to print the filename in bash?

I tried to use this to print it, but it returns empty.

files=(/path/to/files)
echo "${files[773]}"

      

+3


source to share


1 answer


Very close, but you need to make a globe to collect the list into your array, instead of having a list with only one element (parent directory):

files=( /path/to/files/* )
echo "${files[772]}"

      

If you want to represent your filename in a way that is human-readable non-printable characters echo

is the wrong tool. Consider instead:



printf '%q\n' "${files[772]}"

      

If your path comes from a variable, be sure to include its extension, but not the globe symbol:

files=( "$dir"/* )

      

+6


source







All Articles