scp: Do not Overwrite Existing File On Linux or Unix System

I am a new Linux user. I use scp to upload files to net-storage provided CDN company. I do not want to overwrite the existing files. How can I prevent overwriting of files when using scp command under Linux, Mac OS X or Unix like operating systems?

Short answer – Not possible.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements rsync
Est. reading time N/A

Long answer

The scp command will always overwrite the already present files if owner has write permissions. I suggest that you use the rsync command. The syntax is

rsync --ignore-existing file1 user@server1:/dest/
rsync --ignore-existing
### add ssh, just in case ;) ###
rsync -e ssh --ignore-existing
rsync -avP --ignore-existing *.png
rsync --ignore-existing file1 /path/to/dest/

The --ignore-existing option skip updating files that exist on receiver such as

Shell script example

The following script will upload files from your computer to the remote server using the rsync command. First, make sure you setup the public key authentication:

# Purpose: Batch file uploader from my desktop to the remote server
# Author: nixCraft <> under GPL v2.x+
# -----------------------------------------------------------------
## static url mapping ##
## rsync args ##
_args="-aPv --ignore-existing"
## fail safe ##
[[ "$_files" == "" ]] && { echo "Usage: $0 file1.png file2.png"; exit 0; }
## Let us start uploading files. Bash for loop allows us to do a few more additional things, 
## but I am keeping it simple here for demo purpose
for i in $_files
        # make sure $i file exist #
        if [ -f "$i" ]
                rsync "$_args" "$i" ${_user}@${_server}:${_dest}
                echo "Error $i file not found."
if [ $_isuploaded -eq 1 ]
        echo "CDN urls:"
        echo -e "$_cdnurls"

You can use the script as follows:

./script foo.png
./script foo.png bar.png
./script *.mov
./script test.gif

Sample outputs:

sending incremental file list
         950 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)
sent 1053 bytes  received 31 bytes  197.09 bytes/sec
total size is 950  speedup is 0.88
CDN urls:

Run it again:

./script test.gif

Sample outputs:

sending incremental file list
sent 60 bytes  received 12 bytes  13.09 bytes/sec
total size is 950  speedup is 13.19
CDN urls:
See also

If anyone knows a better solution to this FAQ please share in the comments below.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • giel May 16, 2013 @ 18:03

    use rsync

  • jet May 20, 2013 @ 19:45

    mount the resource using sshfs and use regular file manager (if you do stuff manually)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum