Register
It is currently Sun Apr 22, 2018 9:47 pm

find and copy


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sun Aug 27, 2017 5:22 am   

Joined: Sat Aug 19, 2017 1:17 am
Posts: 4
Script that checks all subdirectories for files named “file” and copies them into a new directory called “new”,


#!/bin/bash

for i in ( find . -type f -name "file" )

do

echo $i

cp -r ${i} new/${i}

done


I am having error while executing. Can someone help please?


Top
 Profile  
 PostPosted: Sun Aug 27, 2017 6:35 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Please use code tags. Code is otherwise hardly readable.
Then you have to name the errors precisely.
"Does not work" does not help.

Job can be done with a single find command.


Top
 Profile  
 PostPosted: Sun Aug 27, 2017 8:02 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
#wrong
for i in ( find . -type f -name "file" )
# the line above is NOT a substitution as intended

# syntactically right
for i in $( find . -type f -name "file" )
# bad bad style

# as you get pathnames of files
# you have to create folder "new" prior to using it
# ensure with "mkdir -p new" it's existence
cp -r ${i} new/${i}

# a single line doing the job (if "new" exists)
find . -type f  -name "file" -exec cp {} new/{} \;


Top
 Profile  
 PostPosted: Sat Sep 02, 2017 9:01 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 693
Location: Des Moines, Iowa
Pretty sure we should let naveena_k just do his or her own homework.... ;)


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP