Operating System - Linux
1832622 Members
2561 Online
110043 Solutions
New Discussion

Re: test wheather at least one file exists

 
SOLVED
Go to solution
Andrej Vavro
Frequent Advisor

test wheather at least one file exists

Hi,

When there is more then one file this failed on linux:

if [ -f /folder/fileprefix*.filesuffix ]

there is no problem with the syntax in HP-UX (ksh). Can you help me with the syntax?

Thanks,
Andrej
5 REPLIES 5
Kjartan Maraas
Valued Contributor

Re: test wheather at least one file exists

My shell programming knowledge is very limited, but maybe this link will give you some hints:

http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_20494418.html
Francisco J. Soler
Honored Contributor
Solution

Re: test wheather at least one file exists

Hi, Andrej you can try this, is a little mor complex but works,

if (ls /folder/fileprefix*.filesuffix > /dev/null 2>&1) ; then echo HI ; fi

Frank.
Linux?. Yes, of course.
Steven E. Protter
Exalted Contributor

Re: test wheather at least one file exists

if [ -f /fs/filename ]
then
echo "do something"
else
echo "didn't find the file"
fi

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Stuart Browne
Honored Contributor

Re: test wheather at least one file exists

Unfortunately, what you've described here is not the shell.

You'll find on most Unix systems that the [ is a symboilc link to the program 'test' (but not all).

'ksh' does do some fun things with scripts. Unforunately 'ksh' (pdksh) on Linux does not behave in the exact same manner has 'ksh' on HP-UX (or SCO OSR which is what I was toying with).

To achive what you are wanting to do, you'll have to resort to doing something like:

if [ "$(echo *.blah)" != "*.blah" ]

A strange work around, and admittadly not the best check (will match directories as well as symoblic links).

You could always write your own shell function which looped through + counted arguments..
One long-haired git at your service...
John Meissner
Esteemed Contributor

Re: test wheather at least one file exists

to test for a file do:
if [ -f /home/name/file ]

to test for a directory do:
if [ -d /home/directory ]

to test for file permissions do:
if [ -r file ]
if [ -w file ]
if [ -x file ]

to test if user owns a file do:
if [ -O file ] #capital letter o

to test if a file exists and is non-empty do:
if [ -s file ]

to test if sticky bit is set do:
if [ -k file ]

to test if it is a symbolic link do:
if [ -L file ]

to test if user is in the same group do:
if [ -G file ]

There are several other "if" tests but I think this should cover almost any of your needs.


All paths lead to destiny