Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Mon Jan 21, 2019 4:53 pm


Username:
Subject:
Message body:
Enter your message here, it may contain no more than 60000 characters. 

Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
Font size:
 
Font colour
Options:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation code
Confirmation code:
In an effort to prevent automatic submissions, we require that you enter both of the words displayed into the text field underneath.
     

Topic review - BASH shell script to mointor a directory and move file without overwriting destination
Author Message
Post subject: BASH shell script to mointor a directory and move file without overwriting destination  |  Post Posted: Wed Nov 15, 2017 7:48 am
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.
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO