简体   繁体   中英

how to list filename using find and grep

I want list filename in a recursive directory based on filename pattern.

Here is the code I have developed so far:

find . -name "*pipeline*" | grep -R 'pipeline'

However its output is:

static/js/angular/angular-scenario.js:   * and `$validators` pipelines. If there are no special {@link ngModelOptions} specified then the staged
static/js/angular/angular-scenario.js:   * will not invoke the `$parsers` and `$validators` pipelines. For this reason, you should
static/js/angular/angular.js: * * if the value returned from the `$parsers` transformation pipeline has not changed
static/js/angular/angular.js: * @property {Array.<Function>} $parsers Array of functions to execute, as a pipeline, whenever
static/js/angular/angular.js: * @property {Array.<Function>} $formatters Array of functions to execute, as a pipeline, whenever
static/js/angular/angular.js:   * and `$validators` pipelines. If there are no special {@link ngModelOptions} specified then the staged
static/js/angular/angular.js:   * will not invoke the `$parsers` and `$validators` pipelines. For this reason, you should
social/pipeline.py:    print ("at pipeline *******", details, uid, social, kwargs, response)

And the result I am expecting will be a file name with pipeline as shown below:

social/pipeline.py
social/__pycache__/pipeline.cpython-35.pyc

command

find ./ -type f -print | grep -i pipeline*

output will be

./social/pipeline.py
./social/__pycache__/pipeline.cpython-35.pyc

I guess you want to list names of files that contain 'XYZ'. If it is so, you can do this:

find . -name "*pipeline*" -exec grep -l XYZ {} \;

What this does is it finds all files that include pipeline in filename. find works recursively so there is no need for -R flag to grep . Then it executes grep -l pipeline and {} denotes arguments passed by find . They get inserted one by one so grep runs on each file. -l denotes that you want filenames of matches only and \\ signals -exec that this is the end of command it should execute.

For example, your tree structure is

.
|--pipeline.txt
|--whatpipe.txt
|--anotherdir
   |--pipeline1.txt
   |--xyz.txt
   |--anotherdir1
      |--abapipeline.txt

and file contents are

**./pipeline.txt**
vnsdkvmvd
mvckdslm

**./whatpipe.txt**
vnsdkvmXYZvd
mvckdslm

**./anotherdir/pipeline1.txt**
vnsdkvmXYZvd
mvckdslm

**./anotherdir/xyz.txt**
vnsdkvmvd
mvckdslm

**./anotherdir/anotherdir1/abapipeline.txt**
vnsdkvmXYZvd
mvckdslm

The output would then be

$ find . -name "*pipeline*" -exec grep -l XYZ {} \;
anotherdir/pipeline1.txt
anotherdir/anotherdir1/abapipeline.txt

The problem is that grep -R ignores standard input. Thus, for this command, the output of find is ignored. grep -R performs its own independent search for files of any name:

find . -name "*pipeline*" | grep -R 'pipeline'

The solution is to remove find and use grep's include option:

grep -R --include='*pipeline*' pipeline

Example

Consider a directory with these files:

$ find . -type f
./social/pipeline.py
./social/match_but_wrong_name.py
./not_a_match

Our command finds only the desired file:

$ grep -R --include='*pipeline*' pipeline
social/pipeline.py:    print ("at pipeline *******", details, uid, social, kwargs, response)

The option --include='*pipeline*' tells grep to only search for files whose names contain the string pipeline .

Documentation

From man grep :

--include=GLOB
Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM