LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-12-2012, 09:58 AM   #1
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 346

Rep: Reputation: 38
Question printf Or clause from find issue


I'm sure this is trivial but I've got a bizarre issue when using the printf construct. If I use the -or filter in a find statement with printf then the original filter in the or is ignored and I don't understand why this is.

for example the following command works fine:
Code:
[lisa@LISAXPS410FEDORA ~]$ find ~/Downloads/gpodder/ -name *.mp4 -or -name *.mp3
~/Downloads/gpodder/EpicBattleAxe  Podcasts/taf-057.mp3
~/Downloads/gpodder/EpicBattleAxe  Podcasts/EpicBattleAxe-ebc141147.mp4
~/Downloads/gpodder/EpicBattleAxe  Podcasts/taf-056.mp3
These paths have spaces so I need to format the output of find so they are not seperated by newlines. I do this by specifying the -printf %p\; parameter.

However, when I introduce this logic, the find ignores the -or filter:

Code:
[lisa@LISAXPS410FEDORA ~]$ find ~/Downloads/gpodder/ -name *.mp4 -or -name *.mp3 -printf %p\;
~/Downloads/gpodder/EpicBattleAxe  Podcasts/taf-057.mp3;~/Downloads/gpodder/EpicBattleAxe  Podcasts/taf-056.mp3;
As you can see the -name *.mp4 filter is suddenly being ignored. I've tried this several times with several -or filters and they all seem to be ignored other than the last one.

Can anyome explain what I'm screwing up here?
 
Old 01-12-2012, 10:02 AM   #2
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 346

Original Poster
Rep: Reputation: 38
Ok, I need to format the output for each filter:

Code:
[lisa@LISAXPS410FEDORA ~]$ find ~/Downloads/gpodder/ -name *.mp4 -printf %p\; -or -name *.mp3 -printf %p\;
~/Downloads/gpodder/EpicBattleAxe  Podcasts/taf-057.mp3;~/Downloads/gpodder/EpicBattleAxe  Podcasts/EpicBattleAxe-ebc141147.mp4;~/Downloads/gpodder/EpicBattleAxe  Podcasts/taf-056.mp3;
 
Old 01-12-2012, 10:05 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
See the final section at this link.

http://mywiki.wooledge.org/UsingFind

Short form, adding an explicit action to the command makes it apply only to the search expression it's paired with. So your command becomes "find .mp4 (and do nothing) or find .mp3 and printf" You need to group the entire search with parentheses to force the action to apply to both.

Edit: I see you figured it out for yourself. Good job. Although as I said, you can use grouping rather than two separate actions.

Code:
find ~/Downloads/gpodder/ \( -name "*.mp4" -or -name "*.mp3" \) -printf %p\;
Edit2: One more very important thing. You MUST protect find's "*" globbing patterns with quotes, otherwise the shell does its globbing expansion first, and if there are any matching filenames in the current working directory, you're going to get a broken command.

Last edited by David the H.; 01-12-2012 at 10:15 AM. Reason: as posted
 
1 members found this post helpful.
Old 01-12-2012, 11:33 AM   #4
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 346

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by David the H. View Post
Although as I said, you can use grouping rather than two separate actions.
Excellent explanation and share! Thanks! Just what I needed.
 
  


Reply

Tags
bash scripting, find, printf


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
[SOLVED] [bash] find printf seconds hashbang#! Programming 4 01-14-2011 08:17 PM
[SOLVED] printf white space issue word splitting dukedog Linux - Newbie 2 06-15-2010 01:46 PM
[SOLVED] why I couldn't find the source code of printf function in glibc source? famsinyi Programming 5 09-21-2009 09:06 AM
Changes in Latest Core Utils find/printf Command? Woodsman Slackware 4 08-28-2009 04:52 PM
[Bash] If clause netpumber Programming 2 04-10-2009 06:06 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration