48

How can I display all files greater than 10k bytes in my current directory and it's subdirectories.

Tried ls -size +10k but that didn't work.

Caltor
  • 2,538
  • 1
  • 27
  • 55
eveo
  • 2,797
  • 15
  • 61
  • 95
  • could you please expand on this question, or at least explain why the two solutions that were posted -- and work, are not appropriate to your assignment. (edit: added please) – matchew Nov 08 '12 at 05:37
  • 1
    `ls` doesn't have any options to filter output by size. It does have a `--size` option (with no arguments) which prints the size of each file in blocks. By the way, `-size +10k` seems like a syntax that is used with `find`. – doubleDown Nov 08 '12 at 07:51
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Feb 07 '17 at 22:51
  • [How can I find files that are bigger/smaller than x bytes?](http://superuser.com/q/204564/173513) on Super User. – jww Feb 07 '17 at 22:51

5 Answers5

80

find . -size +10k -exec ls -lh {} \+

the first part of this is identical to @sputnicks answer, and sucesffully finds all files in the directory over 10k (don't confuse k with K), my addition, the second part then executes ls -lh or ls that lists(-l) the files by human readable size(-h). negate the h if you prefer. of course the {} is the file itself, and the \+ is simply an alternative to \;

which in practice \; would repeat or:

ls -l found.file; ls -l found.file.2; ls -l found.file.3

where \+ display it as one statement or:

ls -l found.file found.file.2 found.file.3

more on \; vs + with find

Additionaly, you may want the listing ordered by size. Which is relatively easy to accomplish. I would at the -s option to ls, so ls -ls and then pipe it to sort -n to sort numerically

which would become:

find . -size +10k -exec ls -ls {} \+ | sort -n

or in reverse order add an -r :

find . -size +10k -exec ls -ls {} \+ | sort -nr

finally, your title says find biggest file in directory. You can do that by then piping the code to tail

find . -size +10k -exec ls -ls {} \+ | sort -n | tail -1 would find you the largest file in the directory and its sub directories.

note you could also sort files by size by using -S, and negate the need for sort. but to find the largest file you would need to use head so

find . -size +10k -exec ls -lS {} \+ | head -1

the benefit of doing it with -S and not sort is one, you don't have to type sort -n and two you can also use -h the human readable size option. which is one of my favorite to use, but is not available with older versisions of ls, for example we have an old centOs 4 server at work that doesn't have -h

Community
  • 1
  • 1
matchew
  • 19,195
  • 5
  • 44
  • 48
  • i tried this on a directory w/o subdirectories and it worked flawlessly. let me try this on a different directory. h/o -- `find` is what you are after, btw. – matchew Nov 08 '12 at 05:15
  • ok, I tried it on a different directory. this is what you are after. Can you explain why this is most definitely not this? – matchew Nov 08 '12 at 05:16
  • It's because in this assignment we're supposed to input a command. If the command is correct, I get taken to the next section of the assignment. – eveo Nov 08 '12 at 23:01
  • 3
    Actually this answer is correct. I forgot we covered the find command in this course so we're allowed to use it. Answer was `find . -size +10k`. Thank you so much for the detailed answer, I really really appreciate it. – eveo Nov 08 '12 at 23:48
  • this answer is confusing because `find . -size +10k -exec ls -lh {} \+` returns all files not just the ones that are over 10k – Ereli Jun 02 '18 at 00:04
  • @Ereli That is not the case. Using `find` (GNU findutils) 4.6.0 it functions as intended. Only files > 10k are returned. – matchew Jun 04 '18 at 13:52
  • @matchew well, don't upgrade to `4.7.0-git` because it doesn't work. on 4.7.0 I see directories, 0 bytes files, symbolic links and files that are clearly smaller than 10k. – Ereli Jun 04 '18 at 18:07
  • @Ereli, Sorry its not working for you. As far as I know this is the proper way to do it. Several other answers below reaffirm this as the solution. might it work with `\;` and not `\+`. Could the meaning of `\+` be different in newer versions of `find`? It was a relatively recent addition 5.5 years ago when this question was answered. – matchew Jun 04 '18 at 21:02
  • I tried to dig in the `find` source code to see what was changed between 4.6 and 4.7 but couldn't find a good reason for this change in behavior. – Ereli Jun 10 '18 at 17:52
  • you can reproduce this by running `docker run -i -t ubuntu:bionic sh -c "find . -size +10k -exec ls -lh {} \+"` – Ereli Jun 11 '18 at 23:18
  • What shell is `sh` linked to? I am using `bash`. does it work when appended with `\;` instead of `\+`? Or perhaps just `find . -size +10k -ls` ? – matchew Jun 12 '18 at 13:24
  • It is even better when piped with sort like `$ find . -size +10M -exec ls -lh {} \; | sort -k 5` – tuxErrante Jul 28 '19 at 08:59
16

Try doing this:

find . -size +10k -ls

And if you want to use the binary ls :

find . -size +10k -exec ls -l {} \;
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • I want to display them with ls not find. `ls -l . -size +10k` did not work. – eveo Nov 08 '12 at 05:09
  • Still didn't work, this is for an assignment. "Your current directory is dir. Display all the files within dir (and it's subdirectories) that are larger than 10k bytes." – eveo Nov 08 '12 at 05:11
  • 1
    I don't get you. What I propose is exactly what you asked. – Gilles Quénot Nov 08 '12 at 05:15
  • I agree, sputnick, what could he be after. +1 to you as my answer is a modification of your own. I have never used find with the -ls option before, I often find myself using `find` with size or mtime though. – matchew Nov 08 '12 at 05:26
  • The OP has since clearly stated that this is homework and part of the homework is to use ls – frankc Nov 08 '12 at 15:13
  • 1
    Added a ls binary version in my post. – Gilles Quénot Nov 09 '12 at 01:29
9

I realize the assignment is likely long over. For anyone else:

You are overcomplicating.

find . -size +10k
m00am
  • 5,910
  • 11
  • 53
  • 69
4

I'll add to @matchew answer (not enough karma points to comment):

find . -size +10k -type f -maxdepth 1 -exec ls -lh {} \; > myLogFile.txt

-type f :specify regular file type

-maxdepth 1 :make sure it only find files in the current directory

Microfed
  • 2,832
  • 22
  • 25
holychiz
  • 41
  • 3
3

You may use ls like that:

ls -lR | egrep -v '^d' | awk '$5>10240{print}'

Explanation:

ls -lR         # list recursivly
egrep -v '^d'  # only print lines which do not start with a 'd'. (files)

only print lines where the fifth column (size) is greater that 10240 bytes:

awk '$5>10240{print}'
jens-na
  • 2,254
  • 1
  • 17
  • 22