Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wild cards on find and ls

Tags:

linux

bash

I'm trying to figure out the wild-cards to do file operations.

I have these files in a directory for testing purposes:

file_BSD.GIF  file_linux.gif  file_unix 

See my ls command,

$ ls *{.GIF,.gif} file_BSD.GIF  file_linux.gif 

Which is OK.

But "find" doesn't seem to work the same way:

$ find -name *{.GIF,.gif} find: paths must precede expression: file_linux.gif Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression] 

By the way, I've read that "-iname" should locate both the uppercase and lowercase files, but that doesn't seem to work either:

$find -iname *.gif  ./file_linux.gif 

(This should locate the .GIF file as well, right?).

like image 910
Juan Pablo Barrios Avatar asked Nov 16 '12 20:11

Juan Pablo Barrios


People also ask

Can ls interpret wildcards?

You can use them with any command such as ls command or rm command to list or remove files matching a given criteria, receptively. These wildcards are interpreted by the shell and the results are returned to the command you run.

How do wildcards work with ls?

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 *.

How do you use wildcards to search files and folders?

You can use your own wildcards to limit search results. You can use a question mark (?) as a wildcard for a single character and an asterisk (*) as a wildcard for any number of characters. For example, *. pdf would return only files with the PDF extension.

What is a wild card in a search?

Wildcards take the place of one or more characters in a search term. A question mark (?) is used for single character searching. An asterisk (*) is used for multiple character searching. Wildcards are used to search for alternate spellings and variations on a root word.


1 Answers

find -name *{.GIF,.gif} is wrong.

This command is first expanded by the shell to find -name *.GIF *.gif

Then further expanded to :

find -name file_BSD.GIF  file_linux.gif  # as you have only these files in directory 

Now this -name file_BSD.GIF file_linux.gif is passed to find. And this is wrong as there is no switch like file_linux.gif that is accepted by find.

What you need is this command.

find -name '*.GIF' -or -name '*.gif' 

Assuming you want to collect .gif files in a case insensitive manner, this find command becomes,

find -iname '*.gif' 

Note the single quotes (') here. It means *.GIF should be sent to find as is without any shell expansion. And find will use this as pattern. This single quote is necessary unless you escape the shell meta-characters. In that case the command would look like

find -iname \*.gif 
like image 115
Shiplu Mokaddim Avatar answered Sep 21 '22 13:09

Shiplu Mokaddim