Search code examples
bashfindls

list files in directories that contain a given name


How to list all java files whose absolute paths contain the word opt? this works:

$ find . -name *.java | grep "opt"

But I need the sizes of those files as well (similar to ls)? I've tried the following but failed:

$ ls -l *opt*/*.java
$ find *opt* -name *.java

Solution

  • find -type f -path "*/opt/*.java" -printf "%f %s\n"
    

    Search for a specific directory/file path using -path and then use printf to print the file name (%f) and the file size (%s)