How do I get the find command to print out the fil

2019-01-21 05:28发布

If I issue the find command as follows:

$ find . -name *.ear

It prints out:

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

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

./dir1/dir2/earFile1.ear  5000 KB
./dir1/dir2/earFile2.ear  5400 KB
./dir1/dir3/earFile1.ear  5400 KB

14条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-21 05:42
find . -name '*.ear' -exec ls -lh {} \;

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

查看更多
聊天终结者
3楼-- · 2019-01-21 05:43
find . -name "*.ear" | xargs ls -sh
查看更多
Juvenile、少年°
4楼-- · 2019-01-21 05:43
$ find . -name "test*" -exec du -sh {} \;
4.0K    ./test1
0       ./test2
0       ./test3
0       ./test4
$

Scripter World reference

查看更多
老娘就宠你
5楼-- · 2019-01-21 05:50

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 \{\} \;
查看更多
趁早两清
6楼-- · 2019-01-21 05:50

Why not use du -a ? E.g.

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

Works on a Mac

查看更多
Fickle 薄情
7楼-- · 2019-01-21 05:51
find . -name '*.ear' -exec du -h {} \;

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

查看更多
登录 后发表回答