Solved

ls command - inconsistent results

Posted on 2010-11-23
18
1,191 Views
Last Modified: 2012-05-10
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
Comment
Question by:henrywilson
  • 6
  • 4
  • 3
  • +4
18 Comments
 
LVL 9

Expert Comment

by:jeremycrussell
ID: 34200656
Do all the files contain characters before and after the characters "AB" in their names?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34200666
Could it be that the missing files are starting with a dot ( . ) e.g. .xyABz ?

wmp
0
 

Author Comment

by:henrywilson
ID: 34200797
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
 
LVL 84

Expert Comment

by:ozo
ID: 34200896
what are the files that appear in one list but not the other?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34200944
What do you get with
echo *AB* ?
What does
alias ls
say?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34201016
... and what do you get with
/bin/ls *AB*  ?

0
 

Author Comment

by:henrywilson
ID: 34201443
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
 
LVL 13

Expert Comment

by:Superdave
ID: 34201522
Maybe there is some kind of control character in the file name that doesn't show up?  Try ls -b.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34201533
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 34201953
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
 
LVL 48

Expert Comment

by:Tintin
ID: 34202169
What is the output of

ls -ltb
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 34203861
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
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 34203912
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34203914
@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
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 34203963
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
 

Author Comment

by:henrywilson
ID: 34209048
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
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34209091
Try the "-d" flag of ls!
0
 

Author Closing Comment

by:henrywilson
ID: 34209098
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now