Snake Eyes
Snake Eyes

Reputation: 16764

Get last item of array returned by find command in bash

I have command:

bigfiles=$(find "${path}/" -printf '%s %p\n'| sort -nr | head -2 | sed 's/^[^ ]* //')

Now I want to get last item like

anotherfile=$bigfiles[1]

and it seems to be empty

How to get n-th element of results from find command ?

Upvotes: 0

Views: 996

Answers (1)

Jetchisel
Jetchisel

Reputation: 7801

by using mapfile which is a bash4+ feature, might do what you wanted.

mapfile -t bigfiles < <(find "${path}/" -printf '%s %p\n'| sort -nr | head -2 | sed 's/^[^ ]* //')

anotherfile=${bigfiles[-1]}
echo "$anotherfile"
  • -1 is the last element/item in the array.

  • If you're just interested in the last item with find's output , you can probably pipe the output to tail or maybe head depending on what you're trying to do with it.

Upvotes: 1

Related Questions