Searching for file name or file content in Linux

To search for a file by its name, follow the example below.

[username@machine ~]$ find / -name 'myLostFile*.txt'


Note that “find” is the command we wish to run. The “/” denotes where to start looking. We began looking from the root “/” here; if you know a more specific path where the file is, you can narrow it down by providing a path, such as “/home/username/” or “.”. The “-name” specifies we are searching by file name.

By adding “-ls” at the end, we can return more details on the matched files. Example below:

[username@machine ~]$ find . -name 'myLostFile*.txt' -ls

13795398 2310924 -rw-r--r--   1 username  username 2364071936 Feb 19 14:08 ./myLostFile_Feb2009.txt
13795396 59012 -rw-r--r--   1 username  username 60362752 Mar 10 08:29 ./myLostFile_Mar2009.txt

At times, we may need to search through the content of the files as well. To do so, follow the example below:

[username@machine ~]$ find . -type f -exec grep -H "" {} ;

./myLostFile_Mar2009.txt:To do - write an article on Linux find command for

We will get the file path and name in the first portion (before the colon symbol) of each output line. The particular line of that matched our search criteria will be displayed in the second portion. If multiple lines within the same file matches and/or if matches are found across multiple files, more than one output line will be displayed.

Leave a Reply

Your email address will not be published. Required fields are marked *