Script to find folders with spaces and end of files and directories

Hi
I need a script that can search through a set of directories and can locate any file or directory that has a space at the end
Filename(space)
Foldername(space)
I then need to remove that space within the script
Hope someone can help
thanks in advance
Treds

Using some posix-sh, ksh, bash, ...:

for f in *" "
do
 echo "<$f>"
 new=${f/% /}
 # no overwrite, if there is   xxx<space> and xxx
 if [ ! -e "$new" -a "$new" != "$f" ] ;
 then
        mv "$f"  "$new"
 else
        echo "can't do it"
 fi
done

hi
i could not get that to work. (please excuse bit of a novice)
I copied and pasted what you posted into a script.
I generated a folder
inside the folder i created 2 files and 2 folders one with space and one without
i ran script from inside that directory and got a error. i have pasted the steps i did along with the script. Also if this script work would in drill done to multiple folder depths as this is my made problem

drwxrwxrwx   4 root root   8192 Jul 15 17:58 .
drwxrwxrwx  17 lp   lp     8192 Jul 15 17:42 ..
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture 1�
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture 2
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test1�
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test2
-rwxrwxrwx   1 root root    217 Jul 15 17:58 test.sh
[root@TAG-468 TEST]# ./test.sh
<* >
mv: cannot stat `* ': No such file or directory
[root@TAG-468 TEST]# more test.sh
#!/bin/sh

for f in *" "
do
 echo "<$f>"
 new=${f/% /}
 # no overwrite, if there is   xxx<space> and xxx
 if [ ! -e "$new" -a "$new" != "$f" ] ;
 then
        mv "$f"  "$new"
 else
        echo "can't do it"
 fi
done

thanks
Treds

Please post the output from:

ls -la | sed -n l

And:

ls -lab

Based on "kshji" script execution results I don't believe that the trailing character is a space character.

hi output

[root@TAG-468 TEST]# ls -la | sed -n l
total 3568$
drwxrwxrwx   4 root root   8192 Jul 15 17:58 .$
drwxrwxrwx  17 lp   lp     8192 Jul 15 17:42 ..$
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture 1\357\200\250$
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture 2$
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test1\357\200\250$
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test2$
-rwxrwxrwx   1 root root    217 Jul 15 17:58 test.sh$
You have new mail in /var/spool/mail/root
[root@TAG-468 TEST]# ls -lab
total 3568
drwxrwxrwx   4 root root   8192 Jul 15 17:58 .
drwxrwxrwx  17 lp   lp     8192 Jul 15 17:42 ..
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture\ 1�
-rwxrw-rw-   1 root root 886004 Jun 18 18:26 Picture\ 2
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test1�
drwxrwxrwx   2 root root   8192 Jul 15 17:42 test2
-rwxrwxrwx   1 root root    217 Jul 15 17:58 test.sh
[root@TAG-468 TEST]#

thanks
treds

These are not trailing spaces:

The \nnn format gives you the actual character in octal. These three characters are all outside the normal printable range and may look like spaces in a "ls" display.

There are three trailing characters at the end of each of these names:
\357
\200
\250

It is possible to generate these characters in shell and use them in commands:

e.g.

SUFFIX=echo "\0357\0200\0250\c"

ls -la *"${SUFFIX}"

thanks but think thats a bit over my head

See unix "man ascii".
A character can be represented by at least three ways for the same bit pattern in addition to the display character. Octal, decimal, hexadecimal.
In this case we are looking at the characters in octal just to find out what they are.

In your case there are some characters in the file and directory names which are outside the normal printable range. Unix won't stop you creating a file with pretty much any name. It is inadvisible to create files or directories with non-printing characters but it is easy to do by accident if say your erase key is not set correctly or a program does not validate.

Do you have lots of these files?
Are the trailing characters always the same?
Are you prepared to fix them one-by one or are you looking for a repair script?

hi methly
On the mac i see it as a trailing space and that is always what causes be the problem.
There are hundreds of them which i am doing once by one but i would like a script that will do the task
treds