0

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?

4
  • 1
    What about ... | xargs -i basename "{}" | ...? Commented Jun 22, 2015 at 2:31
  • @higuaro Thanks! (You might want to put that as the answer, by the way.) Commented Jun 22, 2015 at 2:50
  • Is there some problem with echo I*? (Perhaps you really need a recursive listing, or maybe you have folders whose names start with I?) Commented Jun 22, 2015 at 2:51
  • @rici Yes, to clarify I wanted a recursive list. Commented Jun 22, 2015 at 2:57

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice; these ran pretty fast compared to piping it through xargs.
@JamesKo: The second one should be fastest, if your find supports it, since it doesn't require firing up any new process.
1

Using xargs -i should solve the problem:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.