-5

I need to write a script which finds the longest txt file in a folder. I literally have no idea how to do it

closed as unclear what you're asking by Ipor Sircer, Jeff Schaller, Kusalananda, ilkkachu, Rui F Ribeiro Mar 18 at 0:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • What do you mean by "the longest text file"? – Nasir Riley Mar 17 at 19:09
  • Largest in size or longest filename? What designates a "txt" file -- a ".txt" at the end of the filename? – Jeff Schaller Mar 17 at 19:27
  • 2
    Longest in terms of what? Bytes? Characters? Lines? Have you determined how to find out the length of a file? – ilkkachu Mar 17 at 19:29
1

If I understood it correctly:

Assuming all the files has TXT extension, you can do

du -b *.txt | sort -n | tail -n1

and if you want something a little bit more specific:

for a in *;do
    f=$( file -i $a | grep -Eo ": ([a-z\/]+);" )
    [ "$f" == ": text/plain;" ] && du -b $a
done | sort -n | tail -n 1
0

Are there other files in the directory? Can you give us the directory listing?

But barring that I would try something like

ls -Sl $(file * |grep text |cut -d " " -f 1|sed 's/://')

Not the answer you're looking for? Browse other questions tagged or ask your own question.