ksh - Checking directory trees containing wild cards

Hi

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 ...

RETCODE=$(ls -l /u01/app/oracle/local/*/* | grep target_dir)

... will work as $RETCODE will have a value if the dir exists.

But if there are multiple sub dirs at the 'wildcard' level, how can I grab the one that was last created?

Many thanks

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....

1 Like

This is not related to ksh at all - you'll need external commands for this. Try - if your find version allows for it -

find /u01/app/oracle/local -type d -iname target_dir -ls | sort -ksomekey

or

find /u01/app/oracle/local -type d -iname target_dir | xargs ls -tdla | head -1
1 Like

Hi.

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

Best wishes ... cheers, drl

1 Like

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.