How to use find command utilities in the Linux Part-2

Introduction 

In second part of this blog of find utilities we will learn how to perform multiple operations can be performed, So lets begin and start learning more about find command utility.

Find with Name or Extension

The name attribute defines the content of search. The find utility is going to the specified location for whatever string of characters has entered. Typically the name of the file we are looking at the end of the command, after that location, options, and expressions.

Standard letters and numerals are entered as a string of characters at the end of the command.

sudo find . –type f –name ‘*.html’

Special characters – a wildcard, a period, a slash, etc. – require the use of single quotes around the name. Single quotes ensure that the system interprets the request correctly. We can easily find the with the use of files extensions such as .html or .php.

 

4

 

Perform Actions with find Command

We have seen how to use the find command to display search results in terminal window. An action attribute can enhance that functionality and perform a particular task once a file is located.

The following command searches the actual content of files (instead of just the names of files):

 sudo find ~ –type f –exec grep “contents” ‘{}’ ; –print

  1. We can define to find a file (-type f) in our user’s home directory (~).

  2. We can also perform action (-exec) by running the grep command to search through the contents of all the files for the word contents.

  3. The {} brackets show the match results of the find command, and with the use of single quotes so that the grep command doesn’t misinterpret them.

  4. We’re closing out the grep command with a semicolon (;), and we’re using a backslash () so that the system doesn’t misinterpret the semicolon as an instruction.

  5. Finally, the print option tells the system to display the results.

With the use of the mention command into the terminal and substitute of search path for the tilt ~ character and your search term of the content of word file. we can use  find –exec combination to change file permissions quickly:

 sudo find /path –name “filename.ext” –exec chmod 777 ‘{}’ ;

This command uses the exec command to run chmod to change the file permissions of the files and also use name with extension filename.ext so that it can be read, written, and executed by everyone.

How to locate and also delete files with find utility

Uses of the -delete command we can rapidly find and remove single or multiple files in a single commands.

Note: Once files will delete with the use of find utility then it will be unrecoverable so be aware before perform or use of this function.

sudo find . –name “*.html” –delete

Use of the above command will search all .html all files and delete.

 

5

 

This type of action is useful for removing old backup files if we have substituted “*.bak” instead of “*.bmp”.

Use of Add option

Options are special functions inserted before the search path in P, L, H are optional elements used to control the handle the soft links.

  • P This option represents the default behavior. The system does not use soft links.

  • -H The system does not use symbolic links except while processing the specified command.

  • -L Follows symbolic links. The information is taken from the file to which the link directs to. Not for the link itself.

Conclusion:

The find command in Linux can help to keep track of files on a shared system, locate the missing files or files which are accidentally moved.

Leave a Reply

Your email address will not be published.