Visit Our UNIX and Linux User Community

unexpected pipeline result with find -exec

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unexpected pipeline result with find -exec
# 1  
Old 02-19-2008
Lightbulb unexpected pipeline result with find -exec

Hi All,

I probably miss something fundamental here.
I want to rename a bunch of files in subdirectories (that might contain white spaces) with names that are related.

I thought following could do the job:
find . -name *.sh -exec mv {} $(echo {} | sed -e 's/0/1/g') \;

Now to be able to see the output I generate an echo of the substitutes:
find . -name *.sh -exec echo {} $(echo {} | sed -e 's/0/1/g') \;

Interestingly echo {} equals echo {} | sed -e 's/0/1/g'.
It looks like the substitution of {} is somehow not done in the order I expected.

If I try the pipelining directly (without ${..}) it generates the right substitution (but for my purpose not usable):
find . -name *.sh -exec echo {} $(echo "2001" | sed -e 's/0/1/g') \;

Thank you very much for any idea pointing out my mistake,
# 2  
Old 02-20-2008

Thanks for your interest!
To soliloquize further I think the answer is that it can't work - according to the man pages:
-exec utility [argument ...] ;
[..] If the string ``{}'' appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file.
[..] Utility and arguments are not subject to the further expansion of shell patterns and constructs.

Please correct me if I am wrong,
thanks anyway,
# 3  
Old 02-20-2008
use xargs -
find /path | xargs  <command set goes here>
find /path -type f | xargs mv "{}" "{}"".old"

except I think you would be better served with a loop
find /path -type -f  -name '*.sh' | \
while read file
     echo "$file"  # the code you have makes no sense to me, so change this line
     # maybe you want to add 2001 to the file name, I cannot tell.
     # you are sed-ing 2001 for some reason.
     mv "$file" "$file".2001

You should add code to get rid of space - maybe repalce them with underlines. Unless spaces are a required feature.
# 4  
Old 02-20-2008
Thanks for the reply. "2001" is only a place holder.

If I only wanted to add something to the filename
find . -name *.sh -exec mv {} {}.add_something \;

would work anyway.
But I wanted to process the filename; using xargs seem to suffer a similar limitation: the replacement string can't be fed into $(sed ...) or another way of processing.

find . -name "*.cpp" | xargs -I name mv name $(echo name|sed -e s/old/new/g)

In this case the string name will be fed unprocessed into sed instead of the xarg replacement.

# 5  
Old 02-20-2008
Maybe you're looking for something like this:

find . -name "*.cpp" | sed 's/\(.*\)\(old\)\(.*\)/mv & \1new\3/' | sh

Before you run the command try this to be sure you get the right command:

find . -name "*.cpp" | sed 's/\(.*\)\(old\)\(.*\)/mv & \1new\3/'

# 6  
Old 02-23-2008

Thanks for the reply!

As you can see it took me a while to understand what this is doing...
But it is exactly what I was looking for!
Dodgy script!

Thank you very much!

Previous Thread | Next Thread
Test Your Knowledge in Computers #882
Difficulty: Easy
In October 2013 National Geographic released a file titled American Blackout which dealt with an imagined large-scale cyber attack on SCADA and the US electrical grid.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Beginners Questions & Answers

Unexpected result from awk

Hello, Giving those commands: cat > myfile 1 2 3 ^D cat myfile | awk '{ s=s+$1 ; print s}' The output is: 1 3 6 It seems like this command iterates each time on a different row so $1 is the first field of each row.. But what caused it to refer to each row ?. What I mean... (3 Replies)
Discussion started by: uniran
3 Replies

3. Shell Programming and Scripting

2 exec in find

Guys, I want to find the log files greather than 23 days and i want to perform 2 things here. one is to list the files and second is to gzip the files. hope this can be done using sh -c option. but not sure the exact command. find . -name "*.log" -mtime +23 -exec ls -la {} \; ... (5 Replies)
Discussion started by: AraR87
5 Replies

4. UNIX for Dummies Questions & Answers

Weird: unexpected result after piping a sort

Hello, And when you think you know the basics of something, UNIX in this case, something like what I will describe below comes along.... On a Linux system, a "typical" directory with some files. Say 20. I do: > ls | sort > mylisting Now when I: > vi mylisting There is mylisting... (13 Replies)
Discussion started by: stavros
13 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. Ubuntu

Find and exec

Hello, I am a linux newbe. I want to install a program. I can download it only with wget command from internet. As far as i know this wget command does not transfer the exacutable flags. Because of that i wanted to find all configure files and change their mod to 744. I found this... (1 Reply)
Discussion started by: disconnectus
1 Replies

7. Shell Programming and Scripting

shell script - unexpected result

I hv a file --am executing a script which is giving me unexpected results COntents of file: f1 CMT_AP1_CONT:/opt/sybase/syboc125:150:ASE12_5::Y:UX: CMT_AP1:/opt/sybase/syboc125:150:ASE12_5::Y:UX f1.tmp CMT_AP1_CONT:/opt/sybase/syboc125:150:ASE12_5::Y:UX:... (2 Replies)
Discussion started by: rajashekar.y
2 Replies

8. UNIX for Dummies Questions & Answers

Find Exec

Hello All, Is there a way to make exec do a couple of operations on a single input from find? For example, find . -type d -exec ls -l "{}" ";" I would like to give the result of each "ls -l" in the above to a wc. Is that possible? I want to ls -l | wc -l inside... (1 Reply)
Discussion started by: prasanna1157
1 Replies

9. Shell Programming and Scripting

Help! Paste Multiple SQL output result to exec command

Hi, I want to write the shell script to change multple file name (the file name is get from DB) e.g. cp db1.txt file1_new.txt cp db2.txt file2_new.txt cp db3.txt file3_new.txt I have write the script like this: VAR=`sqlplus -s $LOGON @<<ENDOFTEXT set termout off ... (0 Replies)
Discussion started by: jackyntk
0 Replies

10. Shell Programming and Scripting

Unexpected sed result.

I am in the process of writing a script to change the grub password in the grub.conf file. I thought I had it figured out, but am running into an a problem I can't put my finger on. Command I am running when I find that the grub.conf file contains "password --md5". sed... (1 Reply)
Discussion started by: viRaven
1 Replies

Featured Tech Videos