Thursday, April 7, 2022

[SOLVED] Bash 'ls' does not accept * wildcard

Issue

I find it strange that when I use the * wildcard , ls sometimes takes it literally. Here's the actual case: I have the following folder structure: This is the output of ls

00  02  04  06  08  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48  99       Codes                    X12
01  03  05  07  09  11  13  15  17  19  21  23  25  27  29  31  33  35  37  39  41  43  45  47  98  ALL_RAW  Implemented_NN_Model.h5

Each of the numbered folders (00 to 99) contains the following files:

00_Binarized_Cutoff_0.99_.tif  00_dot_img_model.png  00_Final_Loc.tif  00_Raw_Prediction.tif  Implemented_NN_Model.h5 

I want to extract all the *_Final_Loc.tif files from these folders.
Hence I tried this: ls -R *Final*tif, and this: ls *Final*tif. I get the same output in both cases:

ls: cannot access '*Final*tif': No such file or directory

I am just curious to know why ls takes the * literally, and what is the correct way?
A side information, I was able to do this task, but when I used this:

ls  [0-9][0-9]/*Final*tif

i.e.,

for file in `ls  [0-9][0-9]/*Final*tif` ; do cp ${file} ../ALL_Final_Loc/ ; done

And this is strange too, since I don't find a logical explanation for why it should work here.


Solution

You have to specify the directory as well like so: ls */*Final*tif

Unfortunately, ls -R doesn't work with wildcards in a particularly useful way so for a case where there are multiple directory levels you could use find, like so: find . -name '*Final*tif'



Answered By - Perry
Answer Checked By - Cary Denson (WPSolving Admin)