ucfperson
asked on
Shell script to find and replace files. I have a list of all files, there is a source(good files)and destin(bad files.
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.
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.
ASKER
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
/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
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
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
ASKER
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]
#!/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]
Hi,
Please see that I inverted comma not quotes
Please see that I inverted comma not quotes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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@whateve r.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@whateve r.com/.mis /cur/11104 62677.M625 018P97811V 00027416I0 035A4C8_21 .mail.what ever.com,S =9655:2,S" "/usr/local/mail/users/m/e /me@whatev er.com/.mi s/cur/1110 462677.M62 5018P97811 V00027416I 0035A4C8_2 1.mail.wha tever.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/111046 2680.M9524 24P97811V0 0027416I00 35A4D8_29. mail.whate ver.com,S= 4184:2,S
/usr/local/mail/users/m/e/ me@whateve r.com/.mis /cur/11104 62680.M952 424P97811V 00027416I0 035A4D8_29 .mail.what ever.com,S =4184:2,S
/usr/move/test/me@whatever .com/.mis/ cur/111046 2706.M1036 84P97811V0 0027416I00 35A546_85. mail.whate ver.com,S= 51807:2,S
/usr/local/mail/users/m/e/ me@whateve r.com/.mis /cur/11104 62706.M103 684P97811V 00027416I0 035A546_85 .mail.what ever.com,S =51807:2,S
/usr/move/test/me@whatever .com/.nagi os/cur/111 0462848.M5 72430P9781 1V00027416 I00360E22_ 268.mail.w hatever.co m,S=1188:2 ,S
/usr/local/mail/users/m/e/ me@whateve r.com/.nag ios/cur/11 10462848.M 572430P978 11V0002741 6I00360E22 _268.mail. whatever.c om,S=1188: 2,S
/usr/move/test/me@whatever .com/.nagi os/cur/111 0462849.M2 7031P97811 V00027416I 00360E25_2 69.mail.wh atever.com ,S=1128:2,
/usr/local/mail/users/m/e/ me@whateve r.com/.nag ios/cur/11 10462849.M 27031P9781 1V00027416 I00360E25_ 269.mail.w hatever.co m,S=1128:2 ,
/usr/move/test/me@whatever .com/.nagi os/cur/111 0462849.M3 24907P9781 1V00027416 I00360E23_ 270.mail.w hatever.co m,S=1117:2 ,
/usr/local/mail/users/m/e/ me@whateve r.com/.nag ios/cur/11 10462849.M 324907P978 11V0002741 6I00360E23 _270.mail. whatever.c om,S=1117: 2,
/usr/move/test/me@whatever .com/.nagi os/cur/111 0462849.M7 82381P9781 1V00027416 I00360E28_ 271.mail.w hatever.co m,S=1189:2 ,
/usr/local/mail/users/m/e/ me@whateve r.com/.nag ios/cur/11 10462849.M 782381P978 11V0002741 6I00360E28 _271.mail. whatever.c om,S=1189: 2,
/usr/move/test/me@whatever .com/.nagi os/cur/111 0462850.M2 38661P9781 1V00027416 I00360E26_ 272.mail.w hatever.co m,S=1128:2 ,
/usr/local/mail/users/m/e/ me@whateve r.com/.nag ios/cur/11 10462850.M 238661P978 11V0002741 6I00360E26 _272.mail. whatever.c om,S=1128: 2,
#!/bin/sh
for file in `cat matches`
do
find / -name $file >>matches.log
done
he is the example cp "/usr/move/test/me@whateve
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
/usr/local/mail/users/m/e/
/usr/move/test/me@whatever
/usr/local/mail/users/m/e/
/usr/move/test/me@whatever
/usr/local/mail/users/m/e/
/usr/move/test/me@whatever
/usr/local/mail/users/m/e/
/usr/move/test/me@whatever
/usr/local/mail/users/m/e/
/usr/move/test/me@whatever
/usr/local/mail/users/m/e/
/usr/move/test/me@whatever
/usr/local/mail/users/m/e/
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