I will explain What are the Search and Find Commands on Linux in this tutorial series.
Read previous article before this.
There are some commands to look for specific patterns in the contents of the files.
Search commands used in Linux are as follows.
grep ( Global Regular Expression Printer ) command
It is a command that the most common command for Linux users. It stands for Global Regular Expression Printer. The main function of the grep command is to search for a specific expression, word, or pattern in a text within a file. I will explain in more detail the importance of this command. I have described the examples of grep used in different ways below.
1. $ grep “Search Pattern” FileName: this command search “mehmet” word in the KisiselBilgiler file.
2. $ grep -c “Pattern” FileName: This command calculates and returns how many “mehmet” word exists in the KisiselBilgiler file.
3. $ grep -i “Pattern” FileName: This command search “mehmet” word case insensitive in the KisiselBilgiler file
4. $ grep -v “Pattern” FileName: This command search and return except “mehmet” word in the KisiselBilgiler file.
Note: grep is often used with regular expressions. Let’s look at the usage patterns below and what they do.
grep ^ “Pattern” FileName : it lists with the word or letter start with the specified word in the Pattern.
grep “Pattern” $ FileName: it lists with the word or letter finish with the specified word in the Pattern.
egrep ( Extended Global Regular Expression Printer ) command
We said above Grep stands for “Global Regular Expressions Print”, and Egrep is like Grep but it stands for “Extended Global Regular Expressions Print”. The regular expression pattern , for which e in egrep stands for “Extended Regular Expressions” abbreviated ‘ERE’ is enabled in egrep and egrep is mostly used for regular expression.
For example; you can search “install” file under root directory with grep command. But If you want to search both install and jdk same time, you cannot use grep, you should use egrep. Because you can use regular expression with egrep.
[[email protected] ~]# ls anaconda-ks.cfg Documents glibc-2.14 install.log jdk1.7.0_79 Music Public Videos Desktop Downloads glibc-2.14.tar.gz install.log.syslog jdk-7u79-linux-x64.tar Pictures Templates [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# ls | grep install install.log install.log.syslog [[email protected] ~]# [[email protected] ~]# ls | grep "install|jdk" [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# ls | egrep "install|jdk" install.log install.log.syslog jdk1.7.0_79 jdk-7u79-linux-x64.tar [[email protected] ~]# [[email protected] ~]#
This command is similar to grep. The task of the command changes the contents of the text files according to the specified pattern and prints the screen. However, the change is not reflected in the source file, you must save it again.
Usage is as follows.
The functions of the sed command with different parameters are as follows.
- it is writen into the file with w parameter.
- it is deleted rows from the file with d parameter.
- it is read from the file with r parameter.
- it is print to the screen with p parameter.
This command performs file search operations. So with this command you can find a file under a directory
.Different usage methods and examples are as follows.
find / -name “FileName” : Bu kullanımda sistemde Belirtilen bir Dosya Adı bulunur. Kullanımı aşağıdaki gibidir.
find / -name “FileName” * : Burda belirtilen bir dosya adına benzer dosyalar bulunur.
note that In the find and sed commands, regular expressions can be used as in grep.
Locate is a file search command, just like the find command. But it runs faster than find and is widely used. Find is a bit slower as it searches in realtime, but locate does a search from a previously cataloged database or memory. This is why the locate command returns very fast results than the find command.
The use of the command is as follows.
locate FileName : A file name specified in the operating system is quickly found and displayed to the user.
locate -i FileName : A file name (case insensitive) specified in the operating system is quickly found and displayed to the user..
I will continue to explain Linux Command Tutorial for beginners in the next article.
Do you want to learn Linux System Administration for Beginners, then read the following articles.
1,753 views last month, 5 views today