• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1213
  • Last Modified:

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 !!
0
henrywilson
Asked:
henrywilson
  • 6
  • 4
  • 3
  • +4
1 Solution
 
jeremycrussellCommented:
Do all the files contain characters before and after the characters "AB" in their names?
0
 
woolmilkporcCommented:
Could it be that the missing files are starting with a dot ( . ) e.g. .xyABz ?

wmp
0
 
henrywilsonAuthor Commented:
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.
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
ozoCommented:
what are the files that appear in one list but not the other?
0
 
woolmilkporcCommented:
What do you get with
echo *AB* ?
What does
alias ls
say?
0
 
woolmilkporcCommented:
... and what do you get with
/bin/ls *AB*  ?

0
 
henrywilsonAuthor Commented:
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.

 
0
 
SuperdaveCommented:
Maybe there is some kind of control character in the file name that doesn't show up?  Try ls -b.
0
 
woolmilkporcCommented:
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?

0
 
ozoCommented:
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 !!!
This could occur if the file is in a subdirectory like
XY/PQ-XY-AB-ZZ
It may also occur if there is an unseen character in between the A and B
Could you show the actual files?
0
 
TintinCommented:
What is the output of

ls -ltb
0
 
Duncan RoeSoftware DeveloperCommented:
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

0
 
Duncan RoeSoftware DeveloperCommented:
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
0
 
woolmilkporcCommented:
@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
0
 
Duncan RoeSoftware DeveloperCommented:
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
0
 
henrywilsonAuthor Commented:
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.  
0
 
woolmilkporcCommented:
Try the "-d" flag of ls!
0
 
henrywilsonAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now