Can somebody please show me how to check from within a KSH script if a directory exists on that same host when parts of the directory tree are unknown?
If these wildcard dirs were the only dirs at that level then ...
ls -ldt `find /u01/app/oracle/local -type d | head -1`
It does not make sense to request the most recent directory and then want a return code for an answer. This returns the ouptut for ls -l for one file.
Do you want the last time modified or the inode time? Change the ls parameters to match your requirements. The reason I ask is that some posters do not fully understand the difference.
inode time is as close as you get for "create" time - for many kinds of file systems. Since I do not know what filesystems you are using this is the best I can do for you....
Not knowing what your environment is, many systems utilize locate :
NAME
locate - find files by name
SYNOPSIS
locate [OPTION]... PATTERN...
DESCRIPTION
locate reads one or more databases prepared by updatedb(8) and writes
file names matching at least one of the PATTERNs to standard output,
one per line.
...
which might be faster than probing the filesystem, since the updatedb database already contains the results of such a probe.
Available on Linux, Solaris, macOS.
See man locate for details.
For example, finding a file xxx-yyy-zzz (edited ~ for actual home):
$ time locate xxx-yyy-zzz
~/try/grep/match-nomatch-to-separate-files/xxx-yyy-zzz
real 0m0.095s
user 0m0.088s
sys 0m0.004s
$ time find / -type f -name xxx-yyy-zzz 2>/dev/null
~/try/grep/match-nomatch-to-separate-files/xxx-yyy-zzz
real 0m7.649s
user 0m0.896s
sys 0m2.672s
Hi Jim. Actually I originally only wanted to check that the dir existed but then had to change it to handle the possibility of multiple dirs with the same name in different directory trees. I don't need a return code in this scenario. You're right of course, like some of the other comments also mention, the best way to do this is using find. Thanks very much for your reponse.
---------- Post updated at 09:32 AM ---------- Previous update was at 09:32 AM ----------
This worked perfectly. Many thanks Rudi.
---------- Post updated at 09:34 AM ---------- Previous update was at 09:32 AM ----------
Thanks also for this. I wasn't familiar with this command before. Checking it out now.