Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:49:54+00:00 2026-06-17T16:49:54+00:00

I have a shell script find . -name *.java -print0 | xargs -0 grep

  • 0

I have a shell script

find . -name "*.java" -print0 | xargs -0 grep -Lz 'regular_expression'

which outputs file names not matching the regexp in this way:

file1.java
file2.java
...

The way I understand, it works as follows: find find needed files and concatenate their names with \0. Then xargs split the output of find with \0 and feeds them to grep one-by-one.

Then I wanted to add one more stage and get only basename of the files. I modified the script:

find . -name "*.java" -print0 | xargs -0 grep -LzZ 'regular_expression' | xargs -0 basename

but got an error. I started investigating and made an temporary output:

find . -name "*.java" -print0 | xargs -0 grep -LzZ 'regular_expression' | xargs -0  echo basename

and got this:

basename ./file1.java ./file2.java ./subdir/file1.java ./subdir/file2.java

So, the filenames were not split by \0. I can’t get why they are split in case of xargs used with grep and not split in xargs with basename.

I got a workaround by using -n1 in the latter xargs. But still I don’t understand why I needed it (given I didn’t use in in the xargs with grep) and what this parameter does.

Hope you can explain to me what -n1 does and why I needed it in the latter usage and didn’t need it in the former with grep.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:49:55+00:00Added an answer on June 17, 2026 at 4:49 pm

    The filenames were split by \0. The difference is in the commands you’re using. xargs normally takes its standard input, breaks it into a list (here, by splitting on NUL), and then passes that list as extra arguments to your command. So when you do this:

    find . -name "*.java" -print0 | xargs -0 grep -Lz 'regular_expression'
    

    What actually runs is this:

    grep -Lz 'regular_expression' file1.java file2.java file3.java...
    

    Here, the -z doesn’t matter because it only affects how grep reads stdin, and you’re not sending anything to its stdin.

    So, when you add another xargs that runs basename, you get this:

    basename file1.java file2.java file3.java...
    

    But while grep will take any number of filename arguments, basename only takes one and ignores the others.

    That’s where -n 1 comes in: it tells xargs to break its list of arguments into chunks (of 1), and run the command multiple times. So what runs now is:

    basename file1.java
    basename file2.java
    basename file3.java
    ...
    

    And all the output is concatenated together onto stdout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a shell script in which I have to find the last
I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
I have a shell script which outputs all of the .js and .html files
I have the below shell script in which sql file is called which has
I have a start up shell script which reads a value from the file,
I have the following shell script which writes volumes in a find command to
For testing purposes I have this shell script #!/bin/bash echo $$ find / >/dev/null
I have a shell script in which I have to execute two queries (on
I have a shell script, which is run under a opensuse linux, that starts
I have a shell script which runs a python program which has its own

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.