Several exec on find send all the output to the last redirection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Several exec on find send all the output to the last redirection
# 1  
Old 12-12-2014
Several exec on find send all the output to the last redirection

Example script:

Code:
 find mydir -type f -exec echo {}>aaa \; -exec echo {}>bbb \;

The two paths go the the bbb file, while there should be one of them on each file. How should I do it to get it working?
# 2  
Old 12-12-2014
One way:
Code:
find mydir -type f  | tee -a aaa > bbb

You do not need echo, find prints by default.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-12-2014
The reason is that >aaa and >bbb are handled by the shell.
While it does not matter where you place them, one usually places them at the end.
So your code is identical to
Code:
find mydir -type f -exec echo {} \; -exec echo {} \; >aaa >bbb

The tee (or a further descriptor) makes aaa and bbb identical - probably not what you intended.

---------- Post updated at 03:16 PM ---------- Previous update was at 02:07 PM ----------

Probably you want something like this:
Code:
find . -type f -exec sh -c 'echo "$1" >>files' sh {} \; -o -type d -exec sh -c 'echo "$1" >>directories' sh {} \;

Because -print is far more efficient and there are likely more files than directories, the following should be more efficient:
Code:
>directories #rewrite
find . -type f -print -o -type d -exec sh -c 'echo "$1" >>directories' sh {} \; > files

These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with I/O redirection via exec

Hello, I am running a shell script on AIX 6.1. The script calls ksh to run. ksh is also the login shell for the account under which I am running this script, but for convenience I always change to the bash shell via "exec bash" after I "su" to the account. The script redirects stdout and... (4 Replies)
Discussion started by: Clovis_Sangrail
4 Replies

2. UNIX for Dummies Questions & Answers

Send job to Background after input redirection

Hi, I am having issues with syntax when I am trying to send a job to the background after a input redirection. I have this script which sends some files to different servers after zipping them. Once I execute it, it will ask for user input as of which server the files need to go to. (The... (3 Replies)
Discussion started by: grep_me
3 Replies

3. Shell Programming and Scripting

output redirection

Hi all I was wondering if there was a slicker way of doing this without the file - awk '{print $2}' FS=":" "${FILE}" > "${TMPFILE}" { read M_GRP_ID || m_fail 1 "Error: Read failed 1 (${FUNCNAME})" read M_GRP_WAIT || m_fail 1 "Error: Read failed 2 (${FUNCNAME})" }... (6 Replies)
Discussion started by: steadyonabix
6 Replies

4. Shell Programming and Scripting

Redirection of ls -l output

Hi I am making a script where i want to redirect the output of ls -l to a file Example #ls -l fil1.txt > /opt/temp/a.txt ac: No such file or directory I want to capture output of this command like here output is ac: No such file or directory can anyone help (4 Replies)
Discussion started by: anish19
4 Replies

5. Shell Programming and Scripting

find: missing argument to `-exec' while redirecting using find in perl

Hi Friends, Please help me to sort out this problem, I am running this in centos o/s and whenever I run this script I am getting "find: missing argument to `-exec' " but when I run the same code in the command line I didn't find any problem. I am using perl script to run this ... (2 Replies)
Discussion started by: ramkumarselvam
2 Replies

6. UNIX for Dummies Questions & Answers

Output redirection

Hello i am trying to write a script that will redirect the output to a certain file. Here is the code so far: #!/bin/bash ps -e | sort | more > psfile When I execute the script nothing happens since i assume the output was redirected to the file called psfile. When I try to look at the... (1 Reply)
Discussion started by: mfruiz34
1 Replies

7. Shell Programming and Scripting

Output redirection

We have an application here that does some table queries and then prints the result on screen. I do not have the code of this application (which i will just call "queryCommand"), but what it does is that you call it with some parameters and it prints some info about the query and then the... (5 Replies)
Discussion started by: jolateh
5 Replies

8. Shell Programming and Scripting

Output redirection to exec does not work

Hello Experts, I am on Solaris 10 Due to some limitations in one of the vendor software, I am forced to output the command to exec and then run it from there. For example.. $(echo "/usr/bin/cp a.dat b.dat") # This works However, $(echo "/usr/bin/cat a.dat > c.dat") # This does not... (8 Replies)
Discussion started by: Gokul Kumar G
8 Replies

9. Shell Programming and Scripting

Redirection output

Hi there I have a script that runs but it outputs everything onto the screen instead of a file. I've tried using the > outputfile.txt however all it does is dump the output to the screen and creates an outputfile.txt but doesn't put anything in that file. Any help would be appreciated ... (6 Replies)
Discussion started by: kma07
6 Replies

10. Shell Programming and Scripting

redirection and output

I'm redirecting the output of a command to a logfile, however, if the user is on a terminal I would also like the output to be displayed on the screen. tar tvf some_tarfile >Logfile if the user is on a term then have the output to the Logfile and also be displayed on the screen at the same... (2 Replies)
Discussion started by: nck
2 Replies
Login or Register to Ask a Question