0

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 ?

2
  • 1
    $bigfiles is not array. Commented Jun 9, 2020 at 9:58
  • your problem is getting the nth line from an input or convert the multi-line into shell array? Commented Jun 9, 2020 at 10:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.