How to grep Lookup for Filenames As a substitute of Written content in Linux

How to grep Lookup for Filenames As a substitute of Written content in Linux

Contents

[ad_1]

Bash Shell

grep is a Linux device generally made use of for searching textual content documents for specific material. Even so, it is often handy to look for directories for file names in its place of file contents, and this can be finished with grep and other Linux command line utilities

Working with discover In its place of grep

The grep utility fundamentally takes string enter from files or common enter and makes use of designs or Regex to lookup by way of it and print matching traces.

You can technically use grep by by itself to research for file names in its place of written content, but it is only simply because Linux makes it possible for wildcards in filename inputs. However, you can just as effortlessly use ls to listing information this way, or use wildcards in any other command, and it isn’t a true resolution for looking filenames like how grep searches content material.

grep "" ./file* -l

The genuine option is to use the obtain utility, which can search through sub-directories and offers the most resilient way to research for data files as it interacts right with the filesystem. This is the most normally utilised utility for browsing through directories, and it has a lot of options—including sample matching and Regex aid.

The most primary use is to use discover on its individual to print out a listing of documents, including subdirectories, and feed that input to grep. If you previously have scripts employing grep, it will be really uncomplicated to transform them to matching this way.

find | grep "file"

You can also use patterns specifically with locate, eradicating the have to have for grep. Use -iname with an enter.

discover . -iname 'file_*.txt'

Not like grep however, the find command is a very little far more strict—you need to have to use one or double rates to escape the look for string, and you need to use wildcards to match the full string. Simply just acquiring a substring in the identify is not attainable without making use of wildcards to match the rest of the title.

See also  Adrestia Therapeutics — Gene networks to the rescue

Making use of Frequent Expressions (Regex) With obtain

You can also use standard expressions with uncover, which will allow substantially more sophisticated matching:

find . -regex './file.*'

This regex will match all information in the present directory setting up with “file.” Nonetheless, it’s essential to be knowledgeable that this will work differently than -iname the enter includes the whole listing route, and must use ./ to escape the relative path, ./.

This command revealed below is excluding the ./subdirectory/ folder, because it does not start off with “file”. To fix this, you will have to match every thing leading up to the final forward slash with .*/:

find . -regex '.*/file.*'

Common expressions are really complex, but quite powerful. If you’d like to discover additional, you can examine our tutorial on how they do the job.

Linked: How Do You Actually Use Regex?

Leave a Reply

Your email address will not be published. Required fields are marked *