[comp.unix.shell] testing if a file is present

ger@prisma.cv.ruu.nl (Ger Timmens) (11/21/90)

Can anybody tell me how I can verify
whether a file exists or not ?
I want to move files and only when
they exist ! mv "file" gives an
error message if "file" not exists

Thanks,

Ger Timmens,
ger@cv.ruu.nl

ian@hpopd.HP.COM (Ian Watson) (11/21/90)

For the Bourne shell, try :

if [ -f file ] ; then
	mv file otherfile
fi

see test(1) for more details and other useful options.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ian Watson, HP Pinewood Information Systems Division, England.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Phone :				(Intl)+44 344 763015
Unix mail (Internet) :		ian@hpopd.HP.COM
Unix mail (UUCP) :		...!hplabs!hpopd!ian
Openmail :			ian watson/pinewood,lab,hpopd
Openmail from Unix :		ian_watson/pinewood_lab_hpopd@hpopd
HPDesk :			Ian WATSON/HP1600
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

dberg@informix.com (David I. Berg) (11/21/90)

In article <ger.659126250@prisma> ger@prisma.cv.ruu.nl (Ger Timmens) writes:
>Can anybody tell me how I can verify whether a file exists or not ?
>I want to move files and only when they exist ! mv "file" gives an
>error message if "file" not exists 

if (-f filename) will return true if the file exists, false if not.
There are many other options for which you can also test:
-d if filename is a directory,
-z if filename is zero length,
-w if filename is writable,
etc.

RTFM!

jik@athena.mit.edu (Jonathan I. Kamens) (11/22/90)

In article <ger.659126250@prisma>, ger@prisma.cv.ruu.nl (Ger Timmens) writes:
|> Can anybody tell me how I can verify
|> whether a file exists or not ?
|> I want to move files and only when
|> they exist ! mv "file" gives an
|> error message if "file" not exists

  It depends on the shell you're using.  In csh or tcsh:

	if (-e filename) then
		... stuff if filename exists ...
	endif

In sh, with either a test built-in named "[" or a link in your filesystem
called "[" pointing to the test program:

	if [ -r filename ]; then
		... stuff if filename exists ...
	fi

Some versions of test allow "-e" to test for existence, instead of "-r" to
test for existence and readibility, but mine doesn't, so I use "-r" instead of
"-e" above.  In sh without "[":

	if test -r filename; then
		... stuff if filename exists
	fi

Ksh and bash probably do things the same way sh does, using "[".

  In the future, it would help if you would give more details, such as the
shell you're using, when you ask questions like this.
		
-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

bill@twg.bc.ca (Bill Irwin) (11/22/90)

ger@prisma.cv.ruu.nl (Ger Timmens) writes:

>Can anybody tell me how I can verify
>whether a file exists or not ?
>I want to move files and only when
>they exist ! mv "file" gives an
>error message if "file" not exists

Try this:

if [ -f file_name ]
then
        mv file_name /new/destination/file_name
fi

The "-f" tests for the existence of the file.  There are other options:

-x  is the file executable
-r  is the file readable
-s  does the file have anything in it (greater than zero bytes)

There  are  others too.  Check your manual under "sh".  The options should  be
listed.
-- 
Bill Irwin    -       The Westrheim Group     -    Vancouver, BC, Canada
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uunet!van-bc!twg!bill     (604) 431-9600 (voice) |     UNIX Systems
bill@twg.bc.ca            (604) 430-4329 (fax)   |     Integration

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (11/23/90)

In article <1990Nov21.191638.19469@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:

| In sh, with either a test built-in named "[" or a link in your filesystem
| called "[" pointing to the test program:
| 
| 	if [ -r filename ]; then
| 		... stuff if filename exists ...
| 	fi
| 
| Some versions of test allow "-e" to test for existence, instead of "-r" to
| test for existence and readibility, but mine doesn't, so I use "-r" instead of
| "-e" above.  In sh without "[":
| 
| 	if test -r filename; then
| 		... stuff if filename exists
| 	fi

  Close but not correct.
	-f is there a file
	-r is it readable
	-x is it executable
-- 
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
    sysop *IX BBS and Public Access UNIX
    moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (11/23/90)

In article <1990Nov21.155149.3505@informix.com> dberg@informix.com (David I. Berg) writes:

| -z if filename is zero length,

  Actually -z (zero length) and -n (non-zero length) apply to any string
following the operator, although it certainly applies to filename in strings.
-- 
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
    sysop *IX BBS and Public Access UNIX
    moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me

stig@rabbit.ece.cmu.edu (Jonathan Stigelman) (11/26/90)

There's a one line solution that works with both csh & sh...
and is particularly handy in makefiles.

	[ -f foo ] && mv foo bar || echo foo does not exist

This is a handy way to deal with the yacc tokens file y.tab.h
so that you don't fire lots of extra recompilataions when everything
depends on that tokens file.

	cmp y.tab.h tokens.h && rm y.tab.h || mv y.tab.h tokens.h

Cryptic enough?
Stig

jon@jonlab.UUCP (Jon H. LaBadie) (11/27/90)

> In article <ger.659126250@prisma> ger@prisma.cv.ruu.nl (Ger Timmens) writes:
> Can anybody tell me how I can verify whether a file exists or not ?
> I want to move files and only when they exist ! mv "file" gives an
> error message if "file" not exists 

A number of poster have responded to Ger's posting by suggesting the
-f option of test (builtin or separate executable).  Most claimed this
option tests if the file exists and suggest that the manual be read.

They should read the manual which says:

	-f file   true if file exists AND is a regular file

(emphasis mine)

I.e., the -f is a filetype/existance test.  If a directory foobar
exists, test -f foobar is false; test -d foobar is true.

There are a number of filetype/existance options to test:

	-f	regular files
	-d	directory files
	-b	block special files
	-c	character special files
	-p	named pipe files (fifo's)

and on some systems:

	-????	symbolic link files

Jon

-- 
Jon LaBadie
{att, princeton, bcr, attmail!auxnj}!jonlab!jon

gwc@root.co.uk (Geoff Clare) (11/27/90)

In <1990Nov21.155149.3505@informix.com> dberg@informix.com (David I. Berg) writes:

>if (-f filename) will return true if the file exists, false if not.

Wrong.  It will return true if the file exists AND IS A PLAIN FILE.
To test for existence only use -e.

This is one place where csh has an advantage over Bourne shell.  There is
no equivalent way with the Bourne shell "test" command to test for existence
without also testing for a specific file type or access mode.  OK, so you
can do it with

	test -f file -o -d file -o -c file -o -b file -o -p file

but I don't count that as "equivalent"!

-- 
Geoff Clare <gwc@root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, Hayne Street, London EC1A 9HH, England.   Tel: +44-71-315-6600

lugnut@sequent.UUCP (Don Bolton) (11/28/90)

In article <2356@sixhub.UUCP> davidsen@sixhub.UUCP (bill davidsen) writes:
>In article <1990Nov21.191638.19469@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
>
>| In sh, with either a test built-in named "[" or a link in your filesystem
>| called "[" pointing to the test program:
>| 
>| 	if [ -r filename ]; then
>| 		... stuff if filename exists ...
>| 	fi
>| 
>| Some versions of test allow "-e" to test for existence, instead of "-r" to
>| test for existence and readibility, but mine doesn't, so I use "-r" instead of
>| "-e" above.  In sh without "[":
>| 
>| 	if test -r filename; then
>| 		... stuff if filename exists
>| 	fi
>
>  Close but not correct.
>	-f is there a file
>	-r is it readable
>	-x is it executable

	-s file has nonzero length

>-- 
>bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
>    sysop *IX BBS and Public Access UNIX
>    moderator of comp.binaries.ibm.pc and 80386 mailing list

if [ -s $USER.IQ ]
   then 
       echo "Proceed..."
       read chore
       etc etc
   else
       echo "Stupidity, like virtue, is its own reward"
       exit 0
fi


There, now your .sig is more fitting :-)

les@chinet.chi.il.us (Leslie Mikesell) (11/28/90)

In article <ger.659126250@prisma> ger@prisma.cv.ruu.nl (Ger Timmens) writes:
>Can anybody tell me how I can verify
>whether a file exists or not ?
>I want to move files and only when
>they exist ! mv "file" gives an
>error message if "file" not exists

Ah, the error message is the real problem here - just throw it away!
   mv old new 2>/dev/null

Note of course that you also lose "real" error messages with this
approach.  However, also keep in mind that no method of testing
before the move is atomic with the mv itself and thus will fail
with multiple concurrent processes.

Les Mikesell
  les@chinet.chi.il.us

jon@jonlab.UUCP (Jon H. LaBadie) (12/01/90)

In article <2549@root44.co.uk>, gwc@root.co.uk (Geoff Clare) writes:
> 
> 	test -f file -o -d file -o -c file -o -b file -o -p file
> 
> but I don't count that as "equivalent"!
> 

OK, granted, but how about the nearly equivalent

	test ! -f file

It will be true when you want a regular file of the indicated name
and false if said file does not exist or is a type other than regular.

Another possibility not mentioned in this thread (and with its own
limitations) is test -s file.  For this to be true, the file must
exist and have some content (i.e. not an empty file).

Jon

-- 
Jon LaBadie
{att, princeton, bcr, attmail!auxnj}!jonlab!jon