Skip to content
Advertisement

How to sort files by word count in linux?

I have to sort files in current directory based on word count by using command wc and pipe | necessarily. What command do I have to use?

I thought I had to use a command sort | wc -w, but I failed.

Advertisement

Answer

I think this can help.

JavaScript

The ls -1 will list all files of the current directory, and then pass it to xargs to use the output of the previous command as input of the command wc -w. Finally we pipe the result to sort command to order them by number of words each file contain. You can learn more about xargs here.

The output:

JavaScript

Edit

I just figured out that my answer was not correct. Because sort command by default works character by character, so the result of sorting 2, 10, 3 will be:

JavaScript

Because it only checks the first character of 10 and it’s 1 so it’s less than 2 and 3. To fix it we should use numerical sort, by using n flag. Here’s how it works:

JavaScript

And just to make output more cleaner we can remove the total line and just show the file names.

JavaScript
Advertisement