I'm trying to copy a file only if it already exists, not if it doesn't, basically overwrite only on Linux. Does anyone know a solution?
I basically need to "look" for a folder then if a file exists, it overwrites in destination (recursive)
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Although there's a small race condition you could use something like this:
src=from.txt dst=to.txt [[ -f "$dst" ]] && cp -fp "$src" "$dst"
[[ ... ]] operations checks to to see if the file already exists, and if it does the
&& allows the copy to proceed.
If you have
rsync you can do it directly (the
--existing flag tells
rsync only to update files that already exist):
rsync --existing --archive "$src" "$dst"
\cp -rf source destination
overwrites if file exists without confirmation