Use find with cp and sed in ksh to copy files to a slightly different location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use find with cp and sed in ksh to copy files to a slightly different location
# 1  
Old 10-19-2016
Use find with cp and sed in ksh to copy files to a slightly different location

Hello there wonderful people,

I am running on Solaris 10 and with the following ksh version:
Code:
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:
Code:
find /dir1/dir2/save –type f –name *_v2 –exec cp {} `echo {} | sed 's/save//'` \;

But I get this:
Code:
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.

Any help is much appreciated.
Thank you!

Last edited by ejianu; 10-19-2016 at 11:23 AM..
# 2  
Old 10-19-2016
The quotes in the sed command look somewhat suspicious...
# 3  
Old 10-19-2016
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:
Code:
sed 's/save//'

Yep, above looks better, these are the quotes I use.

Edit: I have also changed the quotes in the original post.

Last edited by ejianu; 10-19-2016 at 11:35 AM..
# 4  
Old 10-19-2016
Hi,

Can you try this one ?
Code:
find /dir1/dir2/save -type f -name *_v2 -exec cp {} `dirname {}` \;

Not tested though.
# 5  
Old 10-19-2016
Quote:
Originally Posted by ejianu
So I was thinking of something like this:
Code:
find /dir1/dir2/save -type f -name *_v2 -exec cp {} `echo {} | sed 's/save//'` \;

There are several problems with this: first, the "{}" placeholder for the found filename can only be used ONCE in a single -exec clause.

Second: you are calling poor sed for every file you find. This is overworking the system.

try the following:

Code:
find /dir1/dir2/save -type f -name "*_v2" -print |\
while read infile ; do
     outfile="${infile%%/save*}${infile*save}"
     print - cp "$infile" "$outfile"
done

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 10-19-2016
There might be a # missing - try
Code:
outfile="${infile%%/save*}${infile#*save}"

These 2 Users Gave Thanks to RudiC For This Post:
# 7  
Old 10-19-2016
Quote:
Originally Posted by RudiC
There might be a # missing - try
Code:
outfile="${infile%%/save*}${infile#*save}"

Yes, absolutely. Sorry for the typo and thanks for pointing it out.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy data at specified location from multiple files

Hello everyone, Im super new to coding but increasingly in need of it at work. Im have task stacked because of this problems, that I cannot figure out how to solve looking on the internet after trying many many things that looked similar to me. I have multiple data files of the form (see below).... (2 Replies)
Discussion started by: Xfiles_fan
2 Replies

2. Shell Programming and Scripting

Find and Copy file of specific location

Dear All, I need to transfer all files present in one location to another but those files should be of specific extension like. Find and copy all files of extension .xls, .pdf, .txt from location usr/tmp to location /per/Treat (6 Replies)
Discussion started by: yadavricky
6 Replies

3. Shell Programming and Scripting

How to copy files from one location to another using xargs??

Hello Experts, I need to copy files from one location to another using xargs. Tried something like this (In Ubuntu & Solaris ). mkdir -p 1234; find /home/emd/Desktop/n007/M007/ -type f -name "A2014*" | xargs -0 cp -r {} /home/emd/Desktop/1234 But every time i run this, a weird error... (6 Replies)
Discussion started by: Saidul
6 Replies

4. Shell Programming and Scripting

Copy files from one location to another

I have below files in one location /test/files and also for each dates there are similar files A20130924.0000-0005_file1 A20130924.0000-0005_file2 A20130924.0005-0010_file1 A20130924.0005-0010_file2 . . . A20130924.2355-0000_file1 A20130924.2355-0000_file2 If i execute the script like... (4 Replies)
Discussion started by: Saidul
4 Replies

5. Shell Programming and Scripting

How to copy files from one location to another based on a priority?

Hi Gurus, I am a newbie to shell scripting and I am facing a problem right now.I have to automate the copy of files based on a priority.The scenario is as below: 1) There will be files from Mon-Fri with Mon file being named as abc_def_01_YYYYMMDD and Tue file being abc_def_02_YYYYMMDD and so... (4 Replies)
Discussion started by: vikramgk9
4 Replies

6. UNIX for Dummies Questions & Answers

how to copy files and record original file location?

:EDIT: I think my post name should have been labeled: how to copy files and record original file location. not "retain". Hello, this is my first post! I searched the forums a lot before posting, but was unable to answer my question. Here's my problem: There are several hundred text files... (4 Replies)
Discussion started by: willie8605
4 Replies

7. Shell Programming and Scripting

Copy files in thumbnail folder to a secondary location for Amazon S3

Hello all! I am trying to create a script that will copy files from one location, to another but only folders that are filled with thumbnails to an exact directory replica in the second location. For example: /images/2012/01/19/Event/Photographer/thumbnails to ... (4 Replies)
Discussion started by: Buzzman25
4 Replies

8. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

9. UNIX for Advanced & Expert Users

copy files from one location to similar location

I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure, say location 1, /home/rick/tmp_files/1-12/00-25/ here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
Discussion started by: pharos467
1 Replies
Login or Register to Ask a Question