Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing strange characters from filenames

Status
Not open for further replies.

ianholmes

Programmer
Mar 24, 2001
61
GB
My application picks up external files from host systems.
I then run a script to reformat them, and transfer them to other servers.
I am finding that, on occasions, strange characters, which might be control characters, are included in the filename, which cause my script to misbehave.
eg A file might look like abc.def, but is actually $abc.$def.
How to I enhance my script to remove the "$" signs, which might be separate characters, or might be part of the "a" and the "d" (i.e. all one character )?
I know about "tr -d", but this doesn't seem to work for filenames, just file contents.
Many thanks
 
Code:
echo 'thin\$g.\$ext' | tr -dc 'a-zA-Z\.'

should return 'thing.ext' if that helps
 
When i delete file's with control characters, i do the following,

make a listing of the directory in a file,
# ls > dirlist
Delete all the files you do not want to delete,
# vi dirlist
If only the files you want to delete are in dirlist then do
# rm `cat dirlist`

Good luck.....
 
To furthur this dicussion use -- after a command.
ex, try this.

$ touch -- -Z
This will create a file with name "-Z"!!.
Now no matter what you do you can't rename it(mv), or edit it(vi,..) or even remove it(rm), since -Z will be read as an option instead of filename.
To get around this use -- again.
$ rm -- -Z # To delete the file
$ mv -- -Z Z.txt # To rename it ot Z.txt
etc etc..
In nutshell -- after a command tells shell command to take anything after it as it is.
Thus this can be used in your case.
 
rm ./-Z vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top