How can I keep certain characters from appearing in a filename?

hi i know this is irrelevant to the question above but i was wondering how to pt a restriction in the filename in linux. I want that it is impossible to add numbers into the filename, help will be rely great , thanx!

Which command were you thinking of intercepting? Is this to be illegal for the Operating System and software too? That might have unpredictable results, and I'm not sure how you would stop redirection like this:-

echo "Hello world!" > /tmp/Greeting.$$

This is all down to your application design. If the part of your application that writes files is in shell script, then (it's not pretty, but) you could try:-

:
:
:
read filename?"What do you want to save this as?"
if [ "$filename" != "`echo $filename|tr -d "0-9"`" ]
then
   echo "Illegal filename.  No numerics allowed."
   # Take action here
fi

I'm left a little puzzled:-

  • Why are numerics a problem?[]What have you tried so far?[]What are your preferred coding tools?

Regards,
Robin

Hi samirboss...

Take note of rbatte1's reply and add...

Assuming the filename is going to be generated inside a shell script then write a __filter__, to intercept after user entry, to accept ONLY [a-z] [A-Z] and maybe underscores[_].

Also limit the file length to a fixed value of your choice but inside the maximum allowed by OS/FS that you are using.

If any of those parameters is/are amiss then clear the typed in variable and re-cycle until a correct format is created.

Let us see what you have tried as there are many who are only too willing to help...

1 Like