Score:1

Automatically copy incoming files to multiple folders then remove the source files

ki flag

I would like to know automated script for copying source files from directory to multiple directories and after copying files remove the source files from the source directory

We have one folder where the .xml files are coming. so on first step i wanted to copy these files from this source folder to another two folders i.e folder one and folder two. Folder one is for keeping the files as backup purpose only and folder two is to run another script for splitting xml files according to our requirement. After copying files remove the files from source folder

Regards

BeastOfCaerbannog avatar
ca flag
You need to be more specific about what you want to achieve. Please [edit] your question to provide more details and examples of the desired directory/file structure before and after.
vanadium avatar
cn flag
A move `mv` operation instead of a copy operation `cp` 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.
sunman avatar
ki flag
@vanadium If i use mv command i am unable to copy the file to another folder
sunman avatar
ki flag
@BeastOfCaerbannog Thanks for your reply .. this is only copying the files to directories.. i wanted to copy/move the files to multiple directory and then remove the source file
BeastOfCaerbannog avatar
ca flag
For this just add `&& 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.
sunman avatar
ki flag
@BeastOfCaerbannog thanks i will test it and update you
sunman avatar
ki flag
@BeastOfCaerbannog i have used following command: for i in /opt/test2 /opt/test3; do cp /opt/test1/*.txt $i && rm /opt/test1/*.txt; this command copied the files from test 1 directory to test2 and then rm command delete the files without copying to test3 directory
BeastOfCaerbannog avatar
ca flag
The correct command would be: `for 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`.
sunman avatar
ki flag
@BeastOfCaerbannog Oh my God its working :-D thank you so much !!
sunman avatar
ki flag
@BeastOfCaerbannog One more question can we copy from one server source files to multiple server with different folders ? i.e server1 source file>>server2(folder1,folder2) & server3(folder1,folder2)
BeastOfCaerbannog avatar
ca flag
I'm not sure, since I don't know much about using servers. You better post this as another question.
Score:4
jp flag

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
Score:-1
ng flag

Use cp N-1 times and finally use mv for the Nth time.

cp [files] [folder1] 
cp [files] [folder2]
...
cp [files] [folderN-1]
mv [files] [folderN]
sunman avatar
ki flag
can you please give me a example of cp N-1 for moving files from one source to two destination folders
Anthony Kelly avatar
ng flag
You can't do it in one command. All I'm saying is you need to copy the files to you target directories and for the copy to the last directory use mv instead of cp to make sure the source files are deleted.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.