ksh - Checking directory trees containing wild cards


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh - Checking directory trees containing wild cards
# 1  
Old 09-28-2017
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 ...
Code:
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
# 2  
Old 09-28-2017
Code:
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....
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 09-28-2017
This is not related to ksh at all - you'll need external commands for this. Try - if your find version allows for it -

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

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

This User Gave Thanks to RudiC For This Post:
# 4  
Old 09-28-2017
Hi.

Not knowing what your environment is, many systems utilize locate:
Code:
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):
Code:
$ 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
This User Gave Thanks to drl For This Post:
# 5  
Old 09-29-2017
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 ----------

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

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

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

This worked perfectly. Many thanks Rudi.

---------- Post updated at 09:34 AM ---------- Previous update was at 09:32 AM ----------

Quote:
Originally Posted by drl
Hi.

Not knowing what your environment is, many systems utilize locate:
Code:
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):
Code:
$ 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
Thanks also for this. I wasn't familiar with this command before. Checking it out now.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Find wild card directory and its files of some extensions

I want to use Find command to find directories that have certain name and them find files in that directory having only some extensions. So far, I have come up with this command to list directories with wild card name and list ALL the files in that directory. find . -type d -name prog\* -print... (11 Replies)
Discussion started by: sssccc
11 Replies

2. Shell Programming and Scripting

PERL - getting last file using wild cards

Hi experts, I am new to perl. I am trying to get the last file from set of files. Using the below code; but getting error pls help Files: -rw-r--r-- 1 abc abc 12584 Mar 18 16:22 /abc/def/ghi/xyz.HOSTNAME.2016.03.18.16.21.69709-6 -rw-r--r-- 1 abc abc 12623 Mar 18 16:25... (4 Replies)
Discussion started by: sdosanjh
4 Replies

3. Shell Programming and Scripting

How to copy very large directory trees

I have constant trouble with XCOPY/s for multi-gigabyte transfers. I need a utility like XCOPY/S that remembers where it left off if I reboot. Is there such a utility? How about a free utility (free as in free beer)? How about an md5sum sanity check too? I posted the above query in another... (3 Replies)
Discussion started by: siegfried
3 Replies

4. UNIX for Dummies Questions & Answers

How to use wild cards to find files beginning with upper and lower case

Im trying to use wild cards to find files that start with either an upper or lower case letter e.g. list files that beginning with b or B, i also want to sort them by the time they were last modified. e.g latest file created first. At the moment i have the following code that ls -d... (3 Replies)
Discussion started by: parker4001
3 Replies

5. Shell Programming and Scripting

Checking directory permissions on UNIX directory

Hi, How do i check if I have read/write/execute rights on a UNIX directory? What I'm doing is checking read access on the files but i also want to check if user has rights on the direcory in whcih these files are present. if then...... And I check if the directory exists by using... (6 Replies)
Discussion started by: chetancrsp18
6 Replies

6. UNIX for Advanced & Expert Users

How to rsync or tar directory trees, with hidden directory, but without files?

I want to backup all the directory tress, including hidden directories, without copying any files. find . -type d gives the perfect list. When I tried tar, it won't work for me because it tars all the files. find . -type d | xargs tar -cvf a.tar So i tried rsync. On my own test box, the... (4 Replies)
Discussion started by: fld2007
4 Replies

7. Linux

problem of wild cards matching..$ls t[A-Z] output

hi all, i have created a file by name tt and while listing the file ,i used the command $ls t from which i was expecting not to list tt file output coz of uppercase range, But it listed that file tt file and am not able understand the reason. kindly help. NOTE OS :Red Hat Enterprise... (0 Replies)
Discussion started by: rajp_8007
0 Replies

8. Shell Programming and Scripting

Best way to diff two huge directory trees

Hi I have a job that will be running nightly incremental backsup of a large directory tree. I did the initial backup, now I want to write a script to verify that all the files were transferred correctly. I did something like this which works in principle on small trees: diff -r -q... (6 Replies)
Discussion started by: same1290
6 Replies

9. Shell Programming and Scripting

Need to put a space between wild cards..

how do you put a space in a wild card thing.. example "LastAnalysisT.*${MONTH}.*${DAY}.*20${YEAR}" I need to make it something like "LastAnalysisT.*${MONTH}<I need a space here>.*${DAY}.*20${YEAR}" So it would be something like: LastAnalyst*May 24*2004 Where * just means I dont... (1 Reply)
Discussion started by: LordJezo
1 Replies
Login or Register to Ask a Question