xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

9,021

It appears that some of your filenames have apostrophes (single quote) in their names.

Luckily, find and xargs have ways around this. find's -print0 option along with xargs's -0 option produce and consume a list of filenames separated by the NUL (\000) character. Filenames in Linux may contain ANY character, EXCEPT NUL and /.

So, what you really want is:

 find ~ -type f -print0 | xargs -0 --no-run-if-empty wc -w

Read man find;man xargs.

Share:
9,021

Related videos on Youtube

user10726006
Author by

user10726006

Updated on September 18, 2022

Comments

  • user10726006
    user10726006 over 1 year

    I'd like to count all the ordinary file on home directory with commands:

    $ find ~ -type f | xargs echo | wc -w
    xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
    

    It prompts

    xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
    

    What's the problem with usage?

  • pim
    pim over 5 years
    find ~ -type f | wc -l will work with more files, since xargs put all arguments on one command line and there is a limit in the number of args.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 5 years
    @pim While it may be correct that xargs can suffer from argument list too long error, in this answer -print0 and xargs -0 combination is perfectly acceptable. See unix.stackexchange.com/a/83803/85039
  • steeldriver
    steeldriver over 5 years
    I think the OP's use of wc -w may have led you to suppose they want to count the words inside the files - they're actually using xargs echo | wc -w which will count the number of words in the file names, which I think is more likely an attempt to count the number of files (although it doesn't take into account that filenames may contain whitespace). (Likewise, find ~ -type f | wc -l doesn't take into account that filenames may contain newlines.)