How to pipe list of files returned by find command to cat to view all the files

后端 未结 15 1447
庸人自扰
庸人自扰 2020-12-02 03:44

I am doing a find and then getting a list of files. How do I pipe it to another utility like cat (so that cat displays the contents of all those fi

15条回答
  •  悲&欢浪女
    2020-12-02 04:05

    In bash, the following would be appropriate:

    find /dir -type f -print0 | xargs -0i cat {} | grep whatever
    

    This will find all files in the /dir directory, and safely pipe the filenames into xargs, which will safely drive grep.

    Skipping xargs is not a good idea if you have many thousands of files in /dir; cat will break due to excessive argument list length. xargs will sort that all out for you.

    The -print0 argument to find meshes with the -0 argument to xargs to handle filenames with spaces properly. The -i argument to xargs allows you to insert the filename where required in the cat command line. The brackets are replaced by the filename piped into the cat command from find.

提交回复
热议问题