0

I need to provide a detailed list of files in a directory in descending order of the file size. only to include 10 files in my output. I can not include sub directories in my output. I'm having the problem with the last part, "not including sub directories" here is what I have so far but I'm stuck on excluding subdirectories. Also if you could look at the below command to advise if this is correct

de@Classbox:~/LI_1/etc$ ls -lh -S | sort -r |head -10

I know if i use grep -v d it will exclude directories but I need it to exclude subdirectories.

1
  • Your ls command is not recursing subdirectories; if this is part of a class to learn ls and grep, perhaps they're looking for ls ... | grep -v ^d? You'll likely get different answers here, so it'd be best to clearly explain where you're coming from and what you're expecting. – Jeff Schaller Oct 4 '18 at 13:30
1

Don't expect meaningful results if you sort by "human readable" file sizes. Try

ls -l| sort -k1,1.1 -k5nr | head -n 10

If there are less than 10 regular files, you need additional measures.

2
  • Hi this worked great for me. could you break down the sort command to what it means though so i can understand the command – Deirdre Oct 10 '18 at 14:29
  • 1
    The -k are key definitions. -k1,1.1 sort on the line's first char, - first, d last, i.e. directories a retrailing. Then, within the file types, -k5nr sorts by size, numerically, reversely / descending. – RudiC Oct 10 '18 at 21:39
1

Using the zsh shell:

printf '%s\n' *(.OL[1,10])

The (.OL[1,10]) is a glob modifier that modifies the behaviour of the preceding glob pattern (*). In this case, the . means it will only match regular files (i.e. not directories, sockets, fifos etc.), OL means sorting in descending order (O) based on the file size/length (L). The [1,10] limits the number of returned matches to the first 10.

To include hidden files, set the GLOB_DOTS shell option with setopt GLOB_DOTS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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