Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux how to copy but not overwrite? [closed]

Tags:

linux

bash

cp

I want to cp a directory but I do not want to overwrite any existing files even it they are older than the copied files. And I want to do it completely noninteractive as this will be a part of a Crontab Bash script. Any ideas?

like image 565
mnowotka Avatar asked Feb 22 '12 10:02

mnowotka


People also ask

How do I copy files without overwriting?

If you are copying files using drag-drop or copy/paste, you may simply choose “Skip this file” or “Skip these files” option to not overwrite the files that are already existed at the destination folder. Or, if you are using command line copy, you can answer N to bypass these files that are already existed.

How do I stop a file overwriting in Linux?

1 Answer. The best method would be for you to learn to create a copy yourself before editing a file. Yes. From command line set the "immutable" attribute (only the admin can remove that option) and nobody will be able to alter the file (edit, remove, move etc.).

Does Linux copy overwrite?

By default, cp will overwrite files without asking. If the destination file name already exists, its data is destroyed. If you want to be prompted for confirmation before files are overwritten, use the -i (interactive) option.

How do you copy recursively?

In order to copy the content of a directory recursively, you have to use the “cp” command with the “-R” option and specify the source directory followed by a wildcard character.


1 Answers

Taken from the man page:

-n, --no-clobber               do not overwrite an existing file (overrides a previous -i option) 

Example:

cp -n myoldfile.txt mycopiedfile.txt 
like image 122
hovanessyan Avatar answered Oct 11 '22 02:10

hovanessyan