Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
https://forum.ivorde.com/

BASH shell script to mointor a directory and move file without overwriting destination
https://forum.ivorde.com/bash-shell-script-to-mointor-a-directory-and-move-file-without-overwriting-destination-t20038.html
Page 1 of 1

Author:  admin [ Wed Nov 15, 2017 7:48 am ]
Post subject:  BASH shell script to mointor a directory and move file without overwriting destination

Hello,

So below is a bash script that will monitor a source directory for file creation and, everytime a file is created, move the file to a destination directory without overwriting the file in the destination.

Script:
Code:
$ cat move_file.sh
#!/bin/bash

src_dir=/tmp/test/src
dst_dir=/tmp/test/dst

while : ; do
   ls ${src_dir} | while read file ; do
      # Check if file from src dir exist in dst
      i=1
      stop=0
      while [ $stop -ne 1 ]
      do
         if [ -f "${dst_dir}/${file}" ]
         then
            echo "${dst_dir}/${file} exists. Trying ${dst_dir}/${file}_${i}"
            if [ -f "${dst_dir}/${file}_${i}" ]
            then
               echo "${dst_dir}/${file}_${i} exists. Trying ${dst_dir}/${file}_${i}+1"
               i=`expr $i + 1`
            else
               stop=1
               mv ${src_dir}/${file} ${dst_dir}/${file}_${i}
            fi
         else
            stop=1
            mv ${src_dir}/${file} ${dst_dir}/${file}
         fi
      done
      sleep 1
   done
done


Script is executed and stays in foreground. On a different shell, go to source directory, create a file named "file" and wait for 1 second and check the destination directory. Do this multiple times and you will see in destination directory multiple files named "file_+suffix":

Below is the result of the script:
Code:
server$ find . -type f | xargs rm
server$ touch src/file; sleep 1; find . -type f
./dst/file
server$ touch src/file; sleep 1; find . -type f
./dst/file
./dst/file_1
server$ touch src/file; sleep 1; find . -type f
./dst/file
./dst/file_1
./dst/file_2
server$ touch src/file; sleep 1; find . -type f
./dst/file
./dst/file_1
./dst/file_2
./dst/file_3
server$ touch src/file; sleep 1; find . -type f
./dst/file
./dst/file_1
./dst/file_2
./dst/file_3
./dst/file_4
server$ touch src/file; sleep 1; find . -type f
./dst/file
./dst/file_1
./dst/file_2
./dst/file_3
./dst/file_4
./dst/file_5
server$ touch src/file; sleep 1; find . -type f
./dst/file
./dst/file_1
./dst/file_2
./dst/file_3
./dst/file_4
./dst/file_5
./dst/file_6

And below is output of the script:
Code:
server$ sh ~/move_file.sh
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_1
/tmp/test/dst/file_1 exists. Trying /tmp/test/dst/file_1+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_2
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_1
/tmp/test/dst/file_1 exists. Trying /tmp/test/dst/file_1+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_2
/tmp/test/dst/file_2 exists. Trying /tmp/test/dst/file_2+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_3
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_1
/tmp/test/dst/file_1 exists. Trying /tmp/test/dst/file_1+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_2
/tmp/test/dst/file_2 exists. Trying /tmp/test/dst/file_2+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_3
/tmp/test/dst/file_3 exists. Trying /tmp/test/dst/file_3+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_4
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_1
/tmp/test/dst/file_1 exists. Trying /tmp/test/dst/file_1+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_2
/tmp/test/dst/file_2 exists. Trying /tmp/test/dst/file_2+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_3
/tmp/test/dst/file_3 exists. Trying /tmp/test/dst/file_3+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_4
/tmp/test/dst/file_4 exists. Trying /tmp/test/dst/file_4+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_5
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_1
/tmp/test/dst/file_1 exists. Trying /tmp/test/dst/file_1+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_2
/tmp/test/dst/file_2 exists. Trying /tmp/test/dst/file_2+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_3
/tmp/test/dst/file_3 exists. Trying /tmp/test/dst/file_3+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_4
/tmp/test/dst/file_4 exists. Trying /tmp/test/dst/file_4+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_5
/tmp/test/dst/file_5 exists. Trying /tmp/test/dst/file_5+1
/tmp/test/dst/file exists. Trying /tmp/test/dst/file_6


Make sure to test & adapt prior to use.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/