Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find files with square brackets in the name

Tags:

regex

linux

find

I'm trying to find some files with square brackets, but I can't seem to get it to work.

My files are named are this:

[ABC] test file.txt

Regexp I'm trying:

find . -iregex '\[abc\].*test.*'

That just doesn't seem to work for some reason. If i replace it with -

find . -iregex '.*abc.*test.*'

-it works fine. So the problem is with the square brackets. Any ideas?

like image 563
Amir Avatar asked Jul 08 '13 13:07

Amir


People also ask

Can File names have brackets?

File names can contain any of the following characters: A-Z, a-z, 0-9, underscore, hyphen, space, period, parenthesis, curly braces, square brackets, tilde, exclamation point, comma, semicolon, apostrophe, at sign, number sign, dollar sign, percent sign, plus sign, and equal sign.

Where can I find square brackets?

Square brackets are used, usually in books and articles, when supplying words that make a quotation clearer or that comment on it, although they were not originally said or written.

What do square brackets mean in grep?

Since the grep process itself has "firefox" in it, grep finds that as well. By adding a [] , we are only searching for the character class "[f]" (which consists of only the letter "f" and is therefor equivalent to just an "f" without the brackets).

What are square brackets used for in a dictionary?

square bracket in British English Brackets (also called parentheses) are used to enclose a word or words which can be left out and still leave a meaningful sentence.


1 Answers

No matching square brackets is not a problem. Problem is matching the file's path. Remember find's output starts with ./ for the current path.

So this regex in your find command will work for you:

find . -iregex '\./\[abc\].*test.*'
like image 118
anubhava Avatar answered Oct 01 '22 08:10

anubhava