How to get find to print file size with file name?

If I issue the find command as follows:

$ find . -name *.ear 

He prints:

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

What I want to "print" on the command line is the name and size:

 ./dir1/dir2/earFile1.ear 5000 KB ./dir1/dir2/earFile2.ear 5400 KB ./dir1/dir3/earFile1.ear 5400 KB 
+63
command-line unix find solaris
Sep 15 '08 at 16:53
source share
14 answers
 find . -name '*.ear' -exec ls -lh {} \; 

just h extra from jer.drab.org answer. saves time turning into MB mentally;)

+68
Sep 15 '08 at 16:59
source share

You need to use -exec or -printf. Printf works as follows:

 find . -name *.ear -printf "%p %k KB\n" 

-exec is more powerful and allows you to execute arbitrary commands, so you can use the version of "ls" or "wc" to print the file name along with other information. "man find" will show you the available printf arguments, which can do much more than just file size.

[edit] -printf is not in the official POSIX standard, so check to see if it is supported in your version. However, most modern systems will use GNU find or a similar extended version, so there is a chance that it will be implemented.

+73
Sep 15 '08 at 16:58
source share

a simple solution is to use the -ls option in find:

 find . -name \*.ear -ls 

This gives you each entry in the usual ls -l format. Or, to get the specific result that you seem to be looking for is:

 find . -name \*.ear -printf "%p\t%k KB\n" 

Which will give you the file name followed by the size in KB.

+17
Sep 15 '08 at 16:57
source share

Using gnu find, I think this is what you want. It finds all real files, not directories (-type f), and for each it prints the file name (% p), tab (\ t), size in kilobytes (% k), suffix "KB", and then newline (\ n).

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

If the printf command does not format things the way you want, you can use exec, and then the command you want to execute for each file. Use {} for the file name and end the command with a semicolon (;). In most shells, all three of these characters must be escaped with a backslash.

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

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

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

 find . -type f -exec wc -c \{\} \; 
+12
Sep 15 '08 at 17:08
source share
 find . -name '*.ear' -exec du -h {} \; 

This gives you only the file size and not all unnecessary things.

+6
Jan 13 '14 at 21:28
source share

Awk can correct the exit to give what the questioner asked. On my Solaris 10 system, find -s prints the size in KB as the second field, therefore:

 % 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 select 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 
+3
Oct 28 '08 at 21:14
source share

I struggled with this on Mac OS X, where the find command does not support -printf .

The solution I found that admittedly relies on a β€œgroup” for all the files being β€œstaff” was ...

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

This divides the long ls output into three markers

  • material before the text "Personnel"
  • file size
  • file name

And then it gives out tokens 2 and 3, that is, the output is the number of bytes, and then the file name

 8071 sections.php 54681 services.php 37961 style.css 13260 thumb.php 70951 workshops.php 
+3
Dec 03 2018-11-12T00:
source share

Why not use du -a ? For example.

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

Works on mac

+3
Jan 06 '13 at 17:34
source share
 $ find.  -name "test *" -exec du -sh {} \;
 4.0K ./test1
 0 ./test2
 0 ./test3
 0 ./test4
 $

Link to World of Scripter

+1
Jan 05 '10 at 20:37
source share

This should provide you with what you are looking for, including formatting (e.g. file name first and size):

 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 
+1
Mar 27 '14 at 2:56
source share

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 not standard, so du or wc might be a better approach:

 find . -type f -name *.ear -exec sh -c 'echo "{} $(wc -c < {})"' ';' 
+1
Feb 01 '16 at 11:51
source share
 find . -name "*.ear" -exec ls -l {} \; 
0
Sep 15 '08 at 16:55
source share

You can try the following:

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

This will give you the size in bytes. But du also accepts -k for KB and -m for MB. This will give you a result like

 5000 ./dir1/dir2/earFile1.ear 5400 ./dir1/dir2/earFile2.ear 5400 ./dir1/dir3/earFile1.ear 
0
Sep 15 '08 at 16:56
source share
 find . -name "*.ear" | xargs ls -sh 
0
Sep 15 '08 at 16:57
source share



All Articles