I came up with a command to find files and print their sizes using find, xargs, and du. I am having a problem when I search for something that does not exist. Using the xargs method, du reports all the folders when something doesn't exist, but I expect it to report nothing because nothing should be found. When using the -exec method it works correctly, but from what I have read and observed in bigger searches, it is less efficient because it repeats the du command for each file found instead of operating on the group of files found. See the section where it mentions -delete: http://content.hccfl.edu/pollock/unix/findcmd.htm
Here is an example. First, this is what is in the directories:
ls
bar_dir/ test1.foo test2.foo test3.foo
ls bar_dir
test1.bar test2.bar test3.bar
Here are two searches where I expect to find results:
find . -name '*.foo' -type f -print0 | xargs -0 du -h
4.0K ./test2.foo
4.0K ./test1.foo
4.0K ./test3.foo
find . -name '*.bar' -type f -print0 | xargs -0 du -h
4.0K ./bar_dir/test1.bar
4.0K ./bar_dir/test2.bar
4.0K ./bar_dir/test3.bar
Here is a search where I expect no results, but instead I get a listing of directories:
find . -name '*.qux' -type f -print0 | xargs -0 du -h
16K ./bar_dir
32K .
If I just use find, it returns nothing (as expected)
find . -name '*.qux' -print0
And if I use the -exec method for du, it also returns nothing (as expected)
find . -name '*.qux' -type f -exec du -h '{}' \;
So what is the matter with the xargs du method when find doesn't find anything? Thanks for your time.