0

I have a large directory, with too many files for just ls. My idea: use something along the line of:

find . -name * -exec wc -c < {} \; | sort | tail -n 1

Problem: shell is interpreting it as (find . -name * -exec wc -c) < ({} \;) | ...

I need the "<" on the < {}, to avoid displaying the filename into sort.

I've also tried

find . -name "*" -exec cat {} +| wc -c

However, this seems to be interpreted as: (find . -name "*" -exec cat {}) | (wc -c) -- so it gives me the size of all files combined.

There is also a variant using du-- however, since the biggest files could be just a few bytes apart, this just displays along the lines of a million files of 500-KB size-- again, too many for ls.

8
  • are there subdirectories within your large directory ? Jul 18, 2017 at 14:05
  • What's the idea behind -name *?
    – Philippos
    Jul 18, 2017 at 14:05
  • 1
    But what's wrong about du -b * .??* | sort -rn | head -n7?
    – Philippos
    Jul 18, 2017 at 14:11
  • 1
    @Philippos if there really are a lot of files (thousands or maybe hundreds of thousands) the * will fail to expand. Jul 18, 2017 at 14:12
  • 1
    But ls -lS | sed 10q will work fine and should be faster than anything else, In particular, finding sizes with wc -c is just nuts.
    – NickD
    Jul 18, 2017 at 14:20

2 Answers 2

2

To print the largest file (with size in kilobytes), ls approach:

ls -S1s --block-size=K | sed -n '2{p;q;}'

  • -S - sort by file size, largest first

  • -s - print the allocated size of each file, in blocks

  • -1 - list one file per line

  • sed -n '2{p;q;}' - the 1st line in ls output is the line with total size (e.g. total 388K). The 2nd line is the line containing the largest file with its size. This expression will consider only the 2nd needed line

5
  • It would help to know what the 2{p;q;} does, but this worked.
    – Alex
    Jul 18, 2017 at 14:22
  • This breaks with filenames containing newlines. (Unusual, but possible.) Jul 18, 2017 at 14:23
  • 1
    @Alex the 2{p;q} prints (p) the second line of output and then quits (q) so as not to bother reading the rest of the data stream. Jul 18, 2017 at 14:24
  • @Alex, added some explanation Jul 18, 2017 at 14:25
  • @RomanPerekhrest Apparently, the '-s' option lists the files in 512-byte clusters. So to find the size of the biggest one in bytes, the shortest solution I have found using your method is: stat -f '%z' < "ls -S1 | head -n 1" where the double-quotes would be ` characters.
    – Alex
    Jul 18, 2017 at 15:15
2

Assuming that none of your file names contains a newline (\n), and that you have GNU utilities, you can use this as a quick and dirty solution:

find . -maxdepth 1 -type f -exec stat --fmt $'%s\t%n' {} + | sort -n | tail -1

Remove -maxdepth 1 if you have subdirectories that must be included in the search. If your find doesn't support the terminator + you can replace it with the less efficient \;. Change the format string to just '%s' if you don't want the filename output.


If you have file names with non-printing characters in them you can use this slower variant:

find . -maxdepth 1 -type f -exec stat --printf "%s\t" {} \; -print | sort -n | tail -1

Remove -print if you really don't want the filename output.


For OSX I think you can use a command like this, but I've no easy way of testing it. Remove -print if you don't want the filename output:

find . -maxdepth 1 -type f -exec stat -f '%z' -n {} \; -print | sort -n | tail -n 1
5
  • stat: illegal option -- c Maybe I need to download a newer version somewhere?
    – Alex
    Jul 18, 2017 at 14:08
  • @Alex try --fmt instead of -c. If that doesn't work please identify your OS and version of stat (on my Linux-based system, stat --version returns stat (GNU coreutils) 8.26). Jul 18, 2017 at 14:11
  • stat: illegal option -- -
    – Alex
    Jul 18, 2017 at 14:11
  • Right. When I typed stat --version, that gave me the error I posted above. I have whatever the newest version of Mac is right now.
    – Alex
    Jul 18, 2017 at 14:13
  • 1
    Here is what I got to finally work: find . -maxdepth 1 -type f -exec stat -f %z {} \; | sort -n | tail -n 1 In particular, the -f should have a % in front of the z, and -print is not required when I'm not looking for the name of that file, just its size.
    – Alex
    Jul 18, 2017 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .