How to Use Wildcards (*) When Copying with scp?

The scp is a command line utility mainly used for copying files or directories to a remote system from the local system. This command requires users to log in to the remote system using ssh. The scp uses ssh for secure data transfers. It also supports wild cards such as an asterisk (‘*’) representing multiple files or directories. Now how would you use the wildcards (*) while copying using scp on Linux?

This guide explains the use of wildcards (*) that are used to copy multiple files and directories on Linux.

  • What are Wildcards in scp Command?
  • How to Use Wildcards (*) When Copying with scp?
    • Copy Multiple Files
    • Copy the Whole Directory

What are Wildcards in scp Command?

A wildcard is a set of symbols representing other characters that substitute any string or character. The asterisk (‘*’) represents all characters, and in the case of the scp command, it means multiple files or directories which are specified, followed by the *. Simply put, the asterisk (‘*’) lists all the files for the scp command to copy.

How to Use Wildcards (*) When Copying with scp?

To use the wildcard (‘*’) in scp, specify the source and destination paths, just like the scp command. The difference is that instead of specifying a single file or directory, use the wildcard (‘*’) to represent one or more files or directories.

To use the scp command, follow this syntax:

Syntax:

$ scp [option] [source-file-path] [user_name@target_host:path-to-target-directory]

In the above command we have:

  • scp to invoke the scp command
  • option represents the optional command line arguments to control that controls the command’s flow
  • source-file-path is the path of the source file
  • user_name@target_host is the username and IP address of the system of the target host
  • path-to-target-directory specifies the path where the input file is copied

To view the list of options, use this man command:

$ man scp

Users need to press the “Enter” key to scroll down.

Example 1: Copy Multiple Files Using the scp Command with Wildcards (*) on Linux

To copy the file having .txt extension from the local (home directory) to the remote directory /home/itslinuxfoss/folder, to the remote host “[email protected]use this command:

$ scp file*.txt [email protected]:/home/itslinuxfoss/rfolder/

The above image confirms the copying of all files having the “.txt” extension to the remote “/home/itslinuxfoss/folder/” directory. To verify it, let’s check the folder on the remote host:

$ ls -l /home/itslinuxfoss/rfolder

The above image verifies the successful copying of files.

Example 2: Copy the Whole Directory Using the scp Command with Wildcards (*) on Linux

Before copying, let’s view the contents of the directory that we are going to send “recursively” via the below commands:

$ ls Downloads
$ ls Downloads/Folder
$ ls Downloads/Folder/Folder1
$ ls Downloads/Folder/Folder2
$ ls Downloads/Folder/Folder3

The above image displays the of the Downloads directory and the directories within it, let’s copy them to a remote host using this command:

$ scp -r /home/itslinuxfoss/Downloads/* [email protected]:/home/itslinuxfoss/rfolder/

The above image confirms that the Downloads directory has been copied including sub-files and sub-directories. 

Here’s the view on the remote system:

$ ls -l /home/itslinuxfoss/rfolder

The above figure confirms that the data has been copied in the same hierarchy.

Conclusion

The wildcard asterisk (‘*’) is used to list all the files/directories with the scp command. Using the asterisk, users can copy multiple files and the contents of the whole directory, including all the subdirectories. befoIt requires an ssh server, so ensure it is installed on your system.

This guide shed light on how wildcards (*) can be used when copying with scp.