How to use ls with pattern and including path?

Hello to all,

Maybe someone could help me, my question is:

How can a filter the print of command ls for the files with names of the form "abc*.txt" including the path?

I've done this:

If I move with command cd to /My/Path/Is/This/ and send this command:

ls -lst abc*.txt -i ?zabc*.txt

I get a filtered list containing files with names only with pattern abc*.txt and it seems to work fine.

But if I try to use the same command from another directory and including the path, the filtering is not working.

I've tried like this:

ls -lst abc*.txt -i ?zabc*.txt /My/Path/Is/This/

and like this and doesn't apply the filter neither

ls /My/Path/Is/This/ -lst abc*.txt -i ?zabc*.txt

Thanks for any help.

  • 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

Hello Corona688,
Thanks for your help. Actually I need the filename, date, hour, size etc, like what "ls -lt" gives.
I've modified your code to:

for FILENAME in "/My/Path/Is/This/"?zabc*.txt
do
        ls -lt "$FILENAME"
done

The only thing is that is printing the file with the path like this:

-rw-r--r-- 1 ri xyz  1048 Aug 11 05:15 /My/Path/Is/This/abc-dfe.txt

How to print the file without the path?, like this:

-rw-r--r-- 1 ri xyz  1048 Aug 11 05:15 abc-dfe.txt

Thanks again.

Best regards

You don't need the for loop if you're feeding it into ls, use my other example.

cd /path/to/folder ; ls *.whatever

Thank you Corona,

It works like this:

cd /path/to/folder ; ls *.whatever; cd-

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.

How to get only files that begin with "abc"?

Thanks again

You don't, unless you want to put the output through a text filter to get rid of it.

Thank you Corona,

It works like this:

cd /path/to/folder ; ls *.whatever; cd-

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 again

Are you sure you are not using *abc*.txt ?
abc*.txt WILL all start with abc...

Manually using CygWin...

AMIGA:~> cd /tmp
AMIGA:/tmp> ls st*
stackit.sh  string_shift.sh  strings.sh
AMIGA:/tmp> ls *st*
kbstore  stackit.sh  string_shift.sh  strings.sh  sub_str.sh
AMIGA:/tmp> _

Show us a copy'n'paste of what you input and what results you see...

You man find stat a little easier to get file attributes than parsing the ls -l output:

$ stat -c "size: %s mod-epoch: %Y file: %n" I*
size: 182412 mod-epoch: 1392406500 file: Invoice 266.pdf
size: 185593 mod-epoch: 1392407640 file: Invoice 267.pdf
size: 183923 mod-epoch: 1393205880 file: Invoice 268.pdf

Hello wisecraker,

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):

$ stat -c "size: %s mod-epoch: %Y file: %n" Myfiles/files01/abc*
size: 5 mod-epoch: 1408573510 file: Myfiles/files01/abc-bmns1.dat
size: 5 mod-epoch: 1408573510 file: Myfiles/files01/abc-bmns11.dat
size: 5 mod-epoch: 1408573510 file: Myfiles/files01/abc-bmns113.dat
size: 5 mod-epoch: 1408573510 file: Myfiles/files01/abc-bmns114.dat
size: 5 mod-epoch: 1408573510 file: Myfiles/files01/abc-bmns115.dat

How to print the only the filenames without the path?

Like this:

size: 5 mod-epoch: 1408573510 file: abc-bmns1.dat
size: 5 mod-epoch: 1408573510 file: abc-bmns11.dat
size: 5 mod-epoch: 1408573510 file: abc-bmns113.dat
size: 5 mod-epoch: 1408573510 file: abc-bmns114.dat
size: 5 mod-epoch: 1408573510 file: abc-bmns115.dat

Thanks again.

Regards

The same as with ls: cd into the folder first:

$ cd Myfiles/files01/
$ stat -c "size: %s mod-epoch: %Y file: %n" abc*

I repeat:

It has NOTHING to do with ls.

Absolutely nothing whatsoever.

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*.

Thank you Chubler_XL for your help!

Hello Corona688,

  • 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.

Thanks again

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 .

Hello Don,

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 

Thanks for the help.

That is very strange.

$ 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.

Hello Corona688,

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          

Thanks for the help.

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.

I haven't change anything, this is an equipment that they gave me access to some folders only with a generic user.

The output of IFS is:

 printf "%s" "$IFS" | hexdu mp -C
00000000  20 09 0a                                          | ..|
00000003                                                         

By the way, what is IFS?

Regards

The characters the shell uses to separate input fields are listed in IFS (by default space, tab, and newline; which is what you see in your output).

Maybe we have some backspaces in some of these filenames. Please post the output from the command:

printf "%s\n" *00z* *113*abc*.txt abc*113*.txt|od -cb