sattu sattu - 3 months ago 9
Bash Question

Get filepaths for uniqe files in bash

I have following folder structure:

/a/b/c/d/e.txt
/a/b/c/e.txt
/a/b/m.txt


I want the output to be

/a/b/c/d/e.txt
/a/b/m.txt


Any of the path to e.txt would work. So far, I am able to filter out the unique files, but I am loosing value of full path.

find . -name '*.txt' | awk -F "/" '{print $NF}' | uniq


Output is

e.txt
f.txt


Is there any way using awk, sed or any other command to print the full path in above scenario?

Answer

You can use awk:

awk -F/ '!seen[$NF]++' file

/a/b/c/d/e.txt
/a/b/m.txt

seen is an associative array with key as $NF (last field).