Find files where filename ends with space

Hello all,

I would like to find files where the filename ends with a space - like Test.dat

I tried it with:

#!/bin/bash
for file in $(find . -name 'Test.dat*')
do
   echo "($file)"
done

I found the file I have searched for - but unfortunately the output is as:

(Test.dat)

But I would like to have the real name:

(Test.dat )

How could I do this?

Hello API,

Could you please try following and let me know if this helps you(haven't tested it though).

find -type f -name "* "

Thanks,
R. Singh

@RavinderSingh13: I tried it, but it is same output as described: (Test.dat)

The problem is that the for loop is parsing the arguments it gets, based on the space.

If you let find control the loop, it should work:-

find . -name "Test.*" -exec echo "({})" \;

I get this output:-

$ touch "Test.dat "
$ find . -name "Test.*" -exec echo "({})" \;
(./Test.dat )
$

I hope that this helps, but I'm sure you need to use it in some wider context. How can we help you there?

Robin

The IFS (input field separator) default includes a space so even though "find" is pulling the right file name when the assignment happens to the variable "file" in your for loop the trailing spaces get chopped.

I think you may be looking for:

#!/bin/bash

IFS=$'\n'
for file in $(find . -name '*\ ' )
do
   echo "($file)"
done

Hello API,

I don't think there is a need of a loop and then putting find command's value to it, you could only use find command to print the file names.

Thanks,
R. Singh

If there is no sub directory, and not too many files, then you can simply print them

printf "(%s)\n" Test.dat*

RavinderSingh13's response works for me (tested in bash and zsh):

-0-1- ~/tmp  > echo xx >'ab '
-0-1- ~/tmp  > find . -name '* ' -exec echo '({})' \;
(./ab )

As you can see, I can clearly see the space after ./ab . If you don't see a space with this command, it means that your file doesn't have one at the end.

Thanks a lot for your help...

The best solution for me is the hint of ronaldxs:

IFS=$'\n'
for file in $(find . -name '*\ ' )
do
   echo "($file)"
done

Thats fine for me.

---------- Post updated at 03:59 PM ---------- Previous update was at 03:56 PM ----------

@rovf:

Sorry, but all what I get is:

({})
({})
({})
({})

---------- Post updated at 04:02 PM ---------- Previous update was at 03:59 PM ----------

@ravf: Ah yes, now I tested it on my Linux machine - there it works as you described!!

The Problem I have is on an AIX...

Why is using a for loop better then the solution that has been suggested earlier ?

find . -type f -name '* ' -exec echo "({})" \;

--
If you insist on the for loop approach,
On AIX, you would need to use:

IFS="
"

Some Unix find cannot handle -exec echo "({})" !
My previous post suggested printf. It worked for the current directory only. Likewise the external printf can be used in find

find . -type f -name '* ' -exec printf "(%s)\n" {} +

Should work on all LUnix.
Comment on the $ quoting: only bash,zsh,ksh93 support the $'\n' .

You wrote the find command differently from what I gave in my example ....