LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-18-2017, 12:27 PM   #1
ainulindale
LQ Newbie
 
Registered: Sep 2017
Distribution: Mint, Kali
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 09-18-2017, 01:02 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,655
Blog Entries: 3

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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.
 
1 members found this post helpful.
Old 09-18-2017, 01:07 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
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.
 
1 members found this post helpful.
Old 09-18-2017, 01:08 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,655
Blog Entries: 3

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
variable expansion and pathname expansion ShadeLover Linux - General 6 04-22-2015 10:56 PM
Bash: ${} shamelessly saved from pathname expansion romagnolo Linux - General 2 02-20-2012 04:25 PM
[SOLVED] To find a pathname to any given file abhishekgit Linux - Newbie 8 02-01-2012 02:55 PM
bash: strange problem with pathname expansion frankie_DJ Programming 2 01-14-2007 07:16 PM
Scripting help -- pathname expansion player_2 Programming 3 08-25-2003 05:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration