Skip to Content

How to Fix the “No space left on device” Error on Linux When Copying Files to a USB Drive

Learn how to fix the “No space left on device” error that occurs on Linux when copying files to a USB drive that has enough free space and how to avoid it in the future.

Problem

USB drives are convenient devices that allow you to store and transfer files between different computers. However, sometimes you may encounter an error that prevents you from copying files to a USB drive, even if the drive has enough free space. The error message is “No space left on device” and it usually appears when you use the cp or rsync commands on Linux. This error can be confusing and frustrating, as it seems to contradict the actual space available on the drive.

How to Fix the "No space left on device" Error on Linux When Copying Files to a USB Drive

In this article, we will show you how to fix the “No space left on device” error on Linux when copying files to a USB drive and how to avoid it in the future. We will also answer some frequently asked questions about USB drives and Linux file systems.

What Causes the “No space left on device” Error on Linux When Copying Files to a USB Drive?

The “No space left on device” error on Linux when copying files to a USB drive is caused by a mismatch between the file system of the USB drive and the file system of the Linux system. The file system is the way that the data is organized and stored on a device. There are different types of file systems, such as FAT, NTFS, ext4, etc., and each one has its own features and limitations.

The most common file system for USB drives is FAT, which stands for File Allocation Table. FAT is a simple and widely compatible file system that can be used on different operating systems, such as Windows, Mac, and Linux. However, FAT also has some drawbacks, such as:

  • It does not support files larger than 4 GB.
  • It does not support symbolic links, hard links, or permissions.
  • It does not support journaling, which is a feature that helps to prevent data corruption in case of a power failure or a system crash.
  • It has a limited number of inodes, which are the data structures that store the metadata of the files, such as the name, size, type, etc.

The last point is the main cause of the “No space left on device” error on Linux when copying files to a USB drive. An inode is a fixed-size data structure that occupies a certain amount of space on the device. Each file or directory on the device has a corresponding inode that stores its metadata. The number of inodes on a device is determined by the file system and the size of the device. For example, a FAT32 file system on a 32 GB device has about 4 million inodes.

When you copy files to a USB drive, you are not only using the space of the files, but also the space of the inodes. If you copy a large number of small files, you may run out of inodes before you run out of space. This is because each small file still needs an inode, which takes up a fixed amount of space. When the device runs out of inodes, it cannot create any new files or directories, even if there is still free space available. This is when you get the “No space left on device” error.

How to Fix the “No space left on device” Error on Linux When Copying Files to a USB Drive?

To fix the “No space left on device” error on Linux when copying files to a USB drive, you need to change the file system of the USB drive to a more suitable one that can handle a large number of small files and has more inodes. The most recommended file system for USB drives on Linux is ext4, which stands for Fourth Extended File System. ext4 is a modern and advanced file system that has many advantages over FAT, such as:

  • It supports files larger than 4 GB and devices larger than 2 TB.
  • It supports symbolic links, hard links, and permissions.
  • It supports journaling, which helps to prevent data corruption in case of a power failure or a system crash.
  • It has a dynamic number of inodes, which are allocated on demand and can grow as the device fills up.

To change the file system of the USB drive to ext4, you need to follow these steps:

  1. Backup the data on the USB drive to another location, as changing the file system will erase all the data on the device.
  2. Plug the USB drive into your Linux system and identify its device name, such as /dev/sdb or /dev/sdc. You can use the lsblk or fdisk -l commands to list the devices and their partitions.
  3. Unmount the USB drive if it is mounted, using the umount command, such as umount /dev/sdb1 or umount /media/usb.
  4. Format the USB drive with the ext4 file system, using the mkfs.ext4 command, such as mkfs.ext4 /dev/sdb1 or mkfs.ext4 -L usb /dev/sdb1. The -L option allows you to specify a label for the device, which can help you to identify it later.
  5. Mount the USB drive to a mount point, such as /media/usb, using the mount command, such as mount /dev/sdb1 /media/usb or mount -t ext4 /dev/sdb1 /media/usb. The -t option allows you to specify the file system type, which can help to avoid errors.
  6. Copy the data back to the USB drive from the backup location, using the cp or rsync commands, such as cp -r /home/user/backup/* /media/usb or rsync -a /home/user/backup/ /media/usb.

After changing the file system of the USB drive to ext4, you should be able to copy files to the device without getting the “No space left on device” error.

How to Prevent the “No space left on device” Error on Linux When Copying Files to a USB Drive?

To prevent the “No space left on device” error on Linux when copying files to a USB drive, you need to ensure that the file system of the USB drive is compatible and efficient for your data and your system. You can follow these tips to avoid the error in the future:

  • Use ext4 or another Linux-native file system for USB drives that you use mainly on Linux systems, as they offer better performance and reliability than FAT or NTFS.
  • Use FAT or NTFS for USB drives that you need to use on different operating systems, such as Windows or Mac, as they offer better compatibility and portability than ext4 or other Linux-native file systems.
  • Use a compression tool, such as zip or tar, to reduce the size and the number of files that you need to copy to the USB drive, especially if they are small or text-based files.
  • Use a split tool, such as split or 7z, to split large files into smaller chunks that can fit on the USB drive, especially if they are larger than 4 GB and you need to use a FAT file system.
  • Use a cleanup tool, such as bleachbit or fslint, to remove unnecessary or duplicate files from the USB drive, especially if you use it frequently and accumulate a lot of data.

Frequently Asked Questions (FAQs)

Question: How to check the file system and the inode usage of a USB drive on Linux?

Answer: To check the file system and the inode usage of a USB drive on Linux, you can use the df and the tune2fs commands. For example, to check the file system and the inode usage of the device /dev/sdb1, you can run the following commands:

df -Th /dev/sdb1

This command will show you the file system type, the size, the used space, the available space, and the mount point of the device. For example, you may see the output like this:

Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sdb1      ext4  29G   15G   13G  54% /media/usb

tune2fs -l /dev/sdb1

This command will show you the detailed information about the file system, including the number of inodes, the free inodes, and the inode size.

The Inode count field shows the total number of inodes on the device, the Free inodes field shows the number of unused inodes, and the Inode size field shows the size of each inode in bytes. For example, in this case, the device has 1900544 inodes, of which 1899507 are free, and each inode is 256 bytes.

Question: How to avoid the “No space left on device” error on Windows or Mac when copying files to a USB drive?

Answer: To avoid the “No space left on device” error on Windows or Mac when copying files to a USB drive, you need to use a file system that is compatible with your operating system and your USB drive. The most common file systems for USB drives on Windows and Mac are FAT and NTFS, respectively. However, these file systems also have some limitations, such as:

  • FAT does not support files larger than 4 GB or devices larger than 32 GB.
  • NTFS does not support symbolic links, hard links, or permissions, and it may have compatibility issues with some devices or applications.
  • Both FAT and NTFS have a limited number of inodes, which can cause the “No space left on device” error if you copy a large number of small files.

To avoid these limitations, you can use a third-party tool, such as Rufus or Disk Utility, to format your USB drive with a different file system, such as exFAT or UDF. These file systems have some advantages over FAT and NTFS, such as:

  • exFAT supports files larger than 4 GB and devices larger than 32 GB, and it is compatible with Windows, Mac, and Linux.
  • UDF supports files larger than 4 GB and devices larger than 2 TB, and it supports symbolic links, hard links, and permissions. It is also compatible with Windows, Mac, and Linux, but it may require some drivers or software to work properly.

However, these file systems also have some drawbacks, such as:

  • exFAT does not support journaling, which helps to prevent data corruption in case of a power failure or a system crash, and it may have performance issues with some devices or applications.
  • UDF may have compatibility issues with some devices or applications, and it may have performance issues with some operations, such as deleting files or directories.

Therefore, you should choose the file system that best suits your needs and preferences, and be aware of the pros and cons of each file system.

Summary

In this article, we have explained how to fix the “No space left on device” error on Linux when copying files to a USB drive and how to avoid it in the future. We have also answered some frequently asked questions about USB drives and file systems. We hope that this article has helped you to solve your problem and improve your data management and transfer.

Disclaimer: This article is for informational purposes only and does not constitute professional advice. We are not responsible for any damage or loss that may result from following the instructions in this article. Please use caution and backup your data before making any changes to your system.