1833883 Members
1855 Online
110063 Solutions
New Discussion

Re: file typ of a file

 
Billa-User
Regular Advisor

file typ of a file

how can i detect the file typ of a file ?

the command "file" is not so useful because it shows only strings (in the example in GERMAN), here some examples :

Test for normal file with ASCII strings:

file with size zero:
create of file: touch touch-zero
test of file: file touch-zero
output of file: touch-zero: leer

file with one line:
create of file: echo line > touch-one-line
test of file: file touch-one-line
output of file: touch-one-line: Ascii-Text

character device-file :
test of file: file /dev/null
output of file: /dev/null: zeichenorientiert (3/2)

test of file: file /dev/async
output of file: /dev/async: zeichenorientiert (101/0)

test of file: file /dev/rac/bt6lto3picker
output of file: /dev/rac/bt6lto3picker: zeichenorientiert (231/11534336)

test of file: file /dev/vg00/rlvol1
output of file: /dev/vg00/rlvol1: zeichenorientiert (64/1)

block device-file :
test of file: file /dev/vg00/lvol1
output of file: /dev/vg00/lvol1: blockorientiert (64/1)

so i think it is better to check the file typ with the command "test" :

test -f , test -b , test -c and so on.

but how can i detect if the file exists ( at that time i don't know the file typ)

test -s is not the recommended way ?

test -s /dev/null => isn't true

test -s touch-zero => isn't true

test -s touch-one-line => is true

after the first check, i can begin to check the file typ of the file.

does anyone have some useful tips

regards,Tom
5 REPLIES 5
Heiner E. Lennackers
Respected Contributor

Re: file typ of a file

Hi,

test -r : True if file exists and is readable.

HeL
if this makes any sense to you, you have a BIG problem
Dennis Handly
Acclaimed Contributor

Re: file typ of a file

The posix shell has -e for exists. But you might as well use the tried and true -a for ksh or test(1) exists.
Oviwan
Honored Contributor

Re: file typ of a file

Hallo

Check the manpage of ksh, especially the "Conditional Expressions" section.

there are a lot of test conditions for files.

Regards
Billa-User
Regular Advisor

Re: file typ of a file

thx for your information,here is my solution:

exists()
{
[ -n "${DEBUG}" ] && set -x

[[ -f $1 || -d $1 || -L $1 || -p $1 || -S $1 || -b $1 || -c $1 ]]
return $?
}
Dennis Handly
Acclaimed Contributor

Re: file typ of a file

>here is my solution:

All you needed was:
[[ -a $1 ]]

This is a ksh or posix extension to test(1).