One of the most powerful commands in Unix is the find command. Its amazing to see what all can be accomplished with this command. A question that was once asked to me was how to find all the files which contain a particular word in it.
For the sake of this example lets say the word in question was “xyz”
The person who asked was convinced that ls -ltr | grep -i “xyz” would work fine. NO. It does’nt. The reason is that, ls -ltr would give a text output, which would then be piped to the grep command.
So what would essentially happen is that the grep command would act on the names of the files being listed, rather than the contents of the files.
Ex: if the directory where you run ls -ltr contains three files, file1.txt, file2.txt, file3.txt the following output would be displayed.
So how do we do this?
The find command comes in handy in such a scenario. The command which does the trick is below. I am assuming that the command is being run in the directory where the files are located. Alernatively, the entire path can also be provided.
The key part in the command giving the required results is the exec command which executes the commands grep -i “xyz” on every filename thats provided by the find command. The braces { } \; represent the filename from the find command.
This is just one of the options in the find command. There are plenty of others using which a variety of tasks can be accomplished using the most simplest methods.
For the sake of this example lets say the word in question was “xyz”
The person who asked was convinced that ls -ltr | grep -i “xyz” would work fine. NO. It does’nt. The reason is that, ls -ltr would give a text output, which would then be piped to the grep command.
So what would essentially happen is that the grep command would act on the names of the files being listed, rather than the contents of the files.
Ex: if the directory where you run ls -ltr contains three files, file1.txt, file2.txt, file3.txt the following output would be displayed.
-rw-r--r-- 1 oracle oinstall 0 Aug 24 18:24 file1.txt -rw-r--r-- 1 oracle oinstall 0 Aug 24 18:24 file2.txt -rw-r--r-- 1 oracle oinstall 0 Aug 24 18:25 file3.txtAnd grep would act on this output. It would be the same as using grep on a file containing this data.
So how do we do this?
The find command comes in handy in such a scenario. The command which does the trick is below. I am assuming that the command is being run in the directory where the files are located. Alernatively, the entire path can also be provided.
find . -name “*.*” -exec grep -i “xyz” { } \;
Note: There is a space in between the braces here. This is just for display purposes. Please remove it when typing the command.The key part in the command giving the required results is the exec command which executes the commands grep -i “xyz” on every filename thats provided by the find command. The braces { } \; represent the filename from the find command.
This is just one of the options in the find command. There are plenty of others using which a variety of tasks can be accomplished using the most simplest methods.
No comments:
Post a Comment