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


 
Thread Tools Search this Thread
Operating Systems Linux How can I keep certain characters from appearing in a filename?
# 1  
Old 06-06-2014
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!
Moderator's Comments:
Mod Comment This posting was initially given as a reply to an unrelated thread. It has been moved here.

Last edited by Don Cragun; 06-06-2014 at 04:33 PM.. Reason: Note that new thread has been created.
# 2  
Old 06-09-2014
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:-
Code:
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:-
Code:
:
:
:
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
# 3  
Old 06-09-2014
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...
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove the last 9 characters of a filename

Hi All! Please can someone help, I have a dir with the following files: ~-rw-r--r-- 1 emmuser users 2087361 Oct 16 15:50 MPGGSN02_20131007234519_24291.20131007 -rw-r--r-- 1 emmuser users 2086837 Oct 16 15:50 MPGGSN02_20131007233529_24272.20131007 -rw-r--r-- 1 emmuser ... (7 Replies)
Discussion started by: fretagi
7 Replies

2. Shell Programming and Scripting

Filename rename with characters of file

Hi, I need a bit of help. I've used awk to get the first 7 characters of a file - awk '{print substr($0,0,7)}' test.csv How do I now take this variable to rename test.csv to variable.csv ? Any help or advice would be greatly appreciated! (2 Replies)
Discussion started by: sianm
2 Replies

3. Shell Programming and Scripting

Filter last 2 characters of a filename

I have following script to 'archive' some logfiles: for APPHOME in `cat $HOME/archive/apps.cfg` do . $APPHOME/archive/parms.cfg LOGFILES=$(grep "^LOGFILE=" $APPHOME/archive/parms.cfg) for I in $LOGFILES do LOGPATH=$(echo $I |awk -F'=' '{... (2 Replies)
Discussion started by: oliware
2 Replies

4. Shell Programming and Scripting

removing a range of characters in a filename

hi, I have quite a bunch of files with annoyingly long filenames. I wanted to cut the range of characters from 9-18 and just retain the first 8 characters and the .extension. any suggestion how to do it. thanks much. original filename: 20000105_20000105_20100503.nc.asc output filename:... (4 Replies)
Discussion started by: ida1215
4 Replies

5. UNIX for Dummies Questions & Answers

Help with Removing extra characters in Filename

Hi, It's my first time here... anyways, I have a simple problem with these filenames. This is probably too easy for you guys: ABC_20101.2A.2010_01 ABD_20103.2E.2010_04 ABE_20107.2R.2010_08 Expected Output: ABC_20101 ABD_20103 ABE_20107 The only pattern available are the ff: 1) All... (9 Replies)
Discussion started by: Joule
9 Replies

6. Shell Programming and Scripting

replace and add characters in filename

I have files named like ABAB09s099E1AAV1.pdf and ABAB09s099E2AAV1.pdf in a directory. I need to add _Lop in the end of the name, like ABAB09s099E1AAV1_Lop.pdf for all files. For files with E2 in the name I also have to replace 99 with 88 in the filename, that would be ABAB09s088E2AAV1_Lop.pdf ... (3 Replies)
Discussion started by: hakkar
3 Replies

7. Shell Programming and Scripting

remove special characters from filename recursively

hi: i have several thousand files from users and of course they use all kind of characters on filenames. I have things like: My special report (1999 ) Lisa & Jack's work.doc crazy. How do I remove all this characters in the current dir and subdirs too? Thanks. (3 Replies)
Discussion started by: jason7
3 Replies

8. Shell Programming and Scripting

Open filename with special characters in Perl

Hi All, I am facing a weird problem. I have got a directory structure copied from windows to Linux. Some of the folders are named like /gfs/data/Dow Jones $5/ DJ FCBOT_O_tick_1998.zip /gfs/data/Dow Jones $5/ DJ FCBOT_O_tick_2000.CSV /gfs/data/Dow Jones... (6 Replies)
Discussion started by: mitrashatru
6 Replies

9. AIX

Randomly appearing control characters in text files

Hi, From some time, we have noticed that our ascii files have started corrupting due to the presence of some random control characters (^@, ^M, ^H, ^D). The characters appear randomly on any file after the process that creates the file finishes. If we rerun the process, the files re creates... (0 Replies)
Discussion started by: aakashahuja
0 Replies

10. UNIX for Dummies Questions & Answers

Strange Characters in Filename

Hi folks. None of the conventional methods are working for my dilemma: I have a file in my root directory that has a name comprised of strange characters. When I do an ls, it just hangs at that file until I do a Cntrl-C. rm ./filename & rm \filename do not work. I am entering the... (4 Replies)
Discussion started by: kristy
4 Replies
Login or Register to Ask a Question