how to search content in files – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Tue, 20 Jul 2021 06:00:14 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 find Command in Linux (Search Files and Directories) https://tecadmin.net/linux-find-command-with-examples/ https://tecadmin.net/linux-find-command-with-examples/#respond Thu, 21 Mar 2013 06:15:30 +0000 https://tecadmin.net/?p=123 The “find” command is present in most Unix-like operating systems. It is used to locate and manipulate files and directories based on certain criteria, i.e., find files/directories based on date, size, groups, file types, and other criteria. It will look for files/directories in the current directory unless specified otherwise. The “find” is recursive, which means [...]

The post find Command in Linux (Search Files and Directories) appeared first on TecAdmin.

]]>
The “find” command is present in most Unix-like operating systems. It is used to locate and manipulate files and directories based on certain criteria, i.e., find files/directories based on date, size, groups, file types, and other criteria. It will look for files/directories in the current directory unless specified otherwise. The “find” is recursive, which means that it searches for files in the specified directory as well as the subdirectories of the specified directory. It can even be combined with some options to perform specific actions.

This post will focus on how to use the “find” command to locate and manipulate files and directories in your system. Moreover, we will also learn how to combine options with the “find” command to perform several actions.

How to locate files by name using the find command

First, we will use the “find” command to search files by their names; This is the most common use of the “find” command.

There are two approaches to search a file by its name using the find command:

  • In the current working directory (without providing any path)
  • In a custom directory

How to search files by name in the current directory

To find a file by name in the current directory, use the following command:

find -name test-file.txt

In this example, we are searching for a file named “test-file.txt” located in the current directory:

Or:

find -name test-file2.php

How to search files in a custom directory

As mentioned above, we can also use the find command to find a file by name in a custom directory. You can use the below-given command to do that:

find /home/rahul/Documents -name test-file.txt

The command given above searches for a file named “test-file.txt” in the “/home/rahul/Documents” directory.

The “find” command is case-sensitive and will only look for a file with an exact name. You can use the “-iname” option to make it case insensitive:

find /home/rahul/Documents -iname TEST-FILE.txt

How to find files by their extension

The “find” command can also be used to search for files with a specific extension. Just use the “*” along with the name of an extension to find all the files that have that extension:

find /home/rahul/Documents -name '*.txt'

The command is given above only looks for files with the “.txt” extension in the “/home/rahul/Documents” directory.

The given-below command looks for all the files that have the “.jpg” extension.

find /home/rahul/Documents -name '*.jpg'

How to find files by their type

The “find” command also gives us the option to search for a specific file type. To do that, combine the “find” with the “-type” option along with a flag to specify the file type.

List of commonly used flags:

  • f: a regular file
  • c: character devices
  • d: directory
  • b: block devices
  • s: socket
  • l: symbolic link

The following command is searching for directories in the /home/rahul/Documents directory using the d flag:

find /home/rahul/Documents -type d

And to search for files in the /home/rahul/Documents directory, use the f flag:

find /home/rahul/Documents -type f

In case you forget the name of a file, just execute the given command below:

find /home/rahul/Documents -name "test*"

The above-mentioned command will output all the files starting with “test”.

To find the required file, just replace test with the file you want to find.

How to search a file in multiple directories
You can specify multiple directories along with the find command to find the file in those directories:

find /home/rahul/Documents /home/rahul/Downloads -name test-file.txt -type f

How to search a file by size

You can also search for a file by specifying its size. You can use:

  • c: is for bytes
  • k: is for kilobytes
  • M: is for Megabytes
  • G: is for Gigabytes

For example, to search files larger than a certain size, specify the size of the file along with the + operator as shown in the command below:

find -size +50c

Similarly, to search files smaller than a certain size, specify the size of the file along with the – operator:

find -size -50c

If you remember the exact size of the file, you can also search for it by specifying its size without any operator:

find -size 71c

Replace “71c” with the actual size of your file.

How to search empty files

To find all the empty files in a certain directory, use the following command:

find /home/rahul/Documents -type f -empty

How to search empty directories

To find all the empty directories in a certain directory, use the following command:

find /home/rahul/Documents -type d -empty

How to find and remove files

To find and remove a single file, use the following command:

find -type f -name "test-Img2.jpg" -exec rm -f {} \;

Remember to replace the file name.

Conclusion

The “find” command is used to locate and modify files and directories depending upon the specified parameters. It makes the files more accessible and helps the user find files and directories just by running a single command from the terminal.

This command is regularly used by Linux users and is one of the most popular commands. It is very convenient and proves to be very helpful for the user.

In this write-up, we touched on what the “find” command is and how we can use it to locate files and directories. Moreover, we also learned how we can combine the “find” command with other options to perform specific tasks.

The post find Command in Linux (Search Files and Directories) appeared first on TecAdmin.

]]>
https://tecadmin.net/linux-find-command-with-examples/feed/ 0