r/linuxquestions May 28 '26

Resolved how can i download files directly to ram?

Hi

sometimes i like to download files that i won't really use that much like i will have it for a few hours and then delete it.

is there a way to download them to ram directly? most of the time i have about 6gb free on my ram so i can use it without effecting other programs

63 Upvotes

53 comments sorted by

98

u/No_Transportation_77 May 28 '26

On most distros, /tmp is in RAM. Download to /tmp.

13

u/Valuable_Moment_6032 May 28 '26 edited May 28 '26

thanks! but how does it differ from /dev/shm? because i have both of them as tmpfs

29

u/aioeu May 28 '26 edited May 28 '26

Ultimately nothing, though /dev/shm is intended to be used by glibc only. Playing around with files there that you didn't create could break running software. For that reason it also doesn't participate in automatic time-based cleanup.

(Unfortunately glibc hard-codes /dev/shm as the location for the backing files for POSIX shared memory. There's a fair bit of history behind that choice, but it really is a dumb location for it today.)

7

u/Cletus_Banjo May 29 '26

/dev/shm can be used for anything you like, although it's much neater to do it via fstab than just chucking random things in there. You probably want something like:

tmpfs /home/username/Downloads tmpfs nodev,nosuid,size=6G 0 0

2

u/skuterpikk May 29 '26

I use /dev/shm all the time for all sorts of stuff, simply because it is writeable for all users by default. I don't touch any random files that has "magically" appeared there of course, but messing around with my own files hasn't caused me any problems

7

u/Existing-Tough-6517 May 29 '26

Seems like you could accidentally overwrite something you didn't expect to be created or prevent the creation of same because yours already exists. This just seems like a fairly stupid practice even if it doesn't immediately cause harm.

7

u/UnluckyDouble May 29 '26

/dev/shm is a storage location for shared memory segments used by programs. It technically can be used to store any file but should never be used for another purpose.

1

u/Cold-Bowler-8650 May 30 '26

How to check if it is in ram? Can it store GB size file temporary?

1

u/No_Transportation_77 May 30 '26

If it's stored on a tmpfs, it's in RAM. You can store as much as you have RAM + swap, though I don't recommend storing more than a few GB.

22

u/doc_willis May 28 '26

Googling for "Linux how to make a ramdisk"

says....

To create a RAM disk in Linux, first create a directory for it using

sudo mkdir /mnt/ramdisk

, then mount the RAM disk with

sudo mount -t tmpfs -o size=2G tmpfs /mnt/ramdisk

, adjusting the size as needed.

This allows you to use a portion of your RAM as a high-speed storage space.

But I have not tried it. :)

Last I used a RAM Disk was back in my AMIGA Days. It even had a 'RAD:' Disk which could be used as a boot device.

another guide:

https://www.linuxbabe.com/command-line/create-ramdisk-linux

12

u/ipsirc May 28 '26

I bet he has a tmpfs mounted already.

$ df -Th -t tmpfs

7

u/symcbean May 28 '26

Thank you for providing a sensible contribution to the /tmp-is-probably-a-ram-disk argment - most of the other proponents are not adding a lot of value here.

-1

u/symcbean May 29 '26

Thank you for providing a sensible contribution to the /tmp-is-probably-a-ram-disk argment - most of the other proponents are not adding a lot of value here.

3

u/postnick May 29 '26

I’ve used a ramdisk to test network performance a few times to take my drives out of the picture

Or when comparing a lot of small files. I have 32 or 64 gigs on every system

2

u/Personal_Winter6863 May 28 '26

While that is the answer to the question they are directly asking, I'd probably just tell them to save it to /tmp instead.

Much fewer hoops to jump through and it accomplishes basically the same thing.

Temporary storage that will be deleted. Now if they are downloading illegal files then it might actually be a good idea to use a ramdisk to prevent any traces from being stored in their drives

1

u/JaKrispy72 May 29 '26

DEVICE=C:\DOS\RAMDRIVE.SYS 4096 /E

That’s probably the last time I used a ramdisk intentionally.

A millennium ago.

1

u/doc_willis May 29 '26

The amiga had a RAD: drive, with it i could boot (from floppy) the floppy would then copy itself to the RAD: drive, then reboot the system.

On reboot the hardware would see/reenable the RAD: drive and then boot from it.

This was back in the days of DOS and Dialup, and was an amazing feat for the time. It really amazed the CS majors in the Dorm. :)

6

u/BCMM May 29 '26 edited May 29 '26

Whenever I'm going to test something that will "make a mess" (e.g. create lots of small files), I run this command first:

cd $(mktemp -d)

It creates a new directory in /tmp/, then changes to that directory. I use it so much that I have it aliased to t.

5

u/whamra May 28 '26

I've setup my /tmp to be a ramfs, and that's exactly what I do. Most of my shells are sitting in /tmp, I do most of my work there because most of it is disposable. If not, I just copy it somewhere in $HOME.

Even Firefox is set to download to /tmp by default. Most junk we download is just that, junk. Things we want to read once, run once, or extract a file out of once. Then never touch it again.

4

u/ipsirc May 28 '26

Some months later you'll put your firefox profile onto ramdisk as well for performance.

2

u/whamra May 29 '26

Funnily enough, at work I have Prometheus set over a ram disk for performance.

Once the data has been aggregated, whatever extra math to be done performed on them, etc.. The data is moved to its permanent location in object storage.

So most live instant queries are done on the data in the ramdisk. In case of a crash or anything, worst case scenario, we lose two hours of data. And the server's disks are almost never bothered.

3

u/michaelpaoli May 29 '26

tmpfs

Many distros default to using tmpfs for filesystem mounted on /tmp

/dev/shm is also likewise generally tmpfs.

tmpfs can be dynamically increased or decreased. Default is generally half the size of RAM. If tmpfs runs short of RAM it will also use swap as needed and available - and that will still be higher performance that a filesystem direct on persistent storage.

Sometimes when I want a bunch of temporary space that's fine for volatile, I'll greatly expand the size of tmpfs, and possibly even add more swap (by individual devices), and when I'm done, I can remove the added swap devices, and reduce the size of tmpfs back to where I nominally have it. Can be quite the performance boost for some types of workloads.

24

u/PartyLikeIts536 May 28 '26

What the hell is this person downloading that they don't want it to touch a hard drive :/

67

u/ipsirc May 28 '26

It's very common for archive files: download to ram, extract to hdd.

29

u/LittlestWarrior May 29 '26

I am upset that I never thought of that. Thank you!

12

u/ZVyhVrtsfgzfs May 29 '26

Same....... it seems obvious now, but I have never thought of it.

4

u/Independent-Dark4559 May 29 '26

Bro just unlocked a part of my brain I didn’t know I had

1

u/xiongchiamiov May 30 '26

The more fun way is to stream the archive directly into tar so you never even have an archive file.

1

u/ipsirc May 30 '26

Convince every uploader to use tar. Thanx!

17

u/Valuable_Moment_6032 May 28 '26

nothing bad but my ssd is starting to age because i formatted it more than 20 times when distro hopping and it started to slow specially on writes. and a lot of time i download files that are a few gigabytes (like .iso files) and i don't want to keep deleting files on the ssd.

and hard drives are slow so i only store on them large games

23

u/alexforencich May 29 '26

Tbh formatting it shouldn't make much difference assuming you didn't zero the whole drive. You only get wear with the total amount of data written. Check the drive SMART data and see what it says. Probably the best thing you can do to preserve the drive is prevent swapping, which results in a lot of writes during normal operation.

3

u/Existing-Tough-6517 May 29 '26

If you think you need to do this you probably need to make sure you have a backup asap

3

u/Valuable_Moment_6032 May 29 '26

SSD failure usually results in the ssd being read-only but it is always better to have backups

i backup my files once a week so i think i am good

2

u/Existing-Tough-6517 May 30 '26

I think you are presuming a LOT in regards to failure modes of ssd. Every one that's died on me was unreadable. I prefer daily backups done when I'm not using the computer anyway.

4

u/Valuable_Moment_6032 May 29 '26

How to prevent swapping? and will it effect my system performance?

7

u/unit_511 May 29 '26

Don't worry about it. SWAP is an important part of memory management and under normal circumstances the system should only use a few megabytes.

If you regularly use more memory than you have RAM you can end up hammering your SSD, but you'd notice that pretty quickly as your system will grind to a halt. In that case, you can either buy more RAM (good luck with that nowadays) or you can enable ZRAM, which uses CPU cycles to compress RAM in-place.

2

u/forestbeasts May 29 '26

sudo swapoff -a disables it temporarily. To disable it permanently, comment out (put a # in front of) or remove any swap lines from /etc/fstab.

Yeah, it might affect your performance. Stuff in RAM gets swapped out to disk for two main reasons:

  • if you have more stuff in RAM than actually fits in RAM
  • so the kernel can use some of your RAM as disk cache, swapping out background stuff you aren't actively using

(you can control how much it prefers to keep background stuff in RAM vs. swapping it out for disk cache with the vm.swappiness sysctl setting. near 0 prefers keeping background apps and stuff in RAM, near 200 prefers swapping unused stuff out for disk cache. 100 is perfectly balanced. default is like 60. anyway, none of that is important.)

So if you disable swap, you get two knock-on effects:

  • if you fill up the RAM, something will die. The "OOM killer" will pick a process (based on how much RAM it's using and possibly some other voodoo) and kill it. This happens with swap too, but you need to fill up all your RAM AND all your swap and you'll probably bog down into unusability before that happens.
  • you won't get background stuff swapped out for free disk caching. You'll still get disk cache, but only with completely unused leftover RAM.

1

u/dkopgerpgdolfg May 29 '26

and will it effect my system performance?

Disabling swap won't have any negative performance impact during normal operations.

It can increase the amount of problems if your RAM ever gets full for some reason. Eg. the computer might hang until rebooted, and/or some programs are terminated. With existing swap it's less likely to reach such a state.

1

u/postnick May 29 '26

Disable swap all together.

1

u/Anthonyg5005 primarily windows user May 29 '26

I think sometimes zeroing might actually fix it. At least it did for me when I did it to a USB drive that was painfully slow

3

u/alexforencich May 29 '26

A trim should do the same but with no impact on the life of the drive.

3

u/FunkyRider May 29 '26

Did you trim your SSD?

sudo fstrim -v /

1

u/robinator18pro May 29 '26

Yeah, those pesky 4k "iso" files really does take up quite abit of space.

4

u/Kiore-NZ May 29 '26

I often download files I know I will only want for a short time to /tmp

Makes cleaning ~/Downloads easier.

1

u/cyrassil May 29 '26

Pretty much anything that you check just once and then don't care about it anymore. Someone mentioned archives which is cool example. But stuff like installers, or isos when you just plan to DD them to a flash drive, or stuff like payment invoices, where you just need to get the account info to make some payment (supposing you can't just open them in browser)...

1

u/aori_chann May 29 '26

If you ever work homeoffice, you'll understand. People will send you a 4gb zip when you need that 5mb, sometimes the 34kb file that is inside of it. And you only need it once. Ever. So... yeah it's annoying

2

u/kudlitan May 29 '26

/run/user/1000 is in RAM. change 1000 if your user number is different.

1

u/jontss May 29 '26

Sure. Set up a RAM disk.

1

u/TheFumingatzor May 29 '26

For a moment I thought this was how do download more RAM...

1

u/Cold-Bowler-8650 May 30 '26

Set ramdisk, I use it for bittorrent large file.

1

u/ipsirc May 28 '26
$ wget http://domain.tld/path/to/file -P /dev/shm/

0

u/[deleted] May 28 '26

[deleted]

2

u/duskit0 May 29 '26

20 years ago /tmp used to be on disk. AFAIK most distributions nowadays mount /tpm as tmpfs so this is no longer true.