is not a feature of ls, * is something the shell does.
So when you do ?zabc*.txt in the current directory, the shell will parse that into a list of names to feed into ls -- which happens before ls even runs.
The shell will let you mash them together though. It'll even let you quote any parts of it which don't have *'s and stuff in them:
ls "/My/Path/Is/This/"?zabc*.txt
If all you need is the names, you don't need ls -- remember the shell does it, not ls.
for FILENAME in "/My/Path/Is/This/"?zabc*.txt
do
echo "Got file name $FILENAME"
done
The last issue it seems to be that if I use the pattern abc*.txt I get filenames like 123abc-yue.txt or 01xabc-oie.txt and I would like only those that begins with abc.
How to get only files that begin with "abc"? It could be done modifying the pattern only?
Thanks for the answer. Yes, I'm entering without *abc*.txt. I've tried in cygwin and it works, only in the other system prints
others files that not begin with abc. Maybe an issue with the ls of that system.
Hello Chubler_XL,
Thank you. I didn't use stat before, it works very nice. Only one question.
If I send the command from another directory I need add the path like below, but in the output
appears the filename concatenated with the path (Myfiles/files01/filename):
Using * in that fashion is a property of the shell.
One weird property of Cygwin though -- it has case insensitive filenames, because Windows does, it's unavoidable. Any decent UNIX or UNIX-like has case-sensitive filenames, and will be more picky.
In short, if you don't want to match *abc*, don't use *abc*.
In Cygwin if I use abc*.txt I get exactly only files that begin with abc.
In Linux machine (2010 x86_64 x86_64 x86_64 GNU/Linux) where I have the real files I want to list, when I use abc*.txt I get files that begin with abc and files that contain abc but begins with numbers or other letters (i.e. 17abc-khj.txt, 01xabc-ui.txt). Maybe is the shell like you say.
Please show us your entire script. What shell are you using on Linux? With default options, abc*.txt should only match files with names starting with abc and ending with .txt .
Below is the print containing the Linux version, shell used (bash) and script ( ls -lst abc*.txt ). As you can see there are another file names
that not begin with abc in the list. They are highlighted in red.
bbt@ax-1:~>% uname -a
Linux ax-1 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x8664 x8664 x8664 GNU/Linux
bbt@ax-1:~>%
bbt@ax-1:~>% echo $SHELL
/bin/bash
bbt@ax-1:~>%
bbt@ax-1:/voddf/bbts/ax-abc/files>% ls -lst abc*.txt
4220 -rw-r--r-- 1 bbt voddf 6766 Aug 21 09:19 abc-hjki1.txt
108 -rw-r--r-- 1 bbt voddf 5215 Aug 21 09:19 abc-fgkp1.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 21 09:13 abc-fgkp11.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 21 06:26 abc-hjki11.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 20 19:40 abc-hjki12.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 20 17:22 abc-fgkp12.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 20 13:34 zabc-hjki13.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 20 08:29 0zabc-fgkp13.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 20 07:10 00zabc-hjki14.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 19 19:27 abc-hjki15.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 19 17:00 abc-fgkp14.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 19 13:06 abc-hjki16.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 19 07:36 abc-fgkp15.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 19 06:29 abc-hjki17.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 18 18:33 abc-hjki18.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 18 15:36 abc-fgkp16.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 18 11:53 abc-hjki19.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 18 05:31 abc-fgkp17.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 18 04:53 abc-hjki110.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 17 18:32 abc-hjki111.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 17 15:13 abc-fgkp18.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 17 12:54 abc-hjki112.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 17 06:15 abc-hjki113.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 17 04:42 abc-fgkp19.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 16 19:41 abc-hjki114.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 16 14:50 abc-fgkp110.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 16 13:26 abc-hjki115.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 16 05:47 abc-fgkp111.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 15 16:01 abc-fgkp112.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 15 07:34 abc-fgkp113.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 14 16:36 abc-fgkp114.txt
10260 -rw-r--r-- 1 bbt voddf 1048 Aug 14 07:02 abc-fgkp115.txt
$ touch zabc
$ ls abc*
ls: cannot access abc*: No such file or directory
$
And yes, it is a difference in your shell. The expansion of * happens before ls runs.
BASH is very configurable, it's possible somebody enabled a bizarre option by default. It's also possible for the value of $SHELL to be wrong under some circumstances. Post the output of shopt in that shell, please.
I've change to ksh and the output of the same ls command prints correctly only files that begin with abc. It seems is an issue with bash shell like you said.
Below is shopt print in bash shell.
bbt@ax-1:/voddf/bbts/ax-abc/files>% shopt
cdable_vars off
cdspell off
checkhash off
checkwinsize on
cmdhist on
dotglob off
execfail off
expand_aliases on
extdebug off
extglob off
extquote on
failglob off
force_fignore on
gnu_errfmt off
histappend off
histreedit off
histverify off
hostcomplete on
huponexit off
interactive_comments on
lithist off
login_shell on
mailwarn off
no_empty_cmd_completion off
nocaseglob off
nocasematch off
nullglob off
progcomp on
promptvars on
restricted_shell off
shift_verbose off
sourcepath on
xpg_echo off
BASH doesn't usually do this. Have you customized your ~/.login or ~/.bashrc or ~/.bash_rc etc files at all? What's the value of IFS in BASH? printf "%s" "$IFS" | hexdump -C
Or there could be something strange like programmable completion gone awry happening.
Those look normal enough. I'm not sure what's happening here.