How to use find command with grep command
Manages arguments passed to another command. This is often used either to increase efficiency (avoiding calling a command once for each argument) or to overcome shell environment limits (“Argument list too long”). It works by collecting arguments and invoking your command.
xargs is used to pass command arguments to the other process . So if we want to transfer output of the find command to the grep command we can use xargs.
find . -type f -print | xargs grep ‘oldmonk”
This command will search for all file in the current directory recusively (only file not directory) which contain ‘oldmonk’
Explain find command in Unix
Filed under: Shell Scripting Interview Questions, Unix Command Interview Questions
find : Is used to find a file in the directory or a file system
Usage:
1. find . -print //List all file in the current directory recursive
2. find . -name *.html” -print // Find all file in the current directory with .html recursive
3. find . -mtime -7 -name “*.html” -print // Find all file which has been modified in last 7 days
4. find . -mtime 7 -name “*.html” -print // Find all file which has been exactly 7 days back
5. find . -mtime +7 -name “*.html” -print // Find all file which has not been modified in last 7 days
To find files in more than one directory through find command you can specify more than 1 directory at the start
for example:
6. find /usr /bin -name “*\.htm*” -print
To find file of specific type :
7. find . -type d -print //type d stands for directory
Below are the other type options in find command
d -Directory
f – File
l – Link
FIND COMMAND INTERVIEW QUESTIONS, FIND COMMAND USAGE, FIND COMMAND OPTIONS, WHY FIND COMMAND IS BEING USED UNIX FIND COMMAND
More find command examples :
find $HOME -mtime 0 // Find all files which has been modified in last 24 hours in your home directory
find $HOME -mtime +365 // Find all files which has not been modifed in last 365 days
find $HOME -newer ~joeuser/lastbatch.txt // Find all newer file in home directory which is newer than lastbatch.txt
More Linux find command options:
name: Finds files with certain naming conventions in the directory structure
-ctime time interval Locates files that that were created during the specified time interval
-mtime time interval Finds files that have been modified during the specified time interval
-atime time interval Locates files that have been accessed during the specified time interval
-perm permissions Locates files with certain permission settings
-user Locates files that have specified ownership
-group Locates files that are owned by specified group
-size Locates files with specified size
-type Locates a certain type of file
