Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-22-2006, 07:42 PM   #1
LQ Newbie
Registered: Oct 2006
Posts: 2

Rep: Reputation: 0
Question 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?

Old 10-22-2006, 09:26 PM   #2
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
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 "/"

ls -l `find . -maxdepth 1 -size +500k` | sort -k 5,5 | cut -f 2 -d / > sort.txt
Old 10-22-2006, 09:33 PM   #3
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
* Find all user files larger than 5Mb:
find /home -size +5000000c -print
Snipet from "man find"
-size n[cwbkMG]
File uses n units of space. The following suffixes can be

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

‘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.
Old 10-25-2006, 01:11 AM   #4
LQ Newbie
Registered: Oct 2006
Posts: 2

Original Poster
Rep: Reputation: 0
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!


Last edited by bangback; 10-25-2006 at 01:14 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ List Files In Directory bendeco13 Programming 8 11-02-2010 01:08 PM
Getting a DOS-style sorted directory listing KimVette Linux - General 2 01-29-2006 12:22 PM
How to list total file size of a directory phil1076 Linux - General 3 12-18-2003 04:47 PM
list only files in current directory xscousr Programming 8 09-22-2003 08:35 AM
list size of directory and all directories it contains? ubien Linux - General 2 01-11-2003 11:59 AM > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration