The following bash script will monitor the source directory for incoming new files(i.e. It will not copy or remove any preexisting files) and copy them to two destination directories then delete them afterwords ... You need to run the script and keep it running before you start receiving any new files in the source directory(i.e. The script will catch new incoming files only if it is already running) ... The script uses inotifywait
that you need to install first with sudo apt install inotify-tools
... Please read the comments in the script and specify the paths first:
#!/bin/bash
Specify the full path to the source directory in the line below (keep the last "/").
source_d="/full/path/to/directory/"
Specify the fullpath to the first destination directory in the line below (keep the last "/").
destination_d1="/full/path/to/directory1/"
Specify the full path to the second destination directory in the line below (keep the last "/").
destination_d2="/full/path/to/directory2/"
inotifywait -m -q -e close_write "$source_d" |
while read -r path action file; do
cp -- "$path$file" "$destination_d1$file"
cp -- "$path$file" "$destination_d2$file"
rm -- "$path$file"
done
mv
operation instead of a copy operationcp
is normally what is used to automate this. If you have specific requirements not covered by this, then please edit your question and add explanation. – vanadium Nov 22 '22 at 14:42&& rm -rf /path/to/source_dir
(obviously change the path to the correct one). Make sure to check this in a copied portion of your files to make sure that it works as intended before applying it to your files. – BeastOfCaerbannog Nov 22 '22 at 15:31for i in /opt/test2 /opt/test3; do cp /opt/test1/*.txt "$i"; done && rm /opt/test1/*.txt
The deletion of the files should happen after the for loop has completed. In your command you deleted the files right after copying them for the first time, i.e. after copying them to/opt/test2
. – BeastOfCaerbannog Nov 23 '22 at 12:54