11

I'm trying to use a single line command that will find every directory (or sub-directory) named bin and will then print a list of all the files under it, but will not also list the directory names under them.

I've tried a couple different things to accomplish this, but so far none have worked:

  1. find ~ -type d -name "bin" -exec ls '{}' ';' | grep -v /

I tested this and it will list the files, but it also list directories under whatever bin is there. So if I have a bin sub-directory under a bin directory that looks like this:

~/home/
   ~/home/bin
      file1.txt
      ~/home/bin/bin
         file2.txt

The output looks something like this:

bin
file1.txt
file2.txt
  1. find ~ -type d -name "bin" -exec ls -f '{}' ';'

I read that doing ls -f will list only files, but this unfortunately also lists the directories bin, .. and .

So how can I do this?

3 Answers 3

16

With -path, you could try:

find ~ -path '*/bin/*' -type f

This won't list bin itself, so to get both:

find ~ \( -path '*/bin/*' -type f \) -o \( -name bin -type d \)
4

You can do this with nested find calls:

$ find ~ -type d -name bin -exec find '{}' -type f ';'

Since I'm replacing an ls call, perhaps you did not want more than one level of listing in the second find call. In that case, add -maxdepth 1 after -type f above.

0
find ~ -type f -size +10M -exec du -sh {} + | sort -rh
  • find ~: This specifies the directory to search for files in (~ in this case).
  • -type f: This ensures that only regular files are considered, not directories.
  • -size +10M: This specifies the minimum size of files to search for, in this case, larger than 10 MB.
  • -exec du -sh {} +: This executes the du -sh command to get the size of each file found. The + at the end of the command ensures that multiple files are passed to du at once, reducing the number of du invocations and improving performance.
  • sort -rh: This sorts the output by size in a human-readable format (-h) and in reverse order (-r), so the largest files appear at the top.
1
  • That seems to be answering a different unrelated question Commented Apr 18 at 8:58

You must log in to answer this question.

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