List files over a specific size in current directory and all subdirectories

79,872

Solution 1

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

Solution 2

Try doing this:

find . -size +10k -ls

And if you want to use the binary ls :

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

Solution 3

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

You are overcomplicating.

find . -size +10k

Solution 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

Solution 5

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}'
Share:
79,872

Related videos on Youtube

eveo
Author by

eveo

I'm the boss.

Updated on July 09, 2022

Comments

  • eveo
    eveo almost 2 years

    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.

    • matchew
      matchew over 11 years
      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)
    • doubleDown
      doubleDown over 11 years
      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.
    • jww
      jww over 7 years
      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 in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Also see Where do I post questions about Dev Ops?
    • jww
      jww over 7 years
  • eveo
    eveo over 11 years
    I want to display them with ls not find. ls -l . -size +10k did not work.
  • eveo
    eveo over 11 years
    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."
  • matchew
    matchew over 11 years
    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.
  • Gilles Quenot
    Gilles Quenot over 11 years
    I don't get you. What I propose is exactly what you asked.
  • matchew
    matchew over 11 years
    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
    matchew over 11 years
    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.
  • frankc
    frankc over 11 years
    The OP has since clearly stated that this is homework and part of the homework is to use ls
  • eveo
    eveo over 11 years
    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
    eveo over 11 years
    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.
  • Gilles Quenot
    Gilles Quenot over 11 years
    Added a ls binary version in my post.
  • Dennis Meng
    Dennis Meng over 9 years
    (fwiw, we call it "reputation", not "karma". This ain't reddit)
  • Ereli
    Ereli about 6 years
    this answer is confusing because find . -size +10k -exec ls -lh {} \+ returns all files not just the ones that are over 10k
  • matchew
    matchew about 6 years
    @Ereli That is not the case. Using find (GNU findutils) 4.6.0 it functions as intended. Only files > 10k are returned.
  • Ereli
    Ereli about 6 years
    @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.
  • matchew
    matchew about 6 years
    @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.
  • Ereli
    Ereli about 6 years
    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
    Ereli about 6 years
    you can reproduce this by running docker run -i -t ubuntu:bionic sh -c "find . -size +10k -exec ls -lh {} \+"
  • matchew
    matchew about 6 years
    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 ?
  • tuxErrante
    tuxErrante almost 5 years
    It is even better when piped with sort like $ find . -size +10M -exec ls -lh {} \; | sort -k 5