LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   unix find utility (https://www.linuxquestions.org/questions/linux-software-2/unix-find-utility-575963/)

shamgar03 08-09-2007 02:39 PM

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?

slackie1000 08-09-2007 02:43 PM

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

slakmagik 08-09-2007 02:45 PM

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.

anomie 08-09-2007 02:49 PM

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.

shamgar03 08-09-2007 03:05 PM

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.

slakmagik 08-09-2007 03:15 PM

-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.

shamgar03 08-09-2007 03:33 PM

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.

nilleso 08-09-2007 03:51 PM

for file in `ls`
do
mv $file $file.JPG
done

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

cheers :)

shamgar03 08-09-2007 04:06 PM

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


All times are GMT -5. The time now is 12:20 AM.