How to Find Files by Content on Linux VPS

Introduction

Is is extremely useful for every Linux user to know how to find files by its content. This will save a lot of time avoiding the browsing of each file and going through all directories and files throughout the system.

In this guide we will show you how to use the Grep, a tool that searches the named input FILEs (or any standard input if no files are named) for lines containing a match to the given PATTERN.


Another great tool is Find, which searches for files in a directory hierarchy. It searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence.


Find Files by Content with Grep Command


The general synopsis of grep is:

grep [options] PATTERN [FILE...]

A simple example would be any of the following implementations:

grep 'string' *.xml
grep 'content(' *.txt
grep '#include<example.h>' *.c
grep 'getChar*' *.c
grep -i 'ultra' *.conf
grep -iR 'ultra' *.conf

Remember:

-i option is the equivalent to --ignore-case where case distinctions are ignored in both, the PATTERN and the input files.

-R or -r stands for --recursive so it reads all files under each directory, recursively.


Highlight Search Patterns with Grep


You can highlight the result of the search pattern as follows:

grep --color=auto 'mycontent' *.txt

Display names and numbers with Grep


You can also display the line number and filename of the search pattern results, which is useful when working with large number of files matching the pattern:

grep --nH 'mycontent' *.txt

Where:

-h or --with-filename Prints the filename for each match.

-n or --line-number Prefix each line of output with the line number within its input file.


Use Find Command to Search File Content on Linux


You can use the find command to locate content withing a file as follows:

find . -name "*.txt" -print | xargs grep "mycontent"
 
  • 0 Korisnici koji smatraju članak korisnim
Je li Vam ovaj odgovor pomogao?

Vezani članci

How to enable root login in Debian and Ubutnu VPS

To enable SSH login for a root user on Debian Linux system you need to first configure SSH...

HOW TO REGISTER NAME SERVERS

There are many registars in the world with different panels, however we have provided a...

Powered by WHMCompleteSolution