2

I have a script that creates a temp directory using mktemp -d.

A folder generated in the temp directory is the output of the script that will be copied to another part of the machine.

I was considering using ln to use the same folder instead copying the contents somewhere else. I was wondering if it would still be around if the version of the folder in the /tmp directory got cleaned up by the OS?

  • 1
    were you going to use a hard link or a soft link (ln -s)? – strugee Jun 26 '14 at 18:00
  • 1
    Instead of cp or ln, why not mv? – LatinSuD Jun 26 '14 at 18:06
  • @strugee Most systems do not allow hard links to directories. – jw013 Jun 26 '14 at 18:09
  • I am actually using mv, I was just interested. They are hard links. I kind of figured soft links would not. – Tormyst Jun 26 '14 at 18:09
  • @jw013 I know. it was still ambiguous. – strugee Jun 26 '14 at 18:18
3

It depends on where your temporary directory is. That is, have you created your own temporary directory, or are you using the system's (/tmp)?

In your scenario, you are expecting the files/folder to remain after the temporary directory has been cleaned up.

If it's in the system's /tmp directory then it may well be cleaned up by the system (it's distro specific, but most have a cron job or similar).

Additionally, a few distros create their /tmp directory using tmpfs which means that the contents held in RAM/Swap and don't survive a reboot.

The files will only remain accessible if you create a hard link. However, hard links can only be created within a single mounted filesystem. You cannot create a hard link between a tmpfs /tmp to a (eg) ext4 filesystem mounted on /mystuff.

You can create a soft link from /mystuff to somewhere on a tmpfs mounted at /tmp but when the temp files are deleted the link will point to 'nowehere'; which defeats the object slightly!

If your distro has it's /tmp files on a physical disk which is on the same mount as the location you plan to store your files (/mystuff), then a hard link would work as long as the link is created before the system cleans up /tmp.

1

I have made an example to make things much more clearer.

Inside a particular directory, I have created 2 files.

cat sourcefile
This is the sourcefile. 
##This is to create a hard link. 

ln sourcefile destfile

##List the files available. 
ls
destfile  sourcefile

cat destfile
This is the sourcefile. 

Now assuming you use hardlinks like above to refer to your files inside /tmp directory, let's see what happens if I delete the source file.

rm sourcefile
rm: remove regular file `sourcefile'? y
ls
destfile
cat destfile
This is the sourcefile

In the above scenario, the destfile is still accessible.

Now, let us see what happens if we use softlinks.

cat sourcefile
This is the sourcefile for softlink example. 
ln -s sourcefile destfile
ls -l
total 4
lrwxrwxrwx 1 root root 10 Jun 26 13:40 destfile -> sourcefile
-rw-r--r-- 1 root root 46 Jun 26 13:40 sourcefile

Now, as per your scenario let us delete the source file and see what happens.

rm sourcefile
rm: remove regular file `sourcefile'? y
ls
destfile
cat destfile
cat: destfile: No such file or directory

So if you use soft link, the data might not be there.

EDIT

I used the mktemp to create a temporary file and created a hard link from /tmp directory to one of my working directories.

I restarted the machine to check if the contents are available after reboot as well. If I use hard links, the contents are available after reboot as well. The reason for checking this is while rebooting the /tmp directory normally gets cleared.

  • Thanks for checking. I guess it's still a better idea to just move it, but it's good to know that I can link it. – Tormyst Jun 26 '14 at 19:00
  • @Tormyst, the reason hard links work in this case is both the source and destination files will share the same inode. So even if source is deleted, the destination file still exists with that inode. – Ramesh Jun 26 '14 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.