How to find and display the file name with the fewest words in a file?

  • 3
    Apparently run through the list of files in a loop. using wc to get the count of words and find the minimum ... - Mike

2 answers 2

Go from the console to the directory with the files and run this command:

 ls --file-type | grep -v '/$' | xargs wc -w | sort -g | head -n 1 | awk '{print $2}' 

    You open a file, count the number of spaces +1 and memorize this pair somewhere (file name is the number of words), take another file and do the same. If the number of words is less than what you remembered in the previous step, then you will replace the file name and the new minimum, and so on ..