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

前端 未结 15 547
无人及你
无人及你 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条回答
  • Using gnu find, I think this is what you want. It finds all real files and not directories (-type f), and for each one prints the filename (%p), a tab (\t), the size in kilobytes (%k), the suffix " KB", and then a newline (\n).

    find . -type f -printf '%p\t%k KB\n'
    

    If the printf command doesn't format things the way you want, you can use exec, followed by the command you want to execute on each file. Use {} for the filename, and terminate the command with a semicolon (;). On most shells, all three of those characters should be escaped with a backslash.

    Here's a simple solution that finds and prints them out using "ls -lh", which will show you the size in human-readable form (k for kilobytes, M for megabytes):

    find . -type f -exec ls -lh \{\} \;
    

    As yet another alternative, "wc -c" will print the number of characters (bytes) in the file:

    find . -type f -exec wc -c \{\} \;
    
    0 讨论(0)
  • 2020-12-07 11:31

    Why not use du -a ? E.g.

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

    Works on a Mac

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

    Awk can fix up the output to give just what the questioner asked for. On my Solaris 10 system, find -ls prints size in KB as the second field, so:

    % find . -name '*.ear' -ls | awk '{print $2, $11}'
    5400 ./dir1/dir2/earFile2.ear
    5400 ./dir1/dir2/earFile3.ear
    5400 ./dir1/dir2/earFile1.ear
    

    Otherwise, use -exec ls -lh and pick out the size field from the output. Again on Solaris 10:

    % find . -name '*.ear' -exec ls -lh {} \; | awk '{print $5, $9}'
    5.3M ./dir1/dir2/earFile2.ear
    5.3M ./dir1/dir2/earFile3.ear
    5.3M ./dir1/dir2/earFile1.ear
    
    0 讨论(0)
  • 2020-12-07 11:34

    I struggled with this on Mac OS X where the find command doesn't support -printf.

    A solution that I found, that admittedly relies on the 'group' for all files being 'staff' was...

    ls -l -R | sed 's/\(.*\)staff *\([0-9]*\)..............\(.*\)/\2 \3/'
    

    This splits the ls long output into three tokens

    1. the stuff before the text 'staff'
    2. the file size
    3. the file name

    And then outputs tokens 2 and 3, i.e. output is number of bytes and then filename

    8071 sections.php
    54681 services.php
    37961 style.css
    13260 thumb.php
    70951 workshops.php
    
    0 讨论(0)
  • 2020-12-07 11:34
    find . -name "*.ear" | xargs ls -sh
    
    0 讨论(0)
  • 2020-12-07 11:41
    find . -name '*.ear' -exec ls -lh {} \;
    

    just the h extra from jer.drab.org's reply. saves time converting to MB mentally ;)

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