How to make scp copy hidden files?

89,969

Solution 1

That should absolutely match hidden files. The / at the end of the source says "every file under this directory". Nevertheless, testing and research bear you out. This is stupid behavior.

The "answer" is to append a dot to the end of the source:

scp -rp src/. user@server:dest/

The real answer is to use rsync.

Solution 2

You can try rsync. It's better suited for this job:

rsync -av src/ user@server:dest/

(And its manual page is worth reading.)

Solution 3

Don't put a slash after the source directory. Your code would look like this:

scp -rp src user@server:dest/

This will create a directory 'src' under 'dest' on the remote machine, with all the hidden files included. It's probably not exactly what you want, but it will copy hidden files in src.

Solution 4

To copy only hidden files, Use this command

scp -rp /path_to_copy_hidden/.[!.]* user@host:/path_to_paste/

Actual game is the /.[!.]* tag that is referring to files starting with .(hidden)

Solution 5

The following will solve the problem, this has been fully tested on our continuous integration environment

scp -rp src/. user@server:dest/
example scp -rp /usr/src/code/. [email protected]:/usr/dest/code/

Hope it helps

Share:
89,969

Related videos on Youtube

rascher
Author by

rascher

Updated on September 17, 2022

Comments

  • rascher
    rascher almost 2 years

    I often use SCP to copy files around - particularly web-related files. The problem is that whenever I do this, I can't get my command to copy hidden files (eg, .htaccess).

    I typically invoke this:

    scp -rp src/ user@server:dest/
    

    This doesn't copy hidden files. I don't want to have to invoke this again (by doing something like scp -rp src/.* ... - and that has strange . and .. implications anyway.

    I didn't see anything in the scp man page about an "include hidden files".

    How can I accomplish this?

    • Ken Sharp
      Ken Sharp over 8 years
      I assume that src/.* also copies src/.. (the parent directory), right?
  • user649102
    user649102 about 15 years
    Nice trick I did not realized this yet.
  • bakwarte
    bakwarte about 15 years
    I always use the --progress option for rsync, I can't live without it =D
  • Steve Townsend
    Steve Townsend about 15 years
    rsync -avz -e ssh --progress src/ user@server:dest/
  • Mikel
    Mikel over 13 years
    I can't reproduce that behavior. scp -r source/ host:source2 copies dot files. Works in OpenSSH 5.1 from 2007.
  • user649102
    user649102 about 12 years
    @Hofa I usually use -P because it is shorter, already includes --progress and also includes --partial which can make sense if I am already interested in its progress ;-)
  • Moritz Friedrich
    Moritz Friedrich over 5 years
    After updating scp recently, this now results in scp: error: unexpected filename: . . Make sure to test whether it works with your distribution, fellow Googler :)
  • Stéphane
    Stéphane over 5 years
    Note that adding a . at the end no longer works (2019) due to security issues. This is explained here: superuser.com/questions/1403473/scp-error-unexpected-filenam‌​e
  • Brain90
    Brain90 over 4 years
    Use this if you need different port : rsync -av -e "ssh -p 2222" src/ user@server:dest/