Introduction to Linux - Searching

- 5 mins

Series


Index


Introduction

In this post, I will use the Linux command line to search files on the directory and search text within files. You need a basic understanding of Linux to go ahead. If you missed the first part READ from here.


Create directory and files

Next, we will create a temp directory and a few dummy files on which we search.
Let’s create a directory naming temp and move inside the directory

	#create a directory
	mkdir temp

	#move into that directory
	cd temp

Now create a bunch of files.

	touch file{1..10}.txt 

The command generates 10 different files with a .txt extension. Now we need to edit a few files and enter text for our search operation. We can use nano or vim to edit files. I am going to use vim here-

	#open three files sequentially
	vim file1.txt file2.txt file3.txt 

	#press i to insert, we are now in file1.txt, write down a simple text
	Hello world

	#press Esc button to exit edit mode
	#type :wn and press Enter to write the change and go to next file
	#we are now in file2.txt, write another text
	Hola Mundo is Hello world in Spanish

	#repeat the previous process to move to the next file file3.txt
	#write another text
	Bonjour le monde is Hello world in French

	#press Esc button
	#write :wq and press Enter to write the change and quit 

	#change a file name
	mv file5.txt File5.txt

Find command

Now the ground is ready. Let’s play with searching now. We use a command called find

	#find all file
	find . -type f

	#find all directory
	find . -type d

or we can search file by name or extension

	#find files which start with 'file'
	find . -name 'file*'

	#find all files with the .txt extension 
	find . -name '*.txt'

Now we have a problem. This command is case sensitive. So the search is ignoring file.File5.txt . To solve the issue use i before -name; like this-

	#ignore case and find files which start with the 'file'
	find . -iname 'file*'

Some other useful find method commands are-

	#find files with more than 2048 byte size
	find . -size +2048

	#default size is 512 and unit is byte
	#allowable units-> k for kilobyte, M for megabyte, G for gigabyte
	#More than -> + , Less than -> -

	#find files with less than 2048 byte size
	find . -size -2048

	#modification time more than 1 day
	find . -mtime +1 

	#modification time less than 1 day
	find . -mtime -1 

	#alternative of option 'atime' for access time and 'ctime' for 'created time'
	

To make a powerful search result, we can always combine commands.-

	#find files using 'or' or 'and' connector
	find . -iname "*.txt" -or -iname "*.doc"
	
	find . -iname "*.txt" -or -iname "*.doc" -and -mtime -1

	#ignore specific directory 'dirname' for search
	find . -iname "*.txt" -or -iname "dirname" -prune

	#ignore directory 'dirname' in search and printing output
	find . -iname "*.txt" -print -or -iname "dirname" -prune
	

Search command

There is another method to search within the file. It is called grep. grep requires two parameters. The first one is the search text and the second one is a filename.

	#search text within the file1.txt
	grep "Hello" file1.txt

	#OUTPUT
	#Hello world

	#we can use a wildcard in place of the file
	grep "Hello" *

	#OUTPUT
	#file1.txt:Hello world
	#file2.txt:Hola Mundo is Hello world in Spanish
	#file3.txt:Bonjour le monde is Hello world in French

	#And ignore case too
	grep -i "hello" *

These commands will show if the search text found or not. We also can print the list of files where search key exists-

	#List if files
	grep -il "hello" *

	#OUTPUT
	#file1.txt
	#file2.txt
	#file3.txt


	#Recursively search
	grep -ilr "hello" *

Recursive search has one drawback. It always required a wildcard to do a recursive search. But this is Linux. And every problem has a workaround. So you can use a combination of different command.

	#We are using the 'find' command again with a combination of 'grep' command
	#{} is placeholder for current file
	#; to signal the exec command to end
	#\ for escaping ;
	find . -name '*.txt' -exec grep -il "Hello" {} \;

	#OUTPUT
	#./file1.txt
	#./file3.txt
	#./file2.txt

But exec has risk too. It may cause unavoidable security problems, so it is recommended to use execdir instead of exec

	#the same command only replaces 'exec' with 'execdir'.
	find . -name '*.txt' -execdir grep -il "Hello" {} \;

	#OUTPUT
	#./file1.txt
	#./file3.txt
	#./file2.txt

Conclusion

That is all for today. Let’s practice.


comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora