How to use scp/rsync to copy file with a colon (:) in it

How do I copy a file with a colon punctuation (:) name in it. When I type the following command to copy “file:name.txt” to a remote host called server1:

scp file:name.txt user@server1:/path

I get the following error:

ssh: Could not resolve hostname s: Name or service not known

How do I copy file with scp or rsync with a colon in it?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements scp/rsync/ssh
Est. reading time 2 mintues
Local file names can be made explicit using absolute or relative pathnames to avoid scp treating file names containing : as host specifiers. In other words use the following syntax and put ./ before your file names:
scp ./file:name.txt user@server1:/path/to/dest
# another option to state full path #
scp /path/to/src/file:name.txt user@server1:/path/to/dest
rsync /path/to/src/file:name.txt user@server1:/path/to/dest


In this example, I am coping a file called mysql-blog-db.23-02-2014-11:10:06.gz to a server called backup-test-server13
rsync 'mysql-blog-db.23-02-2014-11:10:06.gz' vivek@backu-test-server13:~

Sample error/outputs:

The source and destination cannot both be remote.
rsync error: syntax or usage error (code 1) at /SourceCache/rsync/rsync-42/rsync/main.c(1083) [receiver=2.6.9]

You will get same result with scp command

scp 'mysql-blog-db.23-02-2014-11:10:06.gz' vivek@backu-test-server13:~

Sample error/outputs:

ssh: Could not resolve hostname mysql-blog-db.23-02-2014-11:10:06.gz: nodename nor servname provided, or not known

How do I scp/rsync a file name with a colon?

In this example, I am coping a file called mysql-blog-db.23-02-2014-11:10:06.gz to a server called backup-test-server13 using scp command:

scp ./mysql-blog-db.23-02-2014-11\:10\:06.gz vivek@backu-test-server13:~

OR

scp /tmp/mysql-blog-db.23-02-2014-11\:10\:06.gz vivek@backu-test-server13:~

OR pass some additional options:

scp -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i /Users/vivek/.ssh/my-keys -P 22 ./mysql-blog-db.23-02-2014-11\:10\:06.gz vivek@backu-test-server13:~

Sample outputs:

Warning: Permanently added '173.xx.yy.zz' (RSA) to the list of known hosts.
mysql-blog-db.23-02-2014-11:10:06.gz

Here is syntax for rsync command:

rsync -av ./foo:bar:file.txt user@server1.cyberciti.biz:/tmp
rsync -av $HOME/data/foo:bar:file.txt user@server1.cyberciti.biz:/tmp

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

🐧 14 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
14 comments… add one
  • Silver Knight Feb 16, 2011 @ 7:12
    Two other methods that have worked well for me in the past regarding this issue...
    
    1) "Escape" the special character(s) using your shell's preferred escape character.  As far as I know, most shells accept a backslash as an escape character.
    
    Example:
    scp file\:name.txt user@server1:/path
    
    2) Enclose the "offending" path in double quotes.  This is especially useful for filenames which contain many spaces and other special characters.
    
    Example:
    scp "file:name.txt" user@server1:/path
    • 🐧 nixCraft Mar 9, 2011 @ 11:48

      scp / rsync command will not work with ‘Escape’ and “double quote” syntax as as those command treat : as host specifiers. As soon as scp see : it assumes a network based host.

      • Thomas D. May 1, 2011 @ 14:27

        Scp and rsync do work with ‘Escape’ and “double quote” syntax. I tested on Fedora 14.

        • Freaky Mar 29, 2013 @ 14:25

          Dear guru, please provide us your command line for escaping : in a filename then.

          Yes, scp “file” host:/path works fine. scp file:withcolon host:/path however does not and neither does file\:withcolor host:/path. So whilst the command can *use* escaping/quoting it doesn’t change the handling of the colon. Perhaps if you escaped it with a ton of blackslashes it will – but seriously – who wants to do that?

          • Lal Ashish Jan 20, 2014 @ 11:15

            It worked for Me when I gave the full path

  • John G Mar 7, 2011 @ 0:26

    Careful if using double quotes, as certain special characters inside the quoted text will still be interpreted by the shell, like ‘$’ and ‘!’ among others. If using quotes, single quotes is better unless you already have single quotes in the text to quote.

    If you’re using bash or csh or any other recent shell that has filename auto-completion, you can probably press TAB key (or maybe ESC, but in ubuntu and most others recently they use bash which defaults to TAB key). This will complete the filename for you (or as much as it can if there are multiple similar filenames in the same directory) and use quotes or slashes to escape the characters as necessary.

  • Henrik Mar 11, 2011 @ 13:54

    As long as you have the : on the local system you can either use backslash to quote it or single quotes.
    Where I havn’t found a solution yet, is when the remote target path (not filename) contains a colon.

    rsync /path/file.txt user@host:/remotes/x:y/file.txt
    
    • Henrik Mar 11, 2011 @ 17:11

      please delete my comment … problem found between keyboad and chair ;-)

  • Adam Ziaja Aug 14, 2011 @ 18:29

    or just \:

  • Martin Skarsaune Oct 30, 2011 @ 18:42

    Hi

    My problem is that I recursively backup a directory structure (iPhoto library) from macos to Linux. During the processing it encounters subfolders with colons on the remote host and fails. Is there any way around this?

  • Judith James Dec 13, 2011 @ 13:41

    Adding ./ before my filename worked like a charm. It was driving my nuts on my MacOS X:
    scp ./file-2011-12-11T12:13:42 nasbox1:/backups/test

  • mARK Jun 13, 2012 @ 22:32

    quoting or escaping the colon won’t work because the shell still passes a plain colon to scp. something like this does work:
    scp ./as:df localhost:qw:er
    that copies a file called ‘as:df’ in your current directory to a file called ‘qw:er’ in your home directory. of course, you can use any host name.

  • Leo Jan 13, 2013 @ 17:32

    I’m also trying to rsync recursively an entire linux home directory to a backup drive connected to my router. The backup drive is mounted using mount.cifs, and is an NTFS filesystem. The errors I get are of the form

    rsync: mkstemp “/media/backup/E6520_Backup/simon/localsvnLogs/.Log_Fri_Dec_21_19:30:59_CST_2012.W95vF6” failed: Not a directory (20)

    Since I regularly identify files by date, thousands of them have colons in them.

    Is there any way around this, eg.., maybe by formating the backup drive with a different filesystem?

  • GTFO Feb 23, 2014 @ 18:00

    Why this is categories under OpenBSD?

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