Skip to content
Advertisement

Check the new uploaded file and redirect the file data in other file in bash script?

I have a script to get the mail notification when any new file was created in a directory.

Now I want to redirect the data in another file which was uploaded.

below is the script which is creating a blank file. Please help.

#!/bin/bash

monitor_dir="/home/deepak/deepak/" || "/home/deepak/deepak/ "
email=deepaksharma@gmai.com

files=$(find "$monitor_dir" -maxdepth 1 | sort)
IFS=$'n'

while true
do
sleep 1s
  newfiles=$(find "$monitor_dir" -maxdepth 1 | sort)
  added=$(comm -13 <(echo "$files") <(echo "$newfiles"))

  [ "$added" != "" ] &&
    find $added -maxdepth 1 -printf '%Tct%st%pn' |   mail -s "incoming" "$email"

  files="$newfiles"
echo $files

sleep 70s;

read $files
if [  -s /home/deepak/deepak/$files ] || [ -s /home/deepak/deepak/ $files  ]
then
echo "$files found"
cat /home/deepak/deepak/$files >/home/deepak/N || cat /home/deepak/deepak/ $files >/home/deepak/N

else
    echo "$files not found"
fi

exit 0
done   

Advertisement

Answer

@Yuvi, check tehe content of the $files variable. Does it contain the full path of each file? If this is the case the reason why You are getting blank files may be because the “source” file does not exist.

e.g.

if $files has value /home/deepak/deepak/file1.txt /home/deepak/deepak/file2.txt, then when You are doing the cat after echo "$files found" You will be doing it for unexisting files: /home/deepak/deepak//home/deepak/deepak/file1.txt and /home/deepak/deepak//home/deepak/deepak/file2.txt

If this is the case You may solve Your problem by removing the leading path: cat $files >/home/deepak/N...

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement