3

How can you print the 6 smallest files in /usr/bin directory for Shell?

ls /usr/bin

I know that this shows all the files that is in that directory but I just don't know to to print out the 6 smallest files.

What would be a one line command for this process on the command line for Shell?

user10752715
  • 45
  • 1
  • 4
  • Possible duplicate of [Find name of smallest file in Linux](https://stackoverflow.com/q/15497965/608639), [How to find the largest file in a directory and its subdirectories?](https://stackoverflow.com/q/12522269/608639), etc. – jww Sep 15 '19 at 21:46

2 Answers2

6

Try this:

ls -SrqL /usr/bin | head -6

-S makes it sorted by file size

-r for reverse order

-q to print ? instead of nongraphic characters (line breaks included)

-L when showing file information for a symbolic link, this shows information for the file the link references rather than for the link itself

head Shows the 6 first lines of the previous output

Community
  • 1
  • 1
Carlos
  • 331
  • 6
  • 14
  • 2
    [Why *not* parse `ls`?](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – Cyrus Sep 15 '19 at 19:37
  • 1
    @Cyrus you are faster than your own shadow. @user10752715 only mentioned printing. So `ls -Sr` and even `head -6` is quite harmless. – Léa Gris Sep 15 '19 at 19:44
  • @Cyrus. What's the relevance? – Mad Physicist Sep 15 '19 at 19:44
  • 2
    @MadPhysicist: It is not guaranteed that the output contains six file names, because line breaks in file names are allowed. – Cyrus Sep 15 '19 at 19:54
  • You're right @Cyrus, I added -q parameter – Carlos Sep 15 '19 at 20:12
  • With GNU: `find . -maxdepth 1 -type f -printf "%s %f\0" | sort -z -rn | head -z -n 6 | cut -z -d ' ' -f 2- | tr '\0' '\n'` – Cyrus Sep 15 '19 at 20:30
  • @Cyrus That solution does not include those files that have line breaks in their names. It also does not print the smallest, only the biggest. – Carlos Sep 15 '19 at 20:39
  • @Carlos: I don't agree with that statement. Remove `| head -z -n 6` and you should see all files in current directory. – Cyrus Sep 15 '19 at 20:43
  • 2
    @Carlos Your command includes symbolic links as well. I ran the command and it listed all the first 6 links which are smallest in size however the actual file link is pointing to is bigger. I think we should exclude links. – Pacifist Sep 15 '19 at 20:46
  • @Cyrus yep, my bad. I made several files in order to test it and those with line breaks were not shown because of the order (I made them the smallest). Make a new answer with that solution fixing the order because it's a good answer. – Carlos Sep 15 '19 at 20:49
  • 1
    @Carlos: No problem. Bug: In my example, you have to remove the `-r` from `sort` to reverse the wrong sorting. – Cyrus Sep 15 '19 at 20:50
  • @Pacifist Agree with that. I don't know how to exclude them so I add -L to show information about the file it's referencing to, not the link itself. – Carlos Sep 15 '19 at 20:55
  • @Carlos we might need something like this : ls -Srql /usr/bin|egrep -v "^l"|head -6|awk '{print $5,$9}'|sed '/^[[:space:]]*$/d' – Pacifist Sep 15 '19 at 21:01
2

List the six smallest files in current directory:

find . -maxdepth 1 -type f -printf "%s %f\0" | sort -z -n | head -z -n 6 | cut -z -d ' ' -f 2- | tr '\0' '\n'
  • This only lists files
  • It can handle all special characters in file names, even line breaks.
Cyrus
  • 84,225
  • 14
  • 89
  • 153