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

后端 未结 5 1811
迷失自我
迷失自我 2020-12-22 21:04

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.

相关标签:
5条回答
  • 2020-12-22 21:35

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

    You are overcomplicating.

    find . -size +10k
    
    0 讨论(0)
  • 2020-12-22 21:38

    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

    0 讨论(0)
  • 2020-12-22 21:52

    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}'
    
    0 讨论(0)
  • 2020-12-22 21:56

    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

    0 讨论(0)
  • 2020-12-22 21:58

    Try doing this:

    find . -size +10k -ls
    

    And if you want to use the binary ls :

    find . -size +10k -exec ls -l {} \;
    
    0 讨论(0)
提交回复
热议问题