Ignore Files when copying using scp

How to ignore certain files when using the scp command? I have the following file structure on the Linux server :

Everything >

  • NeedThis
  • NeedThisToo
  • DontNeedThis

I want to get NeedThis and NeedThisToo and ignore DontNeedThis. I can't just use two separate scps as in reality there are a few hundred files.

Neither can I move nor copy anything (part of this problem, can be seen as a set condition). Does anyone know a solution to my problem?

Answers 1

  • Using the rsync command in place of scp allows us to be more precise with what we want to exclude. Assuming you want to do a recursive copy of some remote directory to a local directory but want to exclude a name from the operation:

    rsync -av --exclude=DontNeedThis [email protected]:/some/dir/ /some/dir

    The above command would recursively copy the contents of the remote directory /some/dir into the local directory /some/dir, while avoiding anything named DontNeedThis (where DontNeedThis may be some quoted pattern).

    Note that the final / on the source path is significant. Without it, you'd copy the directory, not its contents.

    For a full explanation of the patterns that you may use to exclude or include things, see the section called "INCLUDE/EXCLUDE PATTERN RULES" in the rsync manual.

Related Questions