Computing.Net > Forums > Unix > Validation of dir/file names

Validation of dir/file names

Reply to Message Icon

Original Message
Name: ICW Infrastructure
Date: April 29, 2003 at 09:26:58 Pacific
Subject: Validation of dir/file names
OS: Windows 95
CPU/Ram: Pentium 2 350 MHz/ 64MB
Comment:

I am working on a Cygwin Bash shell script which amongst other things, reads the names of some directories and files from an input data file. I want the script to be able to flag whether the data read is a valid Unix filename, or valid Unix path, as appropriate.

I am aware that the test command can be used to check that a specified string is the name of a file which exists, a directory which exists, etc. But, in this case, the files and directories will often not exist. Does anyone know of a command or utility which indicates whether an input parameter is a valid filename or directory name?


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: April 29, 2003 at 11:40:54 Pacific
Subject: Validation of dir/file names
Reply: (edit)

What's your definition of a valid Unix file name or Unix path if it doesn't exist? The choices are limitless.

The following does the trick if the object in data.file does exist:

#!/bin/ksh

while read line
do
[ -f $line ] && echo "it's a file"
[ -d $line ] && echo "it's a directory"
done data.file
# there's a less than sign between done and data.file

Regards,

Nails


Report Offensive Follow Up For Removal

Response Number 2
Name: WilliamRobertson
Date: April 29, 2003 at 15:42:16 Pacific
Subject: Validation of dir/file names
Reply: (edit)

I guess if the file/dir didn't already exist, you could always create an empty file using that name and then delete it.


Report Offensive Follow Up For Removal

Response Number 3
Name: ICW Infrastructure
Date: April 30, 2003 at 07:25:07 Pacific
Subject: Validation of dir/file names
Reply: (edit)

Thanks for the suggestions. Re: the question about what is a valid filename, I can say that some filenames considered INvalid for the purposes of this script would be anything that cannot be created, e.g.:

this?would*be*invalid

So I could do a touch on the filename/dirname and see if that returns an error code. And/Or I could simply search the string for special characters. I was just wondering if Unix provided a dedicated utility to do this, but perhaps not.


Report Offensive Follow Up For Removal

Response Number 4
Name: WilliamRobertson
Date: April 30, 2003 at 08:41:25 Pacific
Subject: Validation of dir/file names
Reply: (edit)

Unfortunately you CAN create a file named this?would*be*invalid. Maybe what you need is a test for undesirable filenames, e.g. those containing unprintable or wildcard characters or multiple blanks, or more than say 30 characters long.


Report Offensive Follow Up For Removal

Response Number 5
Name: ICW Infrastructure
Date: May 1, 2003 at 04:29:02 Pacific
Subject: Validation of dir/file names
Reply: (edit)

Yes - the function to do this is going to use pathchk first to confirm Unix isn't unhappy with the filename. This will pretty much be a formality, as Unix allows just about anything. After passing this check, the function will 'manually' look for any 'special' characters, such as wildcards, shell redirection characters, etc. It seems surprising that there is no built-in utility for this...if anyone from the Free Software Foundation reads this posting, I would like to suggest it could be a very handy validation utility for future releases of XFree86.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Validation of dir/file names

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software