LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I produce sorted list of files in current directory over a certain size? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-produce-sorted-list-of-files-in-current-directory-over-a-certain-size-494704/)

bangback 10-22-2006 06:42 PM

How do I produce sorted list of files in current directory over a certain size?
 
Hi all,

I have a need to produce a list of files in the current directory which exceed a given size (say, 500k). In addition, the list needs to be sorted by the size of the file from smallest to largest size and not list any file below the given size (again, say 500k). An example of the output I am looking for goes like this:

LargeFile001 (501k)
BigFile002 (503k)
FileWav (800k)
FileMP3 (22M)

(SmallFile, which is 499k, would not show up on the report).

I've tried using various combinations of the ls, find, du, and sort commands using the pipe, but I do not get the expected result. Instead, I get things like all files of all sizes (including those below the cutoff) listed in sorted order, or all files greater than the specified size but not in sorted order. I even managed to put together one combination of commands that caused the system to list the same thing over and over non-stop.

Ideally, I can get this task done using a command line entry, as opposed to having to write out a script.

Can someone here help me to solve this problem?

Thanks,
b.

dxqcanada 10-22-2006 08:26 PM

Re: sort
 
You have the right idea ... just put everything together with pipes ... and a little extra:

full ls of files larger than 500k
sort these on the 5th field
cut out everything up to the first "/"

Code:

ls -l `find . -maxdepth 1 -size +500k` | sort -k 5,5 | cut -f 2 -d / > sort.txt

fordeck 10-22-2006 08:33 PM

* Find all user files larger than 5Mb:
Code:

find /home -size +5000000c -print
Snipet from "man find"
Quote:

-size n[cwbkMG]
File uses n units of space. The following suffixes can be
used:

‘b’ for 512-byte blocks (this is the default if no suffix is
used)

‘c’ for bytes

‘w’ for two-byte words

‘k’ for Kilobytes (units of 1024 bytes)

‘M’ for Megabytes (units of 1048576 bytes)

‘G’ for Gigabytes (units of 1073741824 bytes)

The size does not count indirect blocks, but it does count
blocks in sparse files that are not actually allocated. Bear
in mind that the ‘%k’ and ‘%b’ format specifiers of -printf
handle sparse files differently. The ‘b’ suffix always denotes
512-byte blocks and never 1 Kilobyte blocks, which is different
to the behaviour of -ls.

bangback 10-25-2006 12:11 AM

The ls command worked well for this mission, although eventually I had to remove the cut command because the boss wanted to see the details of each file listed. I didn't realize that one can pass the results of a command to another command via the use of backticks.

Thank you for your help! :)

Cheers,
b.


All times are GMT -5. The time now is 11:04 AM.