Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input redirection in find -exec option

Why doesn't this command work?

find / -type f -name "*.c" -exec wc -c < \{} \;

I'm trying to hide the filename while showing number of characters

like image 668
FilBr Avatar asked Oct 23 '25 08:10

FilBr


2 Answers

You can do the following:

find / -type f -name "*.c" -exec wc -c {} + | awk '{print $1}'

< does not work because redirection applies to the find command itself. So what you can do is use awk to only print the first column

As pointed out by @Charles Duffy, this is a bit more efficient since it means we're only starting one wc process, and no shell wrapper, once per file (as the awk instance is shared, not invoked per-file).

like image 161
M. Becerra Avatar answered Oct 25 '25 01:10

M. Becerra


Use a bash command with exec like below

find . -type f -name "*.c" -exec bash -c 'wc -c < "$1"' _ {} \;

In your case the redirection < applies to the find command itself resulting a syntax error as there is no file literally named {}.


A sidenote : The positional parameter inside bash shell should be double quoted to deal with non-standard filenames, say those with spaces and so.


Edit

To NOT pay the extra cost of invoking multiple sub-shells, we can further improve the exec part like below

find . -type f -name "*.c" -exec bash -c 'for arg; do wc -c <"$arg"; done' _ {} +

Courtesy @Charles Duffy for this suggestion.


like image 20
sjsam Avatar answered Oct 25 '25 01:10

sjsam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!