Find command to find a word from list of files

I need to find a word '% Retail by State' in the folder /usr/sas/reports/RetailSalesTaxallocation.

When I tried like below,

-bash-4.1$ cd /usr/sas/reports/RetailSalesTaxallocation
-bash-4.1$ find ./ -name % Retail by State
find: paths must precede expression: Retail
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
-bash-4.1$ 

I could not succeed. Could someone guide me? I need to know file names which has the word '% Retail by State'

Please be informed that there are many files under the folder /usr/sas/reports/RetailSalesTaxallocation.

find ./ -name '*% Retail by State*'

Hello Ram,

If I understood correctly you need to search a word % Retail by State in all files under path /usr/sas/reports/RetailSalesTaxallocation . Following may help you in same.

 find /usr/sas/reports/RetailSalesTaxallocation -type f -exec grep -l "%Retail by State" {} \+ 2>/dev/null
 

Thanks,
R. Singh

1 Like

Your understanding is right. I went to this folder /usr/sas/reports/RetailSalesTaxallocation and ran ,

find /usr/sas/reports/RetailSalesTaxallocation -type f -exec grep -l "%Retail by State" {} \+ 2>/dev/null

I'm not getting any output. Does this means none of the file have the word I'm searching for?

May I request you to explain the command as I've trouble understanding this command.

Hello Ram,

Let me explain you completely here about the code shown above.
Let's say we have following example files.

cat ./test34233
% Retail by State

cat ./test34232
% Retail by State 213121

Now When I run following command it will give these 2 files as follows.

find . -type f -exec grep -l "% Retail by State" {} \+ 2>/dev/null
./test34233
./test34232

Also when I run following command too it will give results as follows.

find . -type f -exec grep -l "Retail by State" {} \+ 2>/dev/null
./test34233
./test34232

Let me explain you the command here.

find /usr/sas/reports/RetailSalesTaxallocation -type f #### Means in Path /usr/sas/reports/RetailSalesTaxallocation  look for files.
  
-exec grep -l                                                           #### Perform grep operation and search word "% Retail by State"
2>/dev/null                                                            #### Put standard errors in /dev/null (don't show on screen).
  
Hope this helps you.

Thanks,
R. Singh

1 Like

Why not just

grep "% Retail by State" /usr/sas/reports/RetailSalesTaxallocation/*
1 Like

Thanks a ton Ravinder for your detailed explanation. Much appreciated.

1 Like

In similar fashion, how to search for a file from the list of folder.

I need to find the file sync_dailyscheduler_data.sh from the folder /usr/?

This file may be available any where (/usr/sas or /usr/sas/tir or /usr/sas/tir/test/) after the main folder /usr.

Hello Ram,

Following may help you in same.

find /usr -type f -name "sync_dailyscheduler_data.sh" 2>/dev/null

Where -type f means it will look for only files, -name means it will look for the file name proceeding to it, 2>/dev/null means put any standard errors to /dev/null more specifically don't show them on standard output. You can go through man find page too to read more nice options for find command, hope this helps.

Thanks,
R. Singh

1 Like

How to explicitly define a folder in your find command to search for files?

Where your command will search for the file?

Hello Ram,

Following may help you in same.

search one /usr directory:
--------------------
find /usr -type f -name "sync_dailyscheduler_data.sh" 2>/dev/null
  
search multiple dirs. say /opt, /usr and /var:
--------------------
find /opt /usr /var -type f -name "sync_dailyscheduler_data.sh" 2>/dev/null
 

Thanks,
R. Singh