find directories that do NOT contain a certain file extension

for x in `find /vmfs/volumes/v01tstn01a01/ -type d`; do find $x -name '*.vmx' > /dev/null || echo $x; done;

The goal of this is to find the subdirectories that do NOT contain a file with the extension of .vmx

Any help would be great!

Try this:


find $x \( ! -name '*.vmx' \)

Thanks for the reply!

I have pasted my attempts with your suggestion below.

[root@Test23]# find /vmfs/volumes/v01tstn01a01 \( ! -name '.vmx' \)
/vmfs/volumes/v01tstn01a01
[root@Test23]# for x in `find /vmfs/volumes/v01tstn01a01`; do find $x \( ! -name '
.vmx' \); done;
/vmfs/volumes/v01tstn01a01
[root@Test23]# find /vmfs/volumes/v01tstn01a01/rtest
rtest1/ rtest2/
[root@Test23]# find /vmfs/volumes/v01tstn01a01/rtest1 \( ! -name '*.vmx' \)
/vmfs/volumes/v01tstn01a01/rtest1
/vmfs/volumes/v01tstn01a01/rtest1/rtest1-flat.vmdk
/vmfs/volumes/v01tstn01a01/rtest1/rtest1.nvram
/vmfs/volumes/v01tstn01a01/rtest1/vmware.log
/vmfs/volumes/v01tstn01a01/rtest1/vmware-39.log
/vmfs/volumes/v01tstn01a01/rtest1/vmware-40.log
/vmfs/volumes/v01tstn01a01/rtest1/vmware-36.log
/vmfs/volumes/v01tstn01a01/rtest1/vmware-41.log
/vmfs/volumes/v01tstn01a01/rtest1/vmware-38.log
/vmfs/volumes/v01tstn01a01/rtest1/rtest1.vmdk
/vmfs/volumes/v01tstn01a01/rtest1/rtest1.vmxf
/vmfs/volumes/v01tstn01a01/rtest1/rtest1.vmsd
/vmfs/volumes/v01tstn01a01/rtest1/vmware-37.log

Maybe I'm not understanding how to apply it. I don't have a lot of Linux experience.
Ideally it should print out only a list of subdirectories NOT containing a .vmx file

I have rigged the rtest1 directory to not contain a vmx

find /vmfs/volumes/v01tstn01a01 -type d | while read dir ; do 
    ls ${dir}/*.vmx > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
         echo "$dir"
    fi
done

Looks like that will work.

Thanks!

I also managed to google this up....

find /vmfs/volumes/v02tstn02a01/ -type d | while read dir; do if [ ! -f $dir/*.vmx ]; then echo $dir; fi; done;

Very similar.

Thanks a bunch for the replies.

That will probably break if you have more than one file in a directory. If you have only one vmx can be in a directory you'll be fine.

If you really, really, really want to protect against oddball filenames and you really, really, really want to avoid as much process creation as possible (perhaps there are many directories and files to go through) the following would be a good approach:

find top -type d -print0 | xargs -0 filecheck.sh

filecheck.sh:

#!/bin/sh

for d; do
    for f in "$d"/*.vmx; do
        [ -f "$f"  ] && continue 2
    done 
    echo "$d"
done

Note that the above checks strictly for a file ending in .vmx. A directory or a pipe or some other type of directory entry ending in .vmx will not be considered a match.

Also, filecheck.sh needs to be in your $PATH or a pathname with a "/" must be used to invoke it.

Regards,
Alister