temp files

Hi there,

As a regular unix user I am forever programming on the command line or writing scripts so that I first write a load of data to a file to read from. In the end I am always left with a bundle of .txt, .tmp which is what I usually call them. As a basic programmmer I was wondering is there a more cleverer way of writing data temporarily to a place of which can be used as reference for my script or next lot of commands??? For example,

du -ks * >> disk.log

......then I go on to using my scripts to manipulate or read in this file

The simplest portable way: use man mktemp (POSIX). Example

#!/usr/bin/ksh

TMPDIR=$( mktemp )
mkdir -p ${TMPDIR}

trap 'ret=$?; rm -rf ${TMPDIR}; exit ${ret}' 0

# do your stuff here, eg
du -ks * >> ${TMPDIR}/disk.log

That way all your temporary output will be collected in a special temporary directory, which will be cleaned when the script exits (virtual Signal 0)

thanks pludi, perfect!

mktemp will create a temporary file, you need add -d option to create a temp directory.

TMPDIR=$( mktemp -d )

mktemp -d is only available in the GNU version. Getting a name from mktemp and then manually creating that directory works across all POSIX compliant systems.

The POSIX standard does not specify a mktemp utility.

Another approach is to use the ramdisk on /dev/shm (shm stands for SHared Memory).
What is /dev/shm and its practical usage