LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pathname Expansion with the File program? (https://www.linuxquestions.org/questions/linux-newbie-8/pathname-expansion-with-the-file-program-4175614048/)

ainulindale 09-18-2017 12:27 PM

Pathname Expansion with the File program?
 
Hi, I'm currently reading The Linux Command Line and came across this:

find ~ -type f -name "*.JPG" -size +1M | wc -l

The author noted that he put the quotations around *.JPG to suppress pathname expansion, but why? I thought using the double quotes would eliminate the special meaning of the asterisk. Since the first part of the above line is looking for regular files in the home directory that end with .JPG, I thought we would want to retain the expansion.

Turbocapitalist 09-18-2017 01:02 PM

If you leave off the quotes, then the asterisk will get processed by the shell and the results passed to find. See "man 7 glob" about that.

If you use single or double quotes, then the asterisk will get processed by find as a parameter. That's what you want.

MensaWater 09-18-2017 01:07 PM

If you did *.JPG it would look for any of your current file names and try to append .JPG to them wether they had that or not. The "*.JPG" is telling find to look for any file ending in .JPG so is making it more clear to find itself.

So say you had a directory with files:
ralph
billybob
suellen.JPG
david
missysue.JPG

Standard expansion of *.JPG would look for ralph.JPG, billybob.JPG, suellen.JPG.JPG, david.JPG and missysue.JPG.JPG and find none of those because they don't exist. On the other hand, by telling find to use "*.JPG" it is saying only find those files that have .JPG at end of name so will only show you suellen.JPG & missysue.JPG.

It often occurs with scripting that expansion is happening at odd locations and you have to do special quoting and/or escaping to make sure it only occurs where you meant it to do so.

Turbocapitalist 09-18-2017 01:08 PM

Here's something to compare the difference:

Code:

cd ~/Pictures/

for i in "*.JPG"; do echo "I=$i"; done

for i in *.JPG;  do echo "I=$i"; done

The one with the quotes is taken literally and passed to for as-is. The one without the quotes is interpreted by the shell and then the results passed to for.


All times are GMT -5. The time now is 06:33 AM.