How do I get the find command to print out the file size with the file name?

前端 未结 15 548
无人及你
无人及你 2020-12-07 11:01

If I issue the find command as follows:

$ find . -name *.ear

It prints out:

./dir1/dir2/earFile1.ear
./dir1/dir2/earFile2.         


        
相关标签:
15条回答
  • 2020-12-07 11:41

    If you need to get total size, here is a script proposal

    #!/bin/bash
    totalSize=0
    
    allSizes=`find . -type f -name *.ear -exec stat -c "%s" {} \;`
    
    for fileSize in $allSizes; do
        totalSize=`echo "$(($totalSize+$fileSize))"`
    done
    echo "Total size is $totalSize bytes"
    
    0 讨论(0)
  • 2020-12-07 11:43
    find . -name '*.ear' -exec du -h {} \;
    

    This gives you the filesize only, instead of all the unnecessary stuff.

    0 讨论(0)
  • 2020-12-07 11:46

    This should get you what you're looking for, formatting included (i.e. file name first and size afterward):

    find . -type f -iname "*.ear" -exec du -ah {} \; | awk '{print $2"\t", $1}'
    

    sample output (where I used -iname "*.php" to get some result):

    ./plugins/bat/class.bat.inc.php  20K
    ./plugins/quotas/class.quotas.inc.php    8.0K
    ./plugins/dmraid/class.dmraid.inc.php    8.0K
    ./plugins/updatenotifier/class.updatenotifier.inc.php    4.0K
    ./index.php      4.0K
    ./config.php     12K
    ./includes/mb/class.hwsensors.inc.php    8.0K
    
    0 讨论(0)
  • 2020-12-07 11:47

    Try the following commands:

    GNU stat:

    find . -type f -name *.ear -exec stat -c "%n %s" {} ';'
    

    BSD stat:

    find . -type f -name *.ear -exec stat -f "%N %z" {} ';'
    

    however stat isn't standard, so du or wc could be a better approach:

    find . -type f -name *.ear -exec sh -c 'echo "{} $(wc -c < {})"' ';'
    
    0 讨论(0)
  • 2020-12-07 11:48

    You could try this:

    find. -name *.ear -exec du {} \;
    

    This will give you the size in bytes. But the du command also accepts the parameters -k for KB and -m for MB. It will give you an output like

    5000  ./dir1/dir2/earFile1.ear
    5400  ./dir1/dir2/earFile2.ear
    5400  ./dir1/dir3/earFile1.ear
    
    0 讨论(0)
  • 2020-12-07 11:48
    $ find . -name "test*" -exec du -sh {} \;
    4.0K    ./test1
    0       ./test2
    0       ./test3
    0       ./test4
    $
    

    Scripter World reference

    0 讨论(0)
提交回复
热议问题