Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX 'ls' command - wildcard 'OR' operator

I have a directory with timestamped files in the format:

processAlpha20120618.txt
processAlpha20120619.txt
processAlpha20120620.txt
processBeta20120618.txt
processBeta20120619.txt
processBeta20120620.txt
... etc.

I want a list of these for specific dates. Something like this:

ls -l *201206[19|20|21]*

Obviously the above doesn't work, but you can see what I was trying to achieve. I want to match anything where the string "201206" is followed by either "19", "20" or "21".

I know that this is possible using grep or find, I just wondered if it could be done using ls.

like image 970
Nossidge Avatar asked Jun 22 '12 10:06

Nossidge


People also ask

How do you use wildcard in ls?

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 *. c” was used the shell expanded the * to include all files, but you specified that you only wanted to find all files that ended with the “. c” at the end.

Can ls interpret wildcards?

Wildcards (also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command such as ls command or rm command to list or remove files matching a given criteria, receptively.

What is a wildcard in Unix?

Their UNIX representation are listed for informational purposes only: Asterisk (*) – matches any string, including an empty string. Question mark (?) – matches a single character.

What is the meaning of * [] in ls command?

If ls is being passed an argument called * , it will look for a file or directory called * in the current directory and list it just like any other.


1 Answers

ls does not do this - the shell expands * etc and then passes them to ls as arguments.

Look at the documentation for the shell - it is call globbing

like image 124
Ed Heal Avatar answered Sep 23 '22 03:09

Ed Heal