GNU/Linux Bash Commands Tutorial for Beginners -5 Search and Find Files and Pattern Commands

Hi,

I will explain What are the Search and Find Commands on Linux in this tutorial series.

 

Read previous article before this.

 

Search Commands

There are some commands to look for specific patterns in the contents of the files.

Search commands used in Linux are as follows.

  • grep
  • egrep
  • sed
  • find
  • locate

 

 

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.

[root@MehmetSalih ~]# 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
[root@MehmetSalih ~]# 
[root@MehmetSalih ~]# 
[root@MehmetSalih ~]# ls | grep install
install.log
install.log.syslog
[root@MehmetSalih ~]# 
[root@MehmetSalih ~]# ls | grep "install|jdk"
[root@MehmetSalih ~]# 
[root@MehmetSalih ~]# 
[root@MehmetSalih ~]# ls | egrep "install|jdk"
install.log
install.log.syslog
jdk1.7.0_79
jdk-7u79-linux-x64.tar
[root@MehmetSalih ~]# 
[root@MehmetSalih ~]#

 

sed command

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.

 

 

 

find command

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 command

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.

Mehmet Salih Deveci

I am Founder of IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience. I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource. I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks. I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients. If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress mehmetsalih.deveci@outlook.com. -                                                                                                                                                                                                                                                 - Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  mehmetsalih.deveci@outlook.com a mail atabilirsiniz.