filesize - Show file size by type in Linux?

08
2013-08
  • gkhewitt

    I'm trying to find out the total size of all files within a directory that have a particular extension.

    I do some offsite backup via rsync but due to limited bandwidth and disk space at the other end I can't do everything, so I'd like to find out, for example, how much disk space MP3 files take up so I can decide whether to remove the mp3 extension from the current list of rsync excluded patterns.

    It's not as simple as doing a 'du -sh' on the My Music directory in there as there's some other file types.

    Thanks!

  • Answers
  • John T

    You can use du:

    find Music/ -type f -name "*.mp3" -exec du -shc {} + | tail -1 | awk '{print $1}'

    output example:

    980M
  • pavium

    I just tried the following

    find Music/ -name '*.mp3' -exec ls -l {} \; | awk '{ SUM += $5} END { print SUM/1024/1024 }'
    

    And got the correct answer in Megabytes (1024 x 1024)

  • Jeffrey Vandenborne
    find Music/ -iname "*.mp3" -type f -exec stat -c "%s" {} \; | awk '{SUM+=$0} END {print SUM/1024/1024}'
    

    Got the correct answer as well, exactly the same as pavium's solution, could be a bit more reliable though.


  • Related Question

    osx - file size returned from ls - Linux vs OS X
  • dtlussier

    I have been moving a large number of files between a Linux system (ext3) and Mac OS X (HFS) and have noticed the slight variation in how the file size is reported by the ls command.

    Having done some digging round the man pages I'm guessing this difference has to do with how the actual data is stored on the disk, and/or how the ls command on each system is looking at the size (i.e. disk usage, blocks used, etc.).

    However, I'm still confused and wondering if there is a simpler answer to the different file sizes between the two systems. Is this a difference in the ls command, or in the filesystems? etc.


  • Related Answers
  • John T

    Likely it's the file system. The command is probably reporting a value as the size on disk rather than the actual size, and since each machine may be using a different cluster size, you see different values. More on the topic in this question.

    You can use tune2fs -l to see how your file system is currently configured.

  • Matt Garrison

    Which version of OSX are you running? Snow Leopard now uses base-10 calculations instead of base-2. If you're on 10.5 or older, I would attribute it to filesystems. Try comparing filesizes from a FAT32 thumbdrive.