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 -exec ls -la {} \;

This command finds directories that start with "prog", prints the name of the directory and lists all files in that directory.

I want to limit the listing of files to certain extensions, say .txt and .prg.

How can I accomplish that without making it too complicated. Any help will be greatly appreciated.

Thanks in advance for the help.
Subhash.

If your find version allows for it, try

find . -path ./prog\* \( -iname "*.txt" -o -iname "*.prg" \)

Tried that. My Find does not support "path".

>find . -path ./prog\* \( -iname "*.txt" -o -iname "*.prg" \)
find: bad option -path
find: [-H | -L] path-list predicate-list

Note that if you would tell us what operating system and shell you're using when you start your threads, we could all save time by not posting suggestions that cannot work in your environment...

Assuming that you are only looking for .txt and .prg files in directories with names that match the pattern file* (not all files with those extensions is the file hierarchies rooted in those directories) and that at least one directory in the file hierarchy rooted in your current working directory matches that pattern, you could try:

for dir in $(find . -type d -name 'file*')
do	ls -l "$dir"/*.txt "$dir"/*.prg "$dir"/.*.txt "$dir"/.*.prg 2>/dev/null
done

Noted Don. I will provide OS/Shell for any future posts. For now, I am using SunOS unix 5.10 Generic_150400-30 and shell is K Shell.

Your suggested code appears to be for use in a script file. Although I would prefer a one line command but I tried it anyway from the command line with "\;" after each line. It goes back to ">" prompt expecting some input.

I played around a little more with my original command and the following seems to have worked for me.

find . -type d -name prog\* -print -exec ls -la {} \; | /usr/xpg4/bin/grep -Ei '/|\.txt|\.PRG'

Please have a look and let me know if you see any potential problem with this.

Thanks.
Subhash.

The code I suggested in post #4 in this thread can be typed directly into ksh at any primary prompt or put into a file and use ksh to execute that file. No \; is required under any circumstances for the for loop I suggested. You could join the 3 lines into a single line if you put just a ; at the end of the 1st 2 lines; but I MUCH prefer to see the structure any of code I'm running instead of trying to cram it all into a single unreadable line. I did have a typo in the first line:

for dir in $(find . -type d -name 'file*')

should have been:

for dir in $(find . -type d -name 'prog*')

but that would cause a different (possibly empty) set of directories to be searched; it shouldn't cause a secondary prompt to be issued.

Unless you added or removed some quotes, there is no reason why the code I suggested would issue a secondary prompt waiting for further input. Please show us EXACTLY (in CODE tags) what you typed into ksh .

For the code you asked about:

find . -type d -name prog\* -print -exec ls -la {} \; | /usr/xpg4/bin/grep -Ei '/|\.txt|\.PRG'

what are you hoping to match with the first of the three alternatives in the ERE you're passing to grep ?

Thanks Don for the reply.

If I put just a ; after the first 2 lines, then it works like a charm. Here is my final version of the command:

for dir in $(find . -type d -name 'pro*'); do ls -l "$dir"/*.txt "$dir"/*.prg "$dir"/.*.txt "$dir"/.*.prg; done

May I ask what is the difference between "$dir"/*.txt and "$dir"/.*.txt in the ls command above ?

To answer your question

the / in the first parameter will list the directory in which the files were found or where no matching files were found. Without the / , I get a long list of matched filenames without knowing which directory they reside in. To make this work, I had to use -print option in the find command.

Thanks
Subhash

See what happens when you execute the following commands in order:

touch open.txt .hidden.txt
ls -l		# will not list .hidden.txt
ls -l  *.txt	# will not list .hidden.txt
ls -l  .*.txt	# will not list open.txt
ls -la *.txt	# will not list .hidden.txt
ls -la .*.txt	# will not list open.txt
ls -la		# will list both open.txt and .hidden.txt

OK. I see the difference now.
So, if I don't expect and/or want to list any files starting with a . , I can safely omit the .*.txt and .*.prg parts. Right ?

Good. :b:

Yes.

Note that your original problem statement only talked about the trailing filename extensions and never gave any specification of the leading part of the filenames. Therefore, I needed to include those to provide an equivalent list of files since you were using ls -la in your find -exec clause. If you don't want to process hidden files, why did you use ls -la ?

I use ls -la as a matter of habit. LOL. However, it had no effect on my results.

No effect other than to make your script run just a little bit slower; consume a few more system resources; and, if there are other users on your system, make the system a little bit slower for those other users.