Execute a command on a list of strings in Bash

603 views Asked by At

I'm trying to find all C# interfaces from a given directory. I tried doing this command:

find . -type f | xargs basename | grep ^I

but basename is giving back an error since I'm sending it a list of strings, not a string itself. How do I get the output of basename executed over all the strings piped to it?

2

There are 2 answers

2
rici On BEST ANSWER

You don't need to use xargs for this. You can use:

find . -type f -name 'I*' -exec basename '{}' ';'

If you are using GNU find, you don't need basename either:

find . -type f -name 'I*' -printf %f\\n

Here, %f is the GNU find printf format for "filename with all but the last component removed". There are many other possible format codes; see man find for details.

0
higuaro On

Using xargs -i should solve the problem:

find . -type f | xargs -i basename "{}" | grep ^I