Bernie
Bernie

Reputation: 1651

Bash: Find all jars in all subdirs and show their folder, name and size

I'm quite new to the Bash and would like to ask this: Is it possible (with a 2-liner ;-) ) to find all jars in all subdirs and show their folder, name and size?

Just like that:

/jars/ | abc.jar | 123456
/ext/  | def.jar | 1234
/ext/  | ghi.jar | 2345

So it should be sth. like (Pseudo code)

find . -name "*.jar" | while read jar; do echo "$jardir | $jarname | $jarsize";

Thanks Bernhard

Upvotes: 0

Views: 4226

Answers (3)

Vytenis Bivainis
Vytenis Bivainis

Reputation: 2376

There's solution using find only:

find -type f -name "*.jar" -printf "| %20h | %10f | %10s |\n"

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 180266

For the record, this question isn't so much about bash itself as about the command-line tools that are commonly available on systems that provide bash. You can, in fact, perform the heavy lifting mostly with bash itself, but if you want a two- (or in fact a one-)liner then you should meet the sed program. For example:

find . -name '*.jar' -exec ls -l {} \; | sed 's,^\([^ ]\+ \+\)\{4\}\([0-9]\+\) \+.* \([^ ]\+/\)\([^ /]\+\)$,\3 | \4 | \2 ,'

Yes, it's incredibly cryptic. Most of the cryptic part is a regular expression that selects the wanted pieces of a long-form directory listing. But you wanted short, and concision comes with a price.

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201447

If I understand your question, you could do it with find and -exec; something like,

find . -name "*.jar" -type f -exec du -m {} \;

Which will find all files ending in .jar and call du -m on each found jar file.

Edit based on OP comment below. To get everything separately, you could use something like -

for i in `find . -name "*.jar" -type f`; do 
  echo $(du -m $i|awk '{print $1}') $(dirname $i) $(basename $i)
done

Upvotes: 3

Related Questions