`find`

Not only you should be able to find files with specific string but also find based on type, empty, size, permission and more..

Once we find the files we should be able to execute linux commands on each and every result we got.

Following is the sample of find command explains what it can do.

find . -name “*.txt” -empty -type f -exec rm -rf {} \;

Explanition of above command

`find`     -> command

`.`            -> current directory, which means it is going to look in current directory and all subdirectories.

`-name`  -> mention the search specifications(you can write meta charectors also regular experssion) which you want look for, here look for all `.txt` files (*.txt)

`-empty` -> looking for only empty files which means it will ignore all “*.txt” files which contain any text in them.

`-type`      -> looking for only files(-f) but not folders, even though above specification(*.txt) narrow our search pretty much to files but it still good practice to mention type as well if are planning to search for only files but not folders with the search specification.

-exec -> this option will help you to execute any commands on each result of command till `-type -f`, you need to follow specific format to make sure it runs properly which is (-exec `any command` {} \), here in above example we have used remove command. In this format flower bracket kind of represent object which will store the result from the command till `type- f` and run the command for each result of the object.

 

Leave a Reply

Your email address will not be published.