Transferring files between systems is a common task for developers and administrators. Using scp (secure copy) allows for secure file transfers over SSH, but copying just the files inside a directory—without copying the directory itself—requires careful command usage. This guide explains how to copy all files from a local directory to a remote location, ensuring the target folder receives only the files and not an additional directory layer.

Copying All Files Within a Directory Using SCP

Step 1: Open your terminal or command prompt on the system where the files are located. Ensure you have SSH access to the remote server and that scp is installed on your machine. You can verify scp by running:

scp -V

This displays the version if scp is installed. If not, install it using your system's package manager.

Step 2: Navigate to the parent directory of the folder containing the files you want to transfer. For example, if your files are in /home/user/myfiles/, use:

cd /home/user/myfiles

Step 3: Use the following scp command to copy all files (but not the directory itself) to the remote server. The * wildcard selects all files in the current directory:

scp * username@remote_host:/path/to/remote/destination/

Replace username with your remote login name, remote_host with your server's address, and /path/to/remote/destination/ with your target directory. This command copies all files (not subdirectories) from your current location directly into the remote destination folder.

Step 4: To include hidden files (those starting with a dot), modify the command to:

scp ./* .* username@remote_host:/path/to/remote/destination/

Be cautious: .* matches . and .. (current and parent directory), which you typically do not want to copy. To avoid this, use:

scp ./* ./.??* username@remote_host:/path/to/remote/destination/

The pattern .??* matches hidden files with at least two characters after the dot, skipping . and ...

Step 5: If you need to copy all files and subdirectories (but not the parent directory itself), add the -r flag for recursive copying:

scp -r * username@remote_host:/path/to/remote/destination/

This will copy all files and subdirectories inside your current directory, but still not the parent directory itself.


Alternative: Using Quoting to Avoid Shell Expansion Issues

Shell expansion can sometimes fail if there are too many files or special characters. To avoid this, you can use quotes and the find command with xargs:

find . -maxdepth 1 -type f -print0 | xargs -0 scp -t username@remote_host:/path/to/remote/destination/

This approach is more robust for directories with a large number of files or unusual filenames. -maxdepth 1 limits the search to the current directory, and -type f selects only files.


Copying Files with Specific Extensions

If you want to copy only files of a certain type (for example, all .txt files), specify the pattern:

scp *.txt username@remote_host:/path/to/remote/destination/

This command copies only the files matching the pattern to the remote directory.


Transferring files with scp in this way keeps your remote directory organized and avoids unnecessary directory nesting. Always double-check your destination path and file selection patterns to prevent accidental overwrites or missed files.