I am running on Solaris 10 and with the following ksh version:
strings /bin/ksh | grep Version | tail -2
@(#)Version M-11/16/88i
Suppose I want to copy files that end in _v2 from underneath /dir1/dir2/save directory to /dir1/dir2. Basically, what I�m looking for is to remove /save out of the path.
So I was thinking of something like this:
find /dir1/dir2/save �type f �name *_v2 �exec cp {} `echo {} | sed 's/save//'` \;
But I get this:
cp: /dir1/dir2/save/app.jar_v2 and /dir1/dir2/save/app.jar_v2 are identical
It looks like putting the output of echo through sed does not work as expected.
Not sure why they ended up like that, that is not the issue though. I confirm I use the correct quotes. I will try adding the quotes one more time below to see if they show up correct:
sed 's/save//'
Yep, above looks better, these are the quotes I use.
Edit: I have also changed the quotes in the original post.
I went with bakunin's suggestion since there are multiple sub-directories underneath /dir1/dr2/save and I want to copy the files ending _v2 outside of save and keep the directory structure.
bakunin, could you please explain the magic behind this line:
outfile="${infile%%/save*}${infile#*save}"
Thank you!
---------- Post updated at 06:44 AM ---------- Previous update was at 03:43 AM ----------
Is there a way of removing _v2 from the end of the file in the line below?
Hi ejianu,
Even though both forms produce the same results in this case, I personally prefer to use the % version (like RudiC suggestion in post #10) instead of the %% version you used in post #11 when there is only one possible match.
The difference between the two cases is apparent when the given pattern can match more than once. For example, the following commands:
x=abc.def.ghi
echo ${x%%.*} ${x%.*}
produce the output:
abc.def abc
because %% removes the longest match for .* from the end of $x and % removes the shortest match for .* from the end of $x . (Note that the patterns used in ${var%%pattern} and ${var%pattern} are filename matching patterns; not basic regular expressions and not extended regular expressions.)