Transferring Files Securely with scp

Walk through how to compress a directory and transfer it from a remote server to your local machine

July 06, 2025 Dykraf

In web development and server maintenance, transferring large sets of files between servers and local machines is a common task

Web Story VersionWeb Story

In web development and server maintenance, transferring large sets of files between servers and local machines is a common task. One of the most secure and straightforward tools for this is scp (Secure Copy Protocol). When combined with tar, which compresses directories into single files, the process becomes even more efficient.

In this tutorial, we’ll walk through how to compress a directory from a PHP Laravel project and securely transfer it from a remote server to your local machine.

Tools You'll Use

tar: For compressing directories into .zip or .tar.gz files.

scp: For securely copying files between local and remote systems over SSH.

Scenario

Let’s say you want to download the storage/app directory from a remote server located at IP address 11.11.111.11. Here's how to do it.

Step 1: Compress the Folder on the Remote Server

Before transferring, it’s a good idea to compress the folder to reduce size and simplify the transfer.

On the remote server, run:

tar -zcvf storage.zip /var/www/html/laravel/storage/app

Explanation:

tar: Archive command.

  • -z: Compress using gzip.

  • -c: Create a new archive.

  • -v: Verbose (shows the files being archived).

  • -f storage.zip: Output file.

/var/www/html/laravel/storage/app: The directory you want to compress.

This command creates a storage.zip file in the current working directory on the server.

Step 2: Securely Copy the File Using scp

Now use scp from your local machine to copy the compressed file:

scp user@11.11.111.11:/var/www/html/laravel/storage/storage.zip /Users/username

Explanation:

  • scp: Secure copy command.

  • user@11.11.111.11: Remote SSH username and IP.

  • /var/www/html/laravel/storage/storage.zip: Path to the file on the server.

  • /Users/username: Destination directory on your local machine.

After running this, you may be prompted to enter the password for the developer account on the server.

Step 3: Extract the Files Locally (Optional)

Once the storage.zip file is on your local machine, located in the /Users/username directory, you can extract it using:

tar -zxvf /Users/username/storage.zip

Alternative Method: Avoid Long Directory Paths After Unzipping

When you compress a directory like:

tar -zcvf storage.zip /var/www/html/laravel/storage/app

…it includes the entire directory path in the archive. This means when you extract it, you’ll get:

./var/www/html/laravel/storage/app/...

Not ideal if all you want is the contents of app/ in your current folder.

  • Cleaner Approach: cd First To avoid this, change into the directory that contains the folder you want to compress, and archive it from there.

    ssh user@11.11.111.11 cd /var/www/html/laravel/storage

    Now you're inside the storage folder.

  • Step 2: Create the Archive Without the Full Path

    tar -zcvf app.zip app

    This way, the archive will contain just the app/ folder and its contents — no full paths.

  • Step 3: Transfer the Archive to Your Local Machine

    From your local machine, run: scp developer@10.13.246.30:/var/www/html/laravel/storage/app.zip /Users/username

  • Step 4: Extract Locally

    cd /Users/username tar -zxvf app.zip

    Now you’ll simply get: ./app/...

    Much cleaner and more manageable!

Tips

  • Make sure the remote server has SSH enabled and you have proper access.

  • You can use .tar.gz or .zip depending on your preference (tar handles both with the right flags).

  • For frequent transfers, consider setting up SSH key-based authentication for password-less access.

Summary

To quickly and securely transfer a directory from a remote server to your local system:

  1. Compress the folder using tar.

  2. Transfer it using scp.

  3. Extract it locally if needed.

This method is ideal for backups, migrations, or syncing project data.

  • Two Ways to Archive
MethodResulting Folder StructureNotes
tar -zcvf file.zip /full/path/to/folder/full/path/to/folder/...Includes full directory tree
cd /path && tar -zcvf file.zip folderfolder/...Cleaner and shorter output

Using cd before archiving is a small change with a big impact when it comes to keeping your file structure clean and easy to work with.

Closing

Transferring files between servers and local machines is a common task, but doing it cleanly can save you time and frustration. Using tar to compress directories and scp to securely copy them is efficient—but if you're not careful, you may end up with deeply nested folders after extraction.

To avoid this, always cd into the parent directory of the folder you want to compress. This ensures your archive contains only what you need—no /var/www/html/... clutter. Whether you're backing up a Laravel storage folder or syncing files across environments, this simple adjustment makes your workflow cleaner and more professional.

Topics

Recent Blog List Content:

Archive