1

I am using sort -k1,1 -k2,2 srcfile> tgtfile... But this is throwing the error "No space in device" as the src file contains 180 million rows in it.

How to fix this issue??

3
  • The error seems to be that the filesystem where you are trying to save tgtfile does not have enough space to save the file. Showing any information about the the space available on your devices (dh -h) could help someone in figuring out what the specific issue is. Commented May 22, 2023 at 17:18
  • Ah looked that up. A crore is 10⁷. I learned something today. Commented May 22, 2023 at 18:52
  • Hi @GracefulRestart there is space in the device...I have checked it has 16tb space left Commented May 23, 2023 at 4:26

1 Answer 1

2

GNU sort can sort files larger than what would fit in your RAM.

It does that by sorting a part of your input that does fit, writing the result to a temporary file, moving on to the next input part, sorting that, writing it to a temporary file and so on. Afterwards, it merges all the partial sorted files into one sorted output.

The file system that these temporary files are written to needs to be large enough to keep the whole content of the input file, potentially multiple times. GNU sort is not very smart about choosing the directory the temporary files are written to - it just uses the system default (typically, /tmp).

To mitigate your problem, tell sort to put the temporary files next to the target file (they will be automatically cleaned up, anyways), using

sort --temporary-directory=. -k1,1 -k2,2 -o tgtfile srcfile
4
  • Using $TMPDIR, falling back to system default like GNU sort does at least seems the smartest thing to do to me. I wouldn't want it to take it upon itself to start using file systems that have not been intended to store temporary data. Commented May 24, 2023 at 8:08
  • 1
    Note that it's not specific to GNU sort, the original Unix sort implementations from the 70s already stored temporary data to disk. Commented May 24, 2023 at 8:12
  • @StéphaneChazelas ah! Regarding specificity to GNU: Only had GNU sort on my phone, didn't want to write things only cobbled together from online documentation Commented May 24, 2023 at 15:40
  • Regarding "smartness in temporary storage choice": a detection of tmpfs mounts, combined with a warning that sorting into temporary files on a RAM-backed mount won't help when sorting files larger than RAM, would not be that unwelcome, I'd say - it's better to sort in swap directly then to run out of tmpfs space as you run out of RAM, of to swap tmpfs. Commented May 24, 2023 at 15:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.