Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression usage with ls

Tags:

regex

ls

glob

I am trying to use ER (Extended Regular Expressions) with ls like ls .+\..+.

I am trying to print all files which contains an extension (I know I could have used ls *.*, but I wanted to try using ER).

When I run that code I get this error: ls: .+..+: No such file or directory.

like image 706
Lucas Rezende Avatar asked Mar 11 '13 18:03

Lucas Rezende


People also ask

Does LS support regular expression?

You don't say what shell you are using, but they generally don't support regular expressions that way, although there are common *nix CLI tools ( grep , sed , etc) that do.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you grep a regular expression in Linux?

GNU grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp ) option.

How do you use LS wildcards?

One of the most used wildcards is the star or asterisk wildcard “*”. This wildcard is used to represent any character, or even no characters at all! Instead of listing all the files in the directory with “ls”, when the command “ls *.


1 Answers

You are confusing regular expression with shell globbing. If you want to use regular expression to match file names you could do:

$ ls | egrep '.+\..+' 
like image 187
Chris Seymour Avatar answered Oct 19 '22 22:10

Chris Seymour