Ls help: ls not working with start character search (^)

Experts,

How to list a file using ^ character, for all files started with character a. (os= hp-ux )

# ls -l 
-rw-------   1 useradm    users            0 Mar 26 14:30 abc
-rw-------   1 useradm    users            0 Mar 26 14:30 def
-rw-------   1 useradm    users            0 Mar 26 14:33 zyx
#
Trying but not working, any idea whats wrong, 
# ls -l ^abc
^abc not found

# ls -l ^a*
^a* not found

# ls ^a*
^a* not found

Thanks a lot.

---------- Post updated at 03:09 PM ---------- Previous update was at 02:46 PM ----------

Also can anyone please explain , how the ^ character not working with

ls

, for regex.

Did not work with ls: # Wanted to list which is not having first letter a .

# ls -l [^a]bc
-rw-------   1 useradm   users            0 Mar 26 14:30 abc

[/CODE]

Worked with grep: # Wanted to list which is not having first letter a .

# ls -l | grep "[^a]bc"
-rw-------   1 useradm   users            0 Mar 26 14:48 bbc

Thanks a lot.

Try:

ls -l [!a]bc
1 Like

Important to note, it is not ls that's doing the search here, it's the shell. If you do echo [!a]bc you'll see similar results.

1 Like

You can also use:

ls -l !(a)+(bc)

From KSH manual:

           ?(pattern-list)     Optionally matches any one of the given
                               patterns.

           *(pattern-list)     Matches zero or more occurrences of the given
                               patterns.

           +(pattern-list)     Matches one or more occurrences of the given
                               patterns.

           @(pattern-list)     Matches exactly one of the given patterns.

           !(pattern-list)     Matches anything, except one of the given
                               patterns.
1 Like

The key point that rveri seems to be missing is that the shell uses filename pattern matching (not regular expressions) when expanding the arguments that will be passed to ls. Although there are some similarities between pattern matching and regular expression, many of the details are different.

1 Like

Thanks,Scrutinizer, Corona, Yoda First 3 worked.. to list all files NOT starting with a :

# Worked well: Seems matching is passed to ls command: " ls -l [!a]bc "

# ls -l ab[c]
-rw-------   1 useradm    users            0 Mar 27 12:57 abc


# ls -l [!a]bc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbc

# Worked well: Seems matching is passed to shell.

# echo [!a]bc 
bbc

This is too worked fine, seems matching is done through shell.

# ls -l !(a)+(bc)
-rw-------   1 useradm    users            0 Mar 26 15:27 bbc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcabc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcbbc
# 

Yoda, thanks for the manual page,

Don thanks for pointing out the difference of use when "shell uses filename pattern matching" & when "Regular expression uses pattern matching".
Is there a way we can figure out which is shell doing, and which is doing by regular expressions.

For example with the below example: How to figure out which portion is shell or regex performing the operation:

# ls -l bbcbbc
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcbbc


# ls -l !([c-z])?[c][b-d]
-rw-------   1 useradm    users            0 Mar 26 15:27 bbcbbc


echo $SHELL
/usr/bin/ksh
#OS= hp-ux 11.23

Thanks..

In general, the answer is to read the man pages. But as a high altitude first cut, any unquoted argument passed to the shell that is not a parameter expansion (i.e., $name or ${...name...} that is not part of a shell command lanuage construct (e.g., if , then , else , fi , case , esac , ...) will be subject to pathname expansion.

1 Like

Don, Thanks .. it is much better now.