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

FAQ
It is currently Thu Dec 07, 2023 8:05 am


Tips & Tricks, Questions regarding shell scripts, awk, perl, sed and much more.

Author Message
admin
Post  Post subject: BASH shell script to mointor a directory and move file without overwriting destination  |  Posted: Wed Nov 15, 2017 7:48 am
Site Admin

Joined: Mon Aug 03, 2009 8:43 am
Posts: 104

Offline
 

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.

_________________
VPSie - SSD VPS servers in AMS-IX, LINX, DE-CIX
https://vpsie.com





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "BASH shell script to mointor a directory and move file without overwriting destination"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. Password generator with user inputs bash script

Kvr123

0

2396

Thu Dec 01, 2016 5:06 am

Kvr123 View the latest post

There are no new unread posts for this topic. grep match pattern: Binary file bincharacters.txt matches - How to make grep treat a file as text

mandrei99

0

2525

Wed Dec 11, 2013 7:15 am

mandrei99 View the latest post

There are no new unread posts for this topic. How to replace new line in perl script

mandrei99

1

5044

Wed Jun 26, 2013 8:18 am

admin View the latest post

There are no new unread posts for this topic. Removing new line / EoL characters in a PERL script

mandrei99

0

4499

Mon Nov 04, 2013 6:47 am

mandrei99 View the latest post

There are no new unread posts for this topic. How to increase bash history size in Linux

mandrei99

0

20982

Mon Jan 12, 2015 11:45 am

mandrei99 View the latest post

There are no new unread posts for this topic. Linux / FreeBSD: Avoid bash history duplicate lines with HISTCONTROL

mandrei99

0

2670

Mon Jan 12, 2015 12:01 pm

mandrei99 View the latest post

There are no new unread posts for this topic. Attachment(s) Juniper SRX - Perl script - SNMP based RRDtool graphis from CPU usage and Flow Session (current)

mandrei99

1

6064

Mon Nov 04, 2013 11:47 am

mandrei99 View the latest post

There are no new unread posts for this topic. Shell scripting

Diana

0

1873

Thu Aug 24, 2017 3:10 am

Diana View the latest post

There are no new unread posts for this topic. Unix shell - using TR to replace new lines with spaces

debuser

0

8959

Thu Feb 25, 2010 8:08 am

debuser View the latest post

There are no new unread posts for this topic. How to echo newline in Linux/Unix shell

mandrei99

0

79948

Tue Jun 25, 2013 5:58 am

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
cronNews 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