We help IT Professionals succeed at work.

Shell script to find and replace files. I have a list of all files, there is a source(good files)and destin(bad files.

273 Views
Last Modified: 2013-12-26
Here is what I have
I have a file with file names in it see exaple
file
filename1
filename2
filename3

I need a script that will search for all files on the drive that have the filename1 then do a copy from source to replace all instances on that drive with 1 that is local.
My first idea is to update the locate databse then move my source(good files on the drive) that way the locate will only pick up the bad files. I need to make sure, because some time the locate command will pull up 3 or 4 different directories.
Any an all help would be awesome.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

The requirement is not clear. Are you trying to copy a file say filename1 from a remote server to replace all the local files with the same name?

- To copy filename1 from remote server, you may use ftp or scp (both can be automated)

To use ftp , use .netrc file in the user's home directory. This file should not be readable by others i.e. use
chmod 400 .netrc

The entry in .netrc should be as below:

machine myftpserver
login remoteusername
password mypassword

myftpserver is the server which is the ftp server where you want to sent the file(s)
remoteusername is the remote user login name on the ftp server
mypassword is the password of the remote user on the ftp server

for more info about .netrc, please use man netrc

The ftp command can be then put in a script to transfer file from remote server.

Similarly, you may use scp to copy file from remote server, but scp requires ssh on the remote server.

- To locate all the files in local server with filename1,
find / -name filename1 > filename1.log

- To get all the local files found replaced by the file transfered (assume it is in /tmp/filename1)

awk '{print "cp /tmp/filename1 ", $1' filename1.log > filename1.sh
sh filename1.sh

Author

Commented:
no here is the situiation.
/usr/local/file1
/usr/local/file2
/usr/me/file3

/usr/src/backup of files. from a previous version.
/usr/src/listof all files (example)
vi file.txt
file1
file2
file3
So search the harddrive for the files listed in the file.txt then copy from the /usr/src/backup to the correct directories.
/usr/local/file1
/usr/local/file2
/usr/me/file3

CERTIFIED EXPERT
Top Expert 2007

Commented:
Ok,

for file in `cat file.txt`
do
     find / -name $file > $file.log
     nawk -v F=$file '{print "cp /usr/src/backup/F ", $1' $file.log > $file.sh
     sh $file.sh
done

Author

Commented:
Troubleshooting what you did,
#!/bin/sh
for file in 'cat matches'
do
    find / -name $file >$file.log
done
resulted in this
[root@mail memberdir]# ./script.sh
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
CERTIFIED EXPERT
Top Expert 2007

Commented:
Hi,

Please see that I inverted comma not quotes
CERTIFIED EXPERT
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Than you, now running the first command. below I get the below results, in a single file, I have put spaces inbetween the non matchs just for here so it would make it easier to read. I need it to always copy the /usr/move/test/me to the /usr/local/mail/users/m/e/me@whatever.com directoryand sub
#!/bin/sh
for file in `cat matches`
do
find / -name $file >>matches.log
done

he is the example cp "/usr/move/test/me@whatever.com/.mis/cur/1110462677.M625018P97811V00027416I0035A4C8_21.mail.whatever.com,S=9655:2,S" "/usr/local/mail/users/m/e/me@whatever.com/.mis/cur/1110462677.M625018P97811V00027416I0035A4C8_21.mail.whatever.com,S=9655:2,S"
looking though the file it appears the 1st entry is always the /usr/local the 1 I need to replace and the 2nd file it finds is always the 1 I need to replace it with.

/usr/move/test/me@whatever.com/.mis/cur/1110462680.M952424P97811V00027416I0035A4D8_29.mail.whatever.com,S=4184:2,S
/usr/local/mail/users/m/e/me@whatever.com/.mis/cur/1110462680.M952424P97811V00027416I0035A4D8_29.mail.whatever.com,S=4184:2,S

/usr/move/test/me@whatever.com/.mis/cur/1110462706.M103684P97811V00027416I0035A546_85.mail.whatever.com,S=51807:2,S
/usr/local/mail/users/m/e/me@whatever.com/.mis/cur/1110462706.M103684P97811V00027416I0035A546_85.mail.whatever.com,S=51807:2,S

/usr/move/test/me@whatever.com/.nagios/cur/1110462848.M572430P97811V00027416I00360E22_268.mail.whatever.com,S=1188:2,S
/usr/local/mail/users/m/e/me@whatever.com/.nagios/cur/1110462848.M572430P97811V00027416I00360E22_268.mail.whatever.com,S=1188:2,S

/usr/move/test/me@whatever.com/.nagios/cur/1110462849.M27031P97811V00027416I00360E25_269.mail.whatever.com,S=1128:2,
/usr/local/mail/users/m/e/me@whatever.com/.nagios/cur/1110462849.M27031P97811V00027416I00360E25_269.mail.whatever.com,S=1128:2,

/usr/move/test/me@whatever.com/.nagios/cur/1110462849.M324907P97811V00027416I00360E23_270.mail.whatever.com,S=1117:2,
/usr/local/mail/users/m/e/me@whatever.com/.nagios/cur/1110462849.M324907P97811V00027416I00360E23_270.mail.whatever.com,S=1117:2,

/usr/move/test/me@whatever.com/.nagios/cur/1110462849.M782381P97811V00027416I00360E28_271.mail.whatever.com,S=1189:2,
/usr/local/mail/users/m/e/me@whatever.com/.nagios/cur/1110462849.M782381P97811V00027416I00360E28_271.mail.whatever.com,S=1189:2,

/usr/move/test/me@whatever.com/.nagios/cur/1110462850.M238661P97811V00027416I00360E26_272.mail.whatever.com,S=1128:2,
/usr/local/mail/users/m/e/me@whatever.com/.nagios/cur/1110462850.M238661P97811V00027416I00360E26_272.mail.whatever.com,S=1128:2,
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.