-2

I need to search for the file path ending with *Tests.cs having multiple directories. I need to display all the file path in spaces instead of new line. Currently, it is displaying as

./dir1/bin/dir1Tests.cs ./dir2/bin/dir2Tests.cs

How to remove ./ from each file path and display it as follows?

dir1/bin/dir1Tests.cs dir2/bin/dir2Tests.cs

My bash script is:

FILES=$(find . -path '*bin/*' -name *Tests.cs -type f -printf "%p ")
echo $FILES
  • Have you tried passing selected files to bash and removing `./` there? Like `find . -path '*bin/*Tests.cs' -exec bash -c 'echo ${@#./}' bash {} +`? – oguz ismail Oct 08 '21 at 13:01
  • https://unix.stackexchange.com/questions/331696/removing-leading-dots-from-find-command-output-when-used-with-exec-echo-opti/331710 https://stackoverflow.com/questions/2596462/how-to-strip-leading-in-unix-find – KamilCuk Oct 08 '21 at 13:59

1 Answers1

0

If your structure is consistent as in the example, it's easy.

$: echo */bin/*Tests.cs
dir1/bin/dir1Tests.cs dir2/bin/dir2Tests.cs

If there are arbitrary, varying depths, use globstar.

$: shopt -s globstar               # make ** register arbitrary depth subdirs

Then use double-asterisks for arbitrary depths.

$: echo **/*bin/**/*Tests.cs
dir1/bin/dir1Tests.cs dir1/subdir/bin/another/deeperTests.cs dir2/bin/dir2Tests.cs

None of that requires a find.

If you just need to do some other editing, pattern expansion works fine.

files=( ./**/*bin/**/*Tests.cs ) # sticking the unnecessary ./ on to remove
echo "${files[@]#./}"            # strip it from each element as processed
dir1/bin/dir1Tests.cs dir1/subdir/bin/another/deeperTests.cs dir2/bin/dir2Tests.cs
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36