Home > OS >  How do I use the logical "or" with a Linux wildcard?
How do I use the logical "or" with a Linux wildcard?

Time:06-16

everyone,

I am currently trying to remove files that start with a "U", end in ".txt" or a number. Thus far, I have used this command to find those files:

 rm *[U-.txt||0-9]* 

However, the command shows that there are no files matching that description.

ls: cannot access '*[U-.txt': No such file or directory

0-9]: command not found.

Apparently, the terminal zsh is looking for a particular file that starts with U, and then looks at all other characters that follow "U", and stops at the .pdf ending. However, when I put the logical "or" within that range, I get an error message. The error message says that there are no files that match that description. There are 6 files that have numbers beginning with a number, but the terminal is either not understanding what I am trying to do, or is saying that there aren't any files that begin with "U", end in .pdf and have a number.

CodePudding user response:

In zsh, that's (with the KSH_GLOB option set)

rm U*@(.txt|[0-9])

The pattern

  • starts with U
  • continues with arbitrary characters
  • ends with either .txt or a digit.

You could also use [:digit:] in place of [0-9].

CodePudding user response:

The normal way to write a wildcard pattern to match what you want in zsh is U*([0-9]|.txt). This should Just Work, without needing any extra options set.

$ ls
Ubaz Ufoo3  Ufoobar.txt  Xfoo8  Xfoobar.txt
$ ls U*([0-9]|.txt)
Ufoo3 Ufoobar.txt
  • Related