File contents in Linux - data that is stored in a file and can be read or changed using various utilities and command line. Often it is necessary to search for files by them, further we will tell you how to do it.
The contents of files can be text or binary, depending on what type of data it stores. Text files contain characters and strings that can be read by humans. Binary files contain binary data such as executable code, images, video and audio files. The contents of the files can be displayed in a terminal using the command cat
or less
, and edited using text editors such as vi
, nano
or emacs
.
The file search by content in Linux is needed to quickly find files that contain a specific text, word, phrase, or character. It can be useful for finding configuration files, logs, and other files in large directories with many subdirectories. Also, it can be useful when exploring a file system in search of files containing a specific virus or unwanted code. With powerful and flexible file search by content, you can quickly find the files you need and work with them conveniently.
To search for files by content in Linux, you can use the command grep
.
Command syntax:
grep [options] [template] [files]
/home/user
, containing the string "hello"
:
grep -r "hello" /home/user
/etc
folder and its subfolders that contain the word "network"
:
grep -R "network" /etc
/var/log
folder that contain the message "error"
:
grep -R "error" /var/log
.txt
containing string "important"
in folder /home/user
:
grep "important" /home/user/*.txt
"password"
, in the folder /etc
and save the result to the file passwords.log
:
grep -R "password" /etc > passwords.log
-i
: ignore case when searching-n
: output the numbers of lines containing the desired information-c
: output only the number of lines containing the desired information-v
: search for lines that do not contain the specified informationIn addition to the grep
command, you can use the find
command to search for files with specific contents in specified folders. However, searching with this command is slower than with the grep
command.
Apply the discount by inserting the promo code in the special field at checkout: