Wildcards: how do I list files ending in `.txt` only without using the dot character?
Solution 1
If you are using the Bash shell:
shopt -s extglob
ls !(*xtxt|*bak|*bat)
Solution 2
I don't understand why you don't want to use .
. Anyway you can use some character class instead where .
is included, but other chars from your directory not, good candidate for this purpose can be [[:punct:]]
:
LC_ALL=C ls -- *[[:punct:]]txt
I changed locale to C
as character classes depend on that, and added --
to ls
option in order to list all files which start with -
properly.
Definition:
`[:punct:]'
Punctuation characters; in the `C' locale and ASCII character
encoding, this is `! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \
] ^ _ ` { | } ~'.
Solution 3
Use [!x]txt
to list all files ending in txt
but exclude the ones ending in xtxt
:
ls ./*[!x]txt
(you may have to turn off history expansion in your shell if you get an event not found
)
or use ASCII code for dot, e.g.:
ls ./*$'\x2E'txt
or
ls ./*$'\056'txt
Solution 4
Extremely ugly, since this could be achieved using find . -type f -name "*.txt"
easily, but if you don't want to put an .
in there, and the only file extensions you are going to find in that directory are .bat
, .bak
, .txt
, and .xtxt
, then you can try this:
ls *txt | grep -v "xtxt$"
ls *txt
will bring anything ending with txt
, and grep -v "xtxt$"
will prevent to show in the results anything that ends in xtxt
.
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on September 18, 2022Comments
-
Admin almost 2 years
I have a directory with nine files in it.
- 1 ending in
.bat
- 2 ending in
.bak
- 4 ending in
.txt
- 2 ending in
.xtxt
My question is, how do i list only the files ending in
.txt
, but without using the period. So I want to display the 4 ending in.txt
, without using the commandls *.txt
because I don't want to use the period to filter out the files. I'm learning wildcards at the moment so they are used in this. I think I have to use the
!
command somehow but I'm unsure where to use it. - 1 ending in
-
pabouk - Ukraine stay strong almost 6 yearsThe part
LC_ALL=C
does not have the intended effect because it affects only the processls
but the path expansion is performed by the shell where the original value ofLC_ALL
stays unchanged. --- The easiest way of doing what you intended is to start a subshell and set the variable in its scope using a separate assignment command (notice the semicolon):( LC_ALL=C ; ls -- *[[:punct:]]txt )
The subshell ensures that the original state ofLC_ALL
does not change.