copy shell script

Hello Experts:

I want to create a shell scripts to copy files and read paths from files.txt file ...

If file doesn't exist insert path in missing.txt
if file exist copy and insert path in done.txt

Content of files.txt
/folder/file1.txt
/folder/file2.txt
/folder/file3.txt
....

Open in new window

LVL 5
CalmSoulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
rm -f done.txt missing.txt
while read file
   do
     if [[ -e $file ]]; then
         # cp $file <some other location>
         echo $file >> done.txt
       else
         echo $file >> missing.txt
   done < files.txt

You wrote "if file exist copy" - but where?
0
woolmilkporcCommented:
If you need just info about completion or failure in done.txt or missing.txt, respectively, instead of the exact line as it appears in files.txt:

cp -v $(<files.txt) /some/new/location >done.txt 2>missing.txt

"done.txt" will then contain something like this:

'/folder/file1.txt' -> '/some/new/location/file1.txt'

and "missing.txt" will contain something like this:

cp: cannot stat `/folder/file2.txt': A file or directory in the path name does not exist.
0
CalmSoulAuthor Commented:
In your script I am getting error line 10: syntax error near unexpected token `done'
0
woolmilkporcCommented:
Sorry, I missed a small but important thing ("fi"). My mistake!

rm -f done.txt missing.txt
while read file
   do
     if [[ -e $file ]]; then
         # cp $file <some other location>
         echo $file >> done.txt
       else
         echo $file >> missing.txt
    fi
   done < files.txt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.