LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   printf Or clause from find issue (http://www.linuxquestions.org/questions/programming-9/printf-or-clause-from-find-issue-923461/)

thund3rstruck 01-12-2012 09:58 AM

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?

thund3rstruck 01-12-2012 10:02 AM

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;


David the H. 01-12-2012 10:05 AM

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.

thund3rstruck 01-12-2012 11:33 AM

Quote:

Originally Posted by David the H. (Post 4572872)
Although as I said, you can use grouping rather than two separate actions.

Excellent explanation and share! Thanks! Just what I needed.


All times are GMT -5. The time now is 01:50 AM.