Link to home
Start Free TrialLog in
Avatar of henrywilson
henrywilsonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ls command - inconsistent results

I am issuing an ls command to list all files in a directory which contain the
string 'AB' in the filename.  

e.g. ls -ltr *AB*

The command returns some results, say, 10 filenames.  

However, when I do an unqualified ls command

e.g ls -ltr

and I manually count the filenames containing 'AB' there are 12 files.

All filenames are in uppercase.  

(And even when I do:-
ls -ltr *ab*
ls -ltr *Ab*
ls -ltr *aB*
I get no results.)

How can that be ?

OS is AIX, shell is ksh.

Help !!
Avatar of jeremycrussell
jeremycrussell
Flag of United States of America image

Do all the files contain characters before and after the characters "AB" in their names?
Avatar of woolmilkporc
Could it be that the missing files are starting with a dot ( . ) e.g. .xyABz ?

wmp
Avatar of henrywilson

ASKER

Yes.  All required files contain characters before and after *AB*.

None contain more than one instance of string "AB" (if that mattered).

No files begin with dot (.).  Indeed all files in the directory begin with the same 4 characters.  e.g. X-XX

It's a mystery !!  But in the context of the system design, it is an important one as it is imperative that the ls command picks up everything.  

Lastly, there are some dashes (-) within the all the filenames, if that might be signficant.
what are the files that appear in one list but not the other?
What do you get with
echo *AB* ?
What does
alias ls
say?
... and what do you get with
/bin/ls *AB*  ?

Woolmilkporc,
Thanks for your suggestions.
echo *AB* doesn't list the file
/bin/ls *AB* doesn't list the file

I've even tried echo, ls and more with entire file name (swiped into the command line from the ls results) andreturn a 'file doesn;t exist'.

Yet I can see it exists when I do an ls with another wildcard file name component e.g. ls *XY*

e.g. file name ins PQ-XY-AB-ZZ

1. ls           the file doesn't appear
2. ls *AB*  the file doesn't appear
3. ls PQ-XY-AB-ZZ  the file doesn't appear
4. ls *XY*  the file appears !!!

Really frustrating.

 
Maybe there is some kind of control character in the file name that doesn't show up?  Try ls -b.
I assume that you use *AB*  in your posts only as an example.

Would you mind posting the "real" thing?

And, if  ls *XY* does show the file, what do you get with

ls *XY* | grep "AB"  ?

If the file shows up now, what is the result of

ls *XY* | grep "AB" | od -tcx  ?

or, if it doesn't appear

echo PQ-XY-AB-ZZ | od -tcx

with the filename obtained via copy-and paste from the ls *XY* output?

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
Tintin

What is the output of

ls -ltb
ls *AB* will not list a file the name of which  contains AB but also contains a space. It should report 2 errors for each such file though. The only way around that which I have ever found is to use find instead of ls.
Can you post 2 things please:

1

The actual string you are keying on if other than AB

2

One of the file names that doesn't work

Your earlier example doesn't fail for me:
19:25:47$ cd tests
20:32:54$ touch PQ-XY-AB-ZZ
20:32:59$ ls *AB*
PQ-XY-AB-ZZ
20:33:19$ 

Open in new window

That's why we need an actual example
@duncan_roe: I can't reproduce here under AIX/ksh what you're describing.

A file xyzABc de is found by ls *AB* without any problem. Or did I get you wrong?

I think using the "-b" flag of "ls" to check for nonprintable characters is the best approach so far.

wmp
wmp you're right - I was thinking of another problem when I wrote that: if you echo or ls or even find and try to pipe the output into xargs, that's when things go pear-shaped with spaces in file names
ozo, you got it right.

There were so many file in the main directory that we missed the sub-directory which contained 'XY' .  The ls obviously extends it's search to such sub-directories where it encounters them.  

Forgetting the fact that there should never have been any such sub-directory there in the first place, is there a way to limit an "ls" search to the current directory only, even if there are sub-directories containing the search string ?

Thanks ozo, and everyone else who responded.  
Try the "-d" flag of ls!
What a simple solution !!. It was masked by large volumes of files in the main directory.  A valuable lesson.  Perhaps it was obvious to AIX/UNIX/LINUX afficionados but it sure stumped this Unix newbie.