henrywilson
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 !!
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 !!
Do all the files contain characters before and after the characters "AB" in their names?
Could it be that the missing files are starting with a dot ( . ) e.g. .xyABz ?
wmp
wmp
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.
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?
echo *AB* ?
What does
alias ls
say?
... and what do you get with
/bin/ls *AB* ?
/bin/ls *AB* ?
ASKER
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.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What is the output of
ls -ltb
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:
Can you post 2 things please:
1
The actual string you are keying on if other than AB2
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$
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
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
ASKER
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.
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!
ASKER
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.