If you have binary programs or scripts generating files in /tmp and you want to grab them before they disappear, then use this script. You can copy them. If you copy them to within a folder in /tmp and you can use a hardlink copy (recall a hardlink copy is a quick copy, that doesnt take up extra bytes,but only works within the same “volume”/”filesystem”). If you copy them to another folder, outside of /tmp, then just use regular copy.

  1. install inotify-tools, apt-get install inotify-tools
  2. Modify the MONITOR folder (the folder you will monitor for new files)
  3. Modify CLONE folder (where you will copy the final)
  4. Uncomment cp -rl line if MONITOR and CLONE are in the same volume, because you can utilize hardlinks (hardlink copy = copy -l)
  5. Uncomment cp -r line if MONITOR and CLONE are in different volumes, so you will need to use regular copy
  6. run the script. or copy paste it (since its in a subshell, this is a good copy pasteable)

Script:

#!/bin/bash
# monitor /tmp/tmp and hardlink copy everything to /root/tmpnew (monitors $MONITOR, and copies to $CLONE)
# requirement: apt-get install inotify-tools
# uncomment 1st copy line & comment out 2nd copy line - if CLONE and MONITOR are in the same volume (we can use hardlink because they are in the same volume)
# uncomment 2nd copy line & comment out 1st copy line - if CLONE and MONITOR are in different volumes (hardlinks only work within the same volume)
(MONITOR="/tmp"
CLONE="/root/tmpnew"
mkdir -p "${CLONE}" 2> /dev/null
inotifywait -mr --format='%w%f' -e create "${MONITOR}" | while read file; do
  DIR=`dirname "$file"`
  echo "###############################"
  echo "FILE: $file"
  echo "- DIR: $DIR"
  echo "- DIR#MONITOR: ${DIR#$MONITOR/}"
  echo "- file#MONITOR: ${file#$MONITOR/}"
  echo "- MKDIR: ${CLONE}/${DIR#$MONITOR/}"
  echo "- CP $file TO ${CLONE}/${file#$MONITOR/}"
  mkdir -p "${CLONE}/${DIR#$MONITOR/}"
  # cp -rl "$file" "${CLONE}/${file#$MONITOR/}" && echo "* Hardlink Copy Success" || echo "* Hardlink Copy Failed, error code: $?"
  cp -r "$file" "${CLONE}/${file#$MONITOR/}" && echo "* Copy Success" || echo "* Copy Failed, error code: $?"
done)

Example, in one shell:
# ./inotifytmp.sh
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.

In another shell, create a file into tmp.
# echo "whatever" > /tmp/wow3.txt2123adsf

In first shell you will see:
# ./inotifytmp.sh
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
###############################
FILE: /tmp/wow3.txt2123adsf
- DIR: /tmp
- DIR#MONITOR: /tmp
- file#MONITOR: wow3.txt2123adsf
- MKDIR: /root/tmpnew//tmp
- CP /tmp/wow3.txt2123adsf /root/tmpnew/wow3.txt2123adsf
* Copy Success

You can go to /root/tmpnew to see the files

NOTE: mkdir has extra slash, double slash, //, bash ignores double slashs as it knows they will appear often. So /folder//somefolder//somefile is the same as /folder/somefolder/somefile.

Leave a Reply

Your email address will not be published.