LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-09-2007, 02:39 PM   #1
shamgar03
Member
 
Registered: Jul 2004
Distribution: Gentoo, CentOS
Posts: 92

Rep: Reputation: 15
unix find utility


I use the 'find' command alot, but I always get stuck on the fact that I can't get the pipe operator to work in the -exec option. Here is what I want to do

find -name -exec echo {} | grep -o whatever \;

This isn't actually what I am doing, but an example. So basically whenever I do this everything after the pipe just gets ignored. I would like each found file to be properly piped into grep -o.

\| and '|' and "|" don't work. Is there an escape sequence to get the pipe to work properly there?
 
Old 08-09-2007, 02:43 PM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
hi there,
Quote:
Originally Posted by shamgar03
I use the 'find' command alot, but I always get stuck on the fact that I can't get the pipe operator to work in the -exec option. Here is what I want to do

find -name -exec echo {} | grep -o whatever \;

This isn't actually what I am doing, but an example. So basically whenever I do this everything after the pipe just gets ignored. I would like each found file to be properly piped into grep -o.

\| and '|' and "|" don't work. Is there an escape sequence to get the pipe to work properly there?
i normally use a -print combined with xargs in this case.
something like this...
Code:
find -name -print $something | xargs echo .... | ...
should work...
regards,
slackie1000
 
Old 08-09-2007, 02:45 PM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
find -name -exec grep -o whatever {} \;

but I don't think that's what you mean. However

find -name | xargs grep whatever

should also work.
 
Old 08-09-2007, 02:49 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by shamgar03
Here is what I want to do

find -name -exec echo {} | grep -o whatever \;
Isn't this functionally equivalent to:
Code:
find . -name '*whatever*' | grep -o whatever
edit: That's a bit strange to be doing anyway. All you'll get is a list of "whatever"s.

edit2: You didn't specify a directory in yours.. I added one to mine above.

Last edited by anomie; 08-09-2007 at 02:54 PM.
 
Old 08-09-2007, 03:05 PM   #5
shamgar03
Member
 
Registered: Jul 2004
Distribution: Gentoo, CentOS
Posts: 92

Original Poster
Rep: Reputation: 15
more specifically

lets say I want to change the extensions on a group of files, say a bunch of jpegs from jpg to jpeg. So I cd to the directory and then I would like to do:

Code:
find . -type d -maxdepth 1 -mindepth 1 -exec cp -v {} "$(echo {} | grep -o '*.\.')" \;
that of course doesn't work because stuff after the | gets ignored. Using xargs seems to just give me everything at once instead of executing on each found item.

Last edited by shamgar03; 08-09-2007 at 03:07 PM.
 
Old 08-09-2007, 03:15 PM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
-type d won't grab too many jpeg files. And if you 'cd' to the directory, then something like 'for f in *jpg; do mv $f ${f%%.*}.jpeg; done' might suffice. Which has nothing to do with find and pipes but is a way to handle your example. How about citing an actual case? You don't have to use xargs - you can just do a for loop with find or whatever you want. There's a million ways to do stuff, so it's hard to guess which corners of your cases you don't like.
 
Old 08-09-2007, 03:33 PM   #7
shamgar03
Member
 
Registered: Jul 2004
Distribution: Gentoo, CentOS
Posts: 92

Original Poster
Rep: Reputation: 15
for loop...

A for loop could work. This is what I am doing. I am trying to change extensions of files, to be uniform. The type d was a typo on my part, I always run the find before I actually try to run the find -exec, I meant to put type f. I will try the for loop, I just like the find, since it is more flexible. This is mainly just a pet peeve since I use find all the time, yet I can never get the pipes to work. I have written scripts to get around it before, but I just thought someone might know a better way.
 
Old 08-09-2007, 03:51 PM   #8
nilleso
Member
 
Registered: Nov 2004
Location: ON, CANADA
Distribution: ubuntu, RHAS, and other unmentionables
Posts: 372

Rep: Reputation: 31
for file in `ls`
do
mv $file $file.JPG
done

...you could do for file in `find ....`

cheers
 
Old 08-09-2007, 04:06 PM   #9
shamgar03
Member
 
Registered: Jul 2004
Distribution: Gentoo, CentOS
Posts: 92

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nilleso
for file in `ls`
do
mv $file $file.JPG
done

...you could do for file in `find ....`

cheers
file in 'find' would be powerful, thanks
 
  


Reply



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
Utility to find C function defintions, variables, etc... stingerkiss Linux - Software 1 11-19-2006 03:35 PM
ERROR: Unable to find the system utility `ld`; dmitree Linux - Newbie 2 02-16-2004 06:40 PM
Free UNIX/Linux/OS X Networking Utility??? cmf5150 Linux - Networking 2 01-05-2004 02:22 PM
Utility to find out FSB siddiqu Linux - Hardware 5 06-16-2003 12:10 AM
What's in my computer/Utility to find out geraldgu Linux - Software 2 12-14-2001 12:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:55 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