Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In a partition I have several files and folders, and I can list all those file sizes with du like this:

du -h

But how can I list all the files which are beyond a specific disk space size like 5MB?

share|improve this question
3  
Use find find /path -type f -size +5M -exec ls -lh {} \; –  Terry Wang Dec 27 '12 at 12:23
add comment

3 Answers 3

up vote 0 down vote accepted

Here is a straight-forward solution in bash, which analyzes the size of both files and folders:

#!/bin/bash

folder="$1"
limit="$2"

IFS=$'\n'
for item in `find "$folder"`; do
    size=$(du -s "$item" | cut -f1)
    if [ $size -gt $limit ]; then
        echo $item
    fi
done

first param is the target folder to examine
second param is the limit in kilobytes, where 1K=1024 bytes.

share|improve this answer
    
hi just now i saw this , i will try and let you know . –  AgentCool Dec 27 '12 at 12:54
    
well I am getting this errors bro . .Moviz/s.sh: 9: [: -gt: unexpected operator du: cannot access `ter': No such file or directory But things were there actually . I think we need more eye here . –  AgentCool Dec 27 '12 at 15:23
    
@snow sounds weird, I've tested this script on my ~/Musik folder, where there is plenty of files with spaces and may be some other "unusual" characters in path - all okay. How do you run the script? Have you specified folder and limit arguments? I've not made any checks on them to keep it simple. Your are supposed to run it like this: ./findLargeItems.sh ~/Music 10000. Don't forget to quote path or backslash spaces in it: "/home/user/random folder" or /home/user/random\ folder. I guess you know all this, but just in case. –  Pavel A Dec 27 '12 at 16:05
add comment
find /home/stephenm/ -maxdepth 1 -size +20k -exec du -h {} \;

That should list anything over 20k in /home/stephenm

to recurse into sub directorys drop the -maxdepth 1 option.

share|improve this answer
    
Yeah it Nice and working . May I get a way with du also please . –  AgentCool Dec 27 '12 at 12:21
    
@snow I don't think du can do it on its own I maybe wrong I just looked at the manual. –  Shutupsquare Dec 27 '12 at 12:25
    
@snow, see a solution with du (I mean where it is used to analyze size) below, though the solution in Stephen's answer is apparently more elegant - I should definitely read over man find again. –  Pavel A Dec 27 '12 at 12:51
    
@snow Wait a minute. If I'm right neither this answer nor Terry's comment take the size of folders into account - they are just for files. –  Pavel A Dec 27 '12 at 13:01
    
@pavel A Yes as i have seen its working for files & not working for folders . –  AgentCool Dec 27 '12 at 13:14
add comment

Be careful: du does not print the size of a file, it gives you an estimate of file space usage.

You can create a 10 MB file, which uses much less:

dd if=/dev/zero of=file seek=10M count=1 bs=1
stat -c %s file
ls -lh file
du -h file
cat file | wc

An example is a compressed file that uses less space on disk that its size, of like the example above a sparse file.

In the example above, find . -type f -size +5M will output file even if du is much less than 5M.

If you know you do not have newlines in your filenames, you can filter the output of find with:

find . -type f -size +5M | while IFS= read -r file; do
  du=$(du -k "$file")
  size="${du%%$(printf "\t")*}"
  if test $size -gt $((5 * 1024)); then
    echo "$file"
  fi
done

If you can have newlines in your filenames, you could use this GNU extension, but then use something else than echo in the loop:

find . -type f -size +5M -print0 | while IFS= read -r -d '' file; do
  du=$(du -k "$file")
  size="${du%%$(printf "\t")*}"
  if test $size -gt $((5 * 1024)); then
    echo "$file"
  fi
done
share|improve this answer
    
Good point about the actual and apparent file sizes, though I doubt TS has any. B/w, you can view the apparent file size with du as well: du --apparent <file> in which case the reported size will be the same as with ls. –  Pavel A Dec 27 '12 at 21:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.