Discussion Locked

CP command with more options

0 helpful
13 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I want to copy all home folder with sub-folders to /mnt/backup folder everyday at midnight. So, I used cp -R /home/* /mnt/backup/ command and make a script and put it in the cron. First night, it works. But second night, it did not work because it prompted for overwrite. I have used --force but still prompt for overwrite. How can I bypass for overwrite? I looked at the man for cp but did not help me.
I am using Redhat 7.3.
Thanks in advance.

13 replies

by ann777

Why not put a delete line in the script just before the copy? The files should be on backup tape the night before if anything crashes + you have the file in their original location if the script fails between the delete of the old and the copy ofthe new.

by ann777

Try a cp -f or a cp -i

I don't know if either will work in your flavor of unix.

by lintadsl

cp -f would not work for me. Again, there are sub-folders and need to be copied as well.

by pahosler

cp -r --remove-destination /home/* /mnt/backup

or

cp -r --reply=yes /home/* /mnt/backup

cp --help will give you a complete list of options

by skizz

Use one of the archive type commands like tar or cpio.
tar will overwrite the backup each time it is run
tar -cvf /mnt/backup/my_backup.tar /home

by bwaynef

Honestly I dont understand why your attempts have not worked. At the command line try typing:
cp -fR /home/* /mnt/backup. If this doesn't work I'd try adding the --remove-destination flag like this

cp -R --remove-destination /home/* /mnt/backup. Again, I believe all of these have been offered as possible solutions. None of these work? Try adding the --reply=yes flag and see if that works. It will basically reply "yes" to any prompt that is given, in effect giving permission to overwrite existing files in /mnt/backup.

by tg2

you are running it as root?

see if your distro allows

cp -af

-a == pdR / preserve no deference (don't follow symlinks just create a new symlink) and Recursive (but allow for fifo or /dev/null type files)

and also try puting the command as this

\cp -af /home /mnt/backup

the \ means to use the command without any modifications from the environment... or as I understand it to mean..

so if someplace the CP command is set to use "interactive" mode by default, then using the \will negate that setting and use the command straight again.

You'd be surprised where that little \ can come in handy by the way. It solved a sendmail on the command line problem we'd had sending email alerts from our solaris boxes.. by escapingthe command, the command line usage would then correctly allow other switches that weren't working without the \ before a sendmail command line, line.

This discussion is closed to new replies.