testing if a file is present

Jonathan I. Kamens jik at athena.mit.edu
Thu Nov 22 06:16:38 AEST 1990


In article <ger.659126250 at prisma>, ger at 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 at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.shell mailing list