Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by steeldriver for conditional find output missing

Your test if [ "find /location/sub/int/ -size +1G" ] doesn't work the way you intend because it tests the non-emptiness of the string "find /location/sub/int/ -size +1G" - which will always be true. In any case, the redirection > /location/sub/int/large_file_audit.txt will not magically pick up the standard output of the preceding command, so will always create an empty file.

Perhaps the closest to your intent in Bash would be to put the results of find into an array, and then test whether it has any elements:

mapfile -t files < <(find /location/sub/int/ -size +1G)

if (( ${#files[@] > 0 )); then 
  printf '%s\n' "${files[@]}" > /location/sub/int/large_file_audit.txt
fi

This won't gracefully handle filenames containing newlines - with newer versions of bash, you could make the find and mapfile null-delimited, but there's not much benefit if you're outputting them as a newline-delimited list anyhow.


Viewing all articles
Browse latest Browse all 3

Trending Articles