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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Validation of dir/file names

Reply to Message Icon

Name: ICW Infrastructure
Date: April 29, 2003 at 09:26:58 Pacific
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?



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 29, 2003 at 11:40:54 Pacific
Reply:

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


0

Response Number 2
Name: WilliamRobertson
Date: April 29, 2003 at 15:42:16 Pacific
Reply:

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


0

Response Number 3
Name: ICW Infrastructure
Date: April 30, 2003 at 07:25:07 Pacific
Reply:

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.


0

Response Number 4
Name: WilliamRobertson
Date: April 30, 2003 at 08:41:25 Pacific
Reply:

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.


0

Response Number 5
Name: ICW Infrastructure
Date: May 1, 2003 at 04:29:02 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Validation of dir/file names

Read File name and pass to SQLoader www.computing.net/answers/unix/read-file-name-and-pass-to-sqloader/5543.html

How to pull out part of a file path www.computing.net/answers/unix/how-to-pull-out-part-of-a-file-path/4109.html

script to rename file names www.computing.net/answers/unix/script-to-rename-file-names/7621.html