How to Transfer Files in Linux

How to Transfer Files in Linux

File transfer is one of the most common tasks you’ll perform in Linux. Whether you are moving files between directories on the same machine, sharing files with other users on the network, or uploading files to a remote server, Linux offers a wide array of tools and commands to get the job done. This guide will walk you through various methods for transferring files in Linux, covering both basic and advanced techniques.

1. Copying Files Locally Using cp Command

The cp command is the most basic and commonly used command for copying files and directories within your local file system.

Basic Syntax:

cp [options] source destination 
  • source: The file or directory you want to copy.
  • destination: The location where you want to copy the file or directory.

Example:

To copy a file named example.txt from your current directory to another directory, use:

cp example.txt /path/to/destination/ 

To copy an entire directory, use the -r (recursive) option:

cp -r /path/to/source_directory /path/to/destination_directory/ 

2. Moving Files Locally Using mv Command

The mv command is used to move files or directories from one location to another. It can also be used to rename files.

Basic Syntax:

mv [options] source destination 

Example:

To move a file named example.txt to a different directory:

mv example.txt /path/to/destination/ 

To rename example.txt to new_example.txt, use:

mv example.txt new_example.txt 

3. Transferring Files Over a Network Using scp Command

The scp (secure copy) command is used to transfer files securely over a network using SSH.

Basic Syntax:

scp [options] source user@host:destination 
  • user: The username on the remote system.
  • host: The IP address or domain name of the remote system.
  • destination: The path on the remote system where you want to transfer the file.

Example:

To copy a file named example.txt to a remote server:

scp example.txt user@192.168.1.10:/remote/directory/ 

To copy an entire directory, use the -r option:

scp -r /local/directory/ user@192.168.1.10:/remote/directory/ 

4. Using rsync for Efficient File Transfers

The rsync command is a powerful tool for syncing files and directories between two locations. It only copies the differences between the source and destination, making it faster and more efficient.

Basic Syntax:

rsync [options] source destination 

Example:

To sync a local directory with a remote directory:

rsync -avz /local/directory/ user@192.168.1.10:/remote/directory/ 
  • -a: Archive mode (preserves symbolic links, permissions, etc.).
  • -v: Verbose (displays progress).
  • -z: Compresses data during the transfer.

To delete files in the destination that don’t exist in the source:

rsync -avz --delete /local/directory/ user@192.168.1.10:/remote/directory/ 

5. Transferring Files Using sftp

The sftp (SSH File Transfer Protocol) command provides an interactive interface for transferring files over SSH.

Basic Syntax:

sftp user@host 

Example:

To start an sftp session with a remote server:

sftp user@192.168.1.10 

Once connected, use put to upload files or get to download files:

put /local/file.txt /remote/directory/ get /remote/file.txt /local/directory/ 

You can exit the session using:

bye 

6. Transferring Files Using rsync with SSH

rsync can also be combined with SSH for secure file transfers. This is often more efficient than scp for large or frequent transfers.

Basic Syntax:

rsync -avz -e ssh /local/directory/ user@192.168.1.10:/remote/directory/ 

Example:

rsync -avz -e ssh /local/file.txt user@192.168.1.10:/remote/directory/ 

This command transfers file.txt securely over SSH.

7. Using FTP for File Transfers

For situations where SSH is not available, ftp (File Transfer Protocol) can be used. However, ftp is less secure than SSH-based methods.

Basic Syntax:

ftp host 

Example:

To connect to an FTP server:

ftp 192.168.1.10 

After logging in, use the following commands:

  • put: Uploads a file.
  • get: Downloads a file.
  • mput: Uploads multiple files.
  • mget: Downloads multiple files.

To upload a file:

put /local/file.txt 

To download a file:

get /remote/file.txt 

8. Using curl and wget for Downloading Files

curl and wget are command-line tools used to download files from the internet.

Using curl:

curl -O http://example.com/file.txt 
  • -O: Saves the file with its original name.

Using wget:

wget http://example.com/file.txt 

Both tools can download multiple files by specifying multiple URLs:

wget http://example.com/file1.txt http://example.com/file2.txt 

9. Transferring Files Between Linux Systems Using NFS

Network File System (NFS) allows you to share directories between Linux systems.

Step 1: Install NFS

On the server:

sudo apt-get install nfs-kernel-server 

On the client:

sudo apt-get install nfs-common 

Step 2: Configure NFS

On the server, edit the /etc/exports file to specify the directory to share:

/path/to/share client_ip_address(rw,sync,no_subtree_check) 

Apply the changes:

sudo exportfs -a 

Restart the NFS service:

sudo systemctl restart nfs-kernel-server 

Step 3: Mount the NFS Share

On the client, create a mount point:

sudo mkdir -p /mnt/nfs_share 

Mount the shared directory:

sudo mount server_ip_address:/path/to/share /mnt/nfs_share 

10. Using rsync for Backups

rsync is also an excellent tool for creating backups.

Example:

To backup a directory to an external drive:

rsync -avz /home/user/ /mnt/external_drive/backup/ 

This command creates an exact copy of your home directory on the external drive.

11. Transferring Files Using tar and gzip

For transferring multiple files or directories, it’s often convenient to compress them into a single archive using tar and gzip.

Creating an Archive:

tar -cvzf archive_name.tar.gz /path/to/directory/ 
  • -c: Creates a new archive.
  • -v: Verbose (lists files as they are archived).
  • -z: Compresses the archive using gzip.
  • -f: Specifies the name of the archive.

Extracting an Archive:

tar -xvzf archive_name.tar.gz 
  • -x: Extracts the archive.

12. Transferring Files Using netcat

netcat is a versatile tool for transferring files between two Linux systems without a third-party service.

On the receiving end:

nc -l -p 12345 > received_file 

On the sending end:

cat file_to_send | nc destination_ip 12345 

This method is particularly useful for large files or when you need a simple, direct transfer.

Conclusion

Transferring files in Linux offers a variety of tools, each suited to different scenarios. For local transfers, cp and mv are your go-to commands. For network transfers, scp, rsync, and sftp provide secure options. NFS is ideal for sharing directories between Linux systems, while tar and gzip simplify the process of transferring multiple files.

By mastering these tools, you’ll be well-equipped to handle any file transfer task in Linux, from the simplest copy operation to the most complex network transfer. Each tool has its strengths, so choose the one that best fits your needs.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: