edit results of a find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting edit results of a find command
# 1  
Old 01-24-2006
edit results of a find command

Hi
Purpose is to have a utility command to find and edit files .
I tried a function like the following in my .profile file


function vifind(){
find . -name $1 -print -exec vi {} \;
}

Is this correct? is there a better way to do it?
I see this behaving a bit strange in case of AIX, and it does not do a group edit. Instead, I am forced to vi each file one by one.

also, any help in explaining the -exec syntax will be appreciated.
How exactly -exec <anycommand> {} \; works? what is the reason behind {} \; ?


Thanks in advance
# 2  
Old 01-24-2006
The command you have used opens one file at a time...

Try whether this helps...

vi $(find . -name $1 | paste -s -)
# 3  
Old 01-24-2006
That worked!
me being a n00b, could you please explain about
-exec and {} and \?
# 4  
Old 01-24-2006
$ ls
test.log tmp1 tmp3

$ find . -name "tmp*"
./tmp1
./tmp3

$ find . -name "tmp*" -exec echo {} " is the file" \;
./tmp1 is the file
./tmp3 is the file

-exec - the command following -exec is executed against the every file found through find command

{} - this can be used in the place of file name in the -exec command because we are not sure what is going to be file name in the command while exectuing the command... say for example, for rm command we need to mention the file name but we don't know the file name, hence we say -exec rm {}... at the time of execution {} will be replaced with the current file of find command.
\; - is just syntax, I don't see any special reason for it, may be I don't know as well... I think it is just mandatory to use in the end along with -exec...

another example :

$ find . -name "tmp*" -exec mv {} {}.bkup \;

is equalent to find all tmp files and mv <filename> <filename>.tmp

you can use {} any number of times to represent the file name.
# 5  
Old 01-25-2006
Thanks a ton!
i really appreciate ur help
# 6  
Old 01-25-2006
hmm just making a guess for the \;, as mahendr said exec works on each file which is a result of find. basically exec spawns a process for each result of find, and executes the command, I think to allow multiple commands to be executed on a single command line you need the ;, but again ; is a special shell built-in character, hence you need to escape it with the \, hence the \;
a point to note here is in case you have lots of files as a result of find, you will end up using lots of process by exec, so then you can have an option to use xargs, instead of exec. finding the difference between the 2 is a different thread, and you might find it posted on some forum on this site. if you dont, lemme know, at the moment i m a bit lazy to explain that Smilie ( or maybe i dont know it either Smilie )
# 7  
Old 10-15-2007
./Example/pnlang/index.html
./Example/pnblocks/index.html
./Example/pnstyle/index.html
./Example/index.html
./legal/pndocs/index.html


this is the result of:
find . -exec grep -q "function v470" '{}' \; -print

How can I use this data to delete each file under this condition. How do I pipe each result into another command?

I want to do something like:
find . -exec grep -q "function v470" -exec rm {} -rdf

Any help GREATLY appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find with rm command gives strange results

I want to remove any files that are older than 2 days from a directory. It deletes those files. Then it comes back with a message it is a directory. What am I doing wrong here? + find /mydir -mtime +2 -exec rm -f '{}' ';' rm: /mydir is a directory (2 Replies)
Discussion started by: jtamminen
2 Replies

2. UNIX for Beginners Questions & Answers

Weird 'find' results

Hello and thanks in advance for any help anyone can offer me I'm trying to learn the find command and thought I was understanding it... Apparently I was wrong. I was doing compound searches and I started getting weird results with the -size test. I was trying to do a search on a 1G file owned by... (14 Replies)
Discussion started by: bodisha
14 Replies

3. Shell Programming and Scripting

ksh script find command not printing any results

Hello, Hitting a wall on this one. When at the command prompt it works fine: # find /home/testuser -name 'PAINT*canvasON.txt' /home/testuser/PAINT_canvasON.txt # pwd /home/testuser # ls -l PAINT*canvasON.txt -rw-r--r-- 1 root user 23 Feb 07 02:58 PAINT_canvasON.txt... (2 Replies)
Discussion started by: seekryts15
2 Replies

4. UNIX for Dummies Questions & Answers

How to do ls -l on results of grep and find?

Hi, Am running the command below to search for files that contains a certain string. grep -il "shutdown" `find . -type f -mtime -1 -print` | grep "^./scripts/active" How do I get it to do a ls -l on the list of files? I tried doing ls -l `grep -il "shutdown" `find . -type f -mtime -1... (5 Replies)
Discussion started by: newbie_01
5 Replies

5. UNIX for Dummies Questions & Answers

Find command gave unexpected results

Hi, I recently executed a find command that caused unexpected permission changes and we had to do a full system restore. Can someone please explain what this command would do? find /staging/admin/scr * -exec chmod 755 '{}' + It caused file permissions inside / to be modified strangely. ... (1 Reply)
Discussion started by: poornima
1 Replies

6. Shell Programming and Scripting

Place 'find' results within TeX command

Hi, In an effort to collect all my .java-files and place them in a LaTeXfile (using the listings environment of latex), i tried to use ex. So what i have now is: find . -name "*\.java" > latex ex latex <<HERE %s/\(.*\)/\\lstinputlisting{\1} wq HERE So i try to escape the '\' with... (1 Reply)
Discussion started by: HannesBBR
1 Replies

7. Shell Programming and Scripting

need to move find results

I am looking for files of a certian type and logging them. After they are logged they need to be moved to a different directory. HOw can i incorporate that in my current script? CSV_OUTFILE="somefile.csv" find . -name W\* -exec printf "%s,%s,OK" {} `date '+%Y%m%d%H%M%S'` \; > ${CSV_OUTFILE} ... (9 Replies)
Discussion started by: pimentelgg
9 Replies

8. Shell Programming and Scripting

Sending find command results to email

This is probably simple so forgive me... I just want to find all files in a folder created within the last 10 minutes... This is easy: # find /home/folder -cmin -10 If the find command locates any files created in the last ten minutes I want it to send an email alert. I just want to... (3 Replies)
Discussion started by: gardellap
3 Replies

9. UNIX for Dummies Questions & Answers

How to sort find results

Hi-- Ok. I have now found that: find -x -ls will do what I need as far as finding all files on a particular volume. Now I need to sort the results by the file's modification date/time. Is there a way to do that? Also, I notice that for many files, whereas the man for find says ls is... (8 Replies)
Discussion started by: groundlevel
8 Replies

10. UNIX for Dummies Questions & Answers

find results

Hi, how can I get only useful results from find / -size 10000000 without the "Permissions denied" files ? tks C (5 Replies)
Discussion started by: Carmen123
5 Replies
Login or Register to Ask a Question