copy_file_range - Linux


copy_file_range is a Linux command used to efficiently copy data between two open file descriptors. It’s particularly useful for performing quick and optimized file-to-file transfers.


copy_file_range(fd_in, offset_in, fd_out, offset_out, len)

| Parameter | Description |
| fd_in | File descriptor of the source file |
| offset_in | Offset within the source file from where to start copying |
| fd_out | File descriptor of the destination file |
| offset_out | Offset within the destination file where to copy data |
| len | Number of bytes to copy |


This command does not support any options or flags.


Example 1: Copying a File to Another File

Copy the contents of source.txt to destination.txt, starting from byte 0 in both files:

copy_file_range(fd_in=open("source.txt", "r"), offset_in=0, fd_out=open("destination.txt", "w"), offset_out=0, len=os.path.getsize("source.txt"))

Example 2: Copying a Range of Data

Copy 100 bytes from offset 100 in source.txt to offset 200 in destination.txt:

copy_file_range(fd_in=open("source.txt", "r"), offset_in=100, fd_out=open("destination.txt", "w"), offset_out=200, len=100)

Common Issues

  • Invalid File Descriptor: Ensure that both fd_in and fd_out point to valid open file descriptors.
  • Invalid Offset: Verify that the provided offsets are within the bounds of the respective files.
  • Insufficient Permissions: Check if you have write permissions to the destination file and read permissions to the source file.


copy_file_range can be used in combination with other Linux commands for advanced tasks:

  • dd: Replicate file contents alongside performing other operations (e.g., conversion, filtering):
dd if=/dev/zero bs=1024 count=1000 conv=sync,notrunc | dd of=/dev/sda2 conv=fdatasync,nocreat
  • cat: Concatenate files while leveraging copy_file_range for optimized copying:
cat file1 file2 file3 | copy_file_range fd_in=- fd_out=4 offset_out=0 len=`cat file1 file2 file3 | wc -c`

Related Commands

  • sendfile: Transfers data between sockets and files more efficiently than read() + write().
  • splice: Transfers data between pipe buffers, similar to copy_file_range but for pipes.