find command in for loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find command in for loop
# 1  
Old 12-22-2011
find command in for loop

i have executed the following command in terminal

Code:
find /Users/vasu -name "*.txt" -print

and i am getting the result 

/Users/vasu/file1.txt
/Users/vasu/file2.txt
/Users/vasu/file3.txt

but while i was trying to execute the same in the script it is not working,I tried with below logic in the shell script

Code:
for f in find /Users/vasu -name "*.txt"
do
    echo "$f"
done

output:find
/Users/vasu/Documents/pragathi_unix_practice/master_unix_practice/textfiles
-name
*.txt

can i achieve this by doing any modifiaction?please tell me the reason as well for modificationSmilie

Last edited by methyl; 12-22-2011 at 12:49 PM.. Reason: Added some code tags
# 2  
Old 12-22-2011
You need to evaluate the command:
Code:
for f in $(find /Users/vasu -name "*.txt")
do
echo "$f"
done

# 3  
Old 12-22-2011
It is because find command is not executed, and it is considered as string.

To execute use either $() or backticks.

And also please use code tags !!

This should work:
Code:
for f in $(find /tmp/ -type f)

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

While loop, input from find command

Hello nix Experts, I am a *nix rookie and have run into this issue, can some one help me here and let me know what I am doing wrong. /home/user1> while read n > do > echo $n > done < <(find . -type f -ctime -1 | grep abc) I am getting the below error: -sh: syntax error near... (5 Replies)
Discussion started by: babyPen1985
5 Replies

2. Shell Programming and Scripting

find in for loop

whats wrong with this script for i in Users.csv abd.csv > do > find . -name $i > done (2 Replies)
Discussion started by: theshashi
2 Replies

3. Shell Programming and Scripting

find in for loop

What's wrong with this script for i in Users.csv anc.csv > do > find . -name $i > doneDouble post. Continue here. (0 Replies)
Discussion started by: theshashi
0 Replies

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

5. AIX

using find in for loop

hello gurus, src="/home/training" typ="*.sh" for i in `find $src -name $typ` do ... .. done the above code piece is giving this error: find: 0652-009 There is a missing conjunction I tried this way: for i in `find $src -name "$typ"` But then it didn't get any files, though there... (11 Replies)
Discussion started by: muthursyamburi
11 Replies

6. Shell Programming and Scripting

find command in while loop - how to get control when no files found?

I have the following statement in script: find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN2} -print | while read file; do ... done When there are no files located by the find comand it returns: "find: bad status-- /home/rnitcher/test/....." to the command line How do I get control in... (3 Replies)
Discussion started by: mavsman
3 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. Shell Programming and Scripting

How to combine "find" command in for each loop (tcsh)

Hello I was wandering if I can combine find command in side for each loop in unix the main propose is to change some thing in files from several types and not all of them is this possible ? (on liner script? ) tnx for the helppers (3 Replies)
Discussion started by: umen
3 Replies

9. Shell Programming and Scripting

For Loop with find

I'm trying to write a script to archive some log files, and I'm getting a little stuck. My 'find' command by itself works great, returning all of the log files I need, but when I try to combine it with my loop, I'm getting an error. Here's what I have so far: for FILENAME in $(find . -type f... (4 Replies)
Discussion started by: kadishmj
4 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