Using pipes in a find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using pipes in a find command
# 1  
Old 02-19-2010
Using pipes in a find command

How are pipes used inside a find -exec clause?

I want to traverse a directory tree and find all of the shell scripts, and then based on that I need to perfrom further processing. So I have something like
Code:
find . -name \*  -exec "file {} \| grep 'script' > /dev/null" \; -ls

with -ls as a simple version of the further processing!
This doesn't work, so any suggestions appreciated.
# 2  
Old 02-19-2010
do you want to grep within files that came out from the find?
or
grep in the file names itself?

if you want all the files then better ignore the -name switch.

grep within files for the word 'script':
Code:
find . -type f -exec grep 'script' {} 2>/dev/null \;

grep within filenames:
Code:
find . -type f | grep 'script' 2>/dev/null

# 3  
Old 02-19-2010
What OS are you using?
I would use something like this:

Code:
find <path> -type f | xargs file | fgrep script ...

# 4  
Old 02-19-2010
Or maybe something like:
Code:
file * |grep 'script'|cut -d: -f1| ls -l

# 5  
Old 02-19-2010
You can write something like this too,
but it will be extremely inefficient (but, in some cases, more flexible):

Code:
find <path> -type f -exec bash -c '
  f=$1 t="$(file -- "$f")"
  case $t in
    ( *script* );;
    ( * ) exit 1 ;;
  esac
  ' - {} \; -ls

Of course, if recursion is not needed, you should use Franklin52's solution.
# 6  
Old 02-22-2010
Clarification...

Thanks for the input guys, but what I want to do is:-
1. Traverse a complete directory tree - not just a single directory
2. I don't want to look for "script" in either the filename or the content of the file. I want to look for it in the output of the 'file' command being run on each file found in turn.
3. I'm natively using Korn shell on Solaris 10, but could drop into bash/csh/sh if I have to!

Yes, I think I could use xargs in this case, but to an extent, I'm more interested in finding out the correct way (assuming there is one!) to use a pipe within a find -exec clause, 'cos then I can use it for all sorts of things in the future!

Jerry
# 7  
Old 02-22-2010
Something to start with:
Code:
file `find . -type f`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Limit on number of pipes after long-running command?

I'm trying to create a minimal, crude keylogger for X using only a shell script. I was quickly stumped: Why do these two commands entered in a terminal emulator produce output when I type... $ xinput test 6 | grep press $ xinput test 6 | awk '{print $3}' ...but this command produces no... (13 Replies)
Discussion started by: DevuanFan
13 Replies

2. Shell Programming and Scripting

awk to find and replace Double quotes between pipes

Hello, Need a AWK command to find and replace Double Quotes in Pipe delimited files Actully its a CSV file converted to Pipe but the double quotes still exists. So i want to Get rid of them. Example Input 1|2|3|sadsad|"Abc Efg 3"""|dada Output 1|2|3|sadsad|Abc Efg 3"|dada Thanks... (5 Replies)
Discussion started by: krux_rap
5 Replies

3. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

4. Linux

Simplified find command to find multiple file types

Hi, I'm using the following command to find the multiple requierd file types and its working fine find . -name "*.pl" -o -name "*.pm" -o -name "*.sql" -o -name "*.so" -o -name "*.sh" -o -name "*.java" -o -name "*.class" -o -name "*.jar" -o -name "*.gz" -o -name "*.Z" -type f Though... (2 Replies)
Discussion started by: vickramshetty
2 Replies

5. Shell Programming and Scripting

Multiple pipes toward a single awk command

Hello, I would like to pipe two variables into awk, but I don't know how to do. Each variable, "a" and "b", are in fact a list of data. They are not files. So to get awk to work with it I am using: echo $a | awk 'FNR==NR{print $1}FNR!=NR{print $4}' The above works, but when I am... (5 Replies)
Discussion started by: jolecanard
5 Replies

6. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

7. Shell Programming and Scripting

Little bit weired : Find files in UNIX w/o using find or where command

Yes , I have to find a file in unix without using any find or where commands.Any pointers for the same would be very helpful as i am beginner in shell scritping and need a solution for the same. Thanks in advance. Regards Jatin Jain (10 Replies)
Discussion started by: jatin.jain
10 Replies

8. UNIX for Advanced & Expert Users

help with !(tail -2) command.. using pipes

I am trying to using pipe (|) with ! (not) operator to list all the other files except the latest two and I am using the following command. $ ls -ltr *.lst|!(tail -2) ksh: 20050211180252.lst: cannot execute but it is trying to execute the file returned by tail -2. I am able to do that in 4... (8 Replies)
Discussion started by: sdlayeeq
8 Replies

9. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies

10. Shell Programming and Scripting

command find returned bash: /usr/bin/find: Argument list too long

Hello, I create a file touch 1201093003 fichcomp and inside a repertory (which hava a lot of files) I want to list all files created before this file : find *.* \! -maxdepth 1 - newer fichcomp but this command returned bash: /usr/bin/find: Argument list too long but i make a filter all... (1 Reply)
Discussion started by: yacsil
1 Replies
Login or Register to Ask a Question