Linux

Linux Searching Commands

In this section you will learn the Searching commands in Linux.

grep

The grep command search the line from given file. With the following examples you will see the use of grep command.
grep command syntax

[root@localhost ~] # grep 'word' filename
Use grep to search file
Search /etc/passwd for mohit user
grep for searching
grep for string searching

You can force grep to ignore word case i.e match mohit, Mohit and all other combination with -i option.
grep for searching case sensitive
grep for string searching case sensitive

grep recursively
Syntax

grep -r directory_name
You can search recursively i.e. read all files under each directory for a string mohit.
grep recursively
grep recursively

grep to search words only
Syntax

grep -w string file_name
When you search for mohit, grep will match mohit, mohitraj, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only mohit word:
grep -w
grep exact match

grep to search 2 different words
Syntax

grep -w word1|word2 file_name
use egrep as follows:
grep -w
grep to search 2 different words

Count line when words has been matched
Syntax

grep -c string file_name
grep can report the number of times that the pattern has been matched for each file using -c (count) option:
grep -c
grep count lines of matched word

Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:
grep -n
grep print number of matched word


Pipes ( | )

A pipe is a way to connect the output of one program to the input of another program without any temporary file.
Syntax:
command1 | command2
pipe
Linux pipe
Examles:
[root@localhost ~]# cat /proc/cpuinfo | grep -i 'model'
pipe
Linux pipe
Check the output of ls | more and who | sort


Finding and Locating files with find command

The find command allows you to search for files according to various criteria. Here is the general format of find:
Let us try out some examples.
You wish to find out all *.conf files located under /etc directory, enter
Linux find command
Linux find command
Here -name means File's name should be .conf
You would like to find httpd.conf file location:
Linux find command
Linux find command

Finding all files owned by a user. Find out all files owned by user mohit:
Linux find command
Linux find command


Finding files according to date and time
Linux find command
Linux find command
  • -atime +7: All files that were last accessed more than 7 days ago.
  • -atime 7: All files that were last accessed exactly 7 days ago.
  • -atime -7: All files that were last accessed less than7 days ago.

Locate command

The locate command is the simplest and quickest way to find the locations of files.
Linux locate command
Linux locate command

Some useful command

Clear command
To clear the terminal screen
[root@localhost ~] # clear

Creating aliases
Syntax
[root@localhost ~] # alias newname="original command"
Linux Creating aliases
Linux creating aliases

To eject CD-ROM
[root@localhost ~] # eject

used for load the CD ROM
[root@localhost ~] # eject -t


To SHUTDOWN the system
[root@localhost ~] # init 0

To Restart the system
[root@localhost ~] # init 6


Disk Free: df
The df program displays the amount of free space, partition by partition. The drives/partitions must be mounted in order to get this information.
To show the free space for all locally mounted drivers, use this command:
[root@localhost ~] # df -l
To show the free space in a human-readable format
[root@localhost ~] # df -h
To show the free space in a human-readable format for the file system on which /tmp is located, type this command:
[root@localhost ~] # df -h /tmp








admin