'How to make each row an element of the table? [duplicate]

I would like each row generated by my command to be an element of an array but when I try to see how many elements are in my array, it tells me that there is only one.

#!/bin/bash

var=$(find "$1" 2>/dev/null)
echo "$var"
t=("$var")
echo "$t"
echo "${t[1]}"
echo "${#t[*]}"

In fact I would like to iterate on my elements. I tried another way but it doesn't work either :

#!/bin/bash

find "$1" 2>/dev/null | while read line
do
        code_fichier=$(md5sum "$line" 2>/dev/null)
        echo "$code_fichier" >> list_md5sum.txt 2> /dev/null
done

cat list_md5sum.txt | while read line
do
        var=$(grep "$(echo "$line" | cut -f1 -d" ")" list_md5sum.txt)
        echo "$var"
done

rm list_md5sum.txt

I want to have a grep on the line element only once but, by iterating, the grep is done several times (the number of lines there are in the file) but I don't see how to avoid that

I thank you for your help and wish you a good day



Solution 1:[1]

Reading between the lines, I'm guessing you are trying to find duplicate files by hashing. Perhaps try this.

find "$1" -exec md5sum {} + 2>/dev/null |
sort |
awk '$1==hash { if(p) print p; p="";
    print; next }
  { hash=$1; p=$0 }'

This will break in interesting ways if you have file names with newlines in them. For a fuller treatment, perhaps see https://mywiki.wooledge.org/BashFAQ/020

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tripleee