10 examples of `scp` (secure copy) commands in Linux, along with explanations for each:
1. Copy a local file to a remote server:
scp /path/to/local/file.txt user@remote-server:/path/on/remote/
This command copies the local `file.txt` to the remote server at the specified path.
2. Copy a remote file to a local directory:
scp user@remote-server:/path/to/remote/file.txt /path/on/local/
This command copies the remote `file.txt` to the local directory at the specified path.
3. Copy an entire directory to a remote server:
scp -r /path/to/local/directory/ user@remote-server:/path/on/remote/
The `-r` flag indicates recursive copying, allowing you to copy the entire local directory to the remote server.
4. Copy a remote directory to a local directory:
scp -r user@remote-server:/path/to/remote/directory/ /path/on/local/
Similar to the previous example, this command copies a remote directory to a local directory using the `-r` flag.
5. Copying with a specific SSH key:
scp -i /path/to/private/key.pem /path/to/local/file.txt user@remote-server:/path/on/remote/
If you want to use a specific private key for the SSH connection, you can use the `-i` flag followed by the path to the private key.
6. Copying using a specific port:
scp -P 2222 /path/to/local/file.txt user@remote-server:/path/on/remote/
The `-P` flag specifies the port to be used for the SSH connection. In this example, the port is set to `2222`.
7. Copying multiple files with a wildcard:
scp /path/to/local/*.txt user@remote-server:/path/on/remote/
You can use wildcards like `*` to match multiple files and copy them to the remote server.
8. Copying with progress information:
scp -r /path/to/local/directory/ user@remote-server:/path/on/remote/ -v
The `-v` flag adds verbose output, showing the progress of the copy operation.
9. Copying between two remote servers:
scp user@source-server:/path/to/source/file.txt user@destination-server:/path/on/destination/
You can use `scp` to directly transfer files between two remote servers.
10. Copying with compression:
scp -rC /path/to/local/directory/ user@remote-server:/path/on/remote/
The `-C` flag enables compression during the copy process, which can speed up the transfer of large files or directories.
Remember to replace `/path/to/local` and similar placeholders with the actual paths you want to use in your specific scenarios, and replace `user`, `remote-server`, and other placeholders with your actual remote server details.
No comments:
Post a Comment