A
A
Alexander Arbuzov2018-09-28 16:49:06
linux
Alexander Arbuzov, 2018-09-28 16:49:06

How to delete files from a remote machine?

Tell. I am syncing with rsync. After synchronization, I need to delete several files on the remote machine where the synchronization was made. Their list (with paths) is formed in a file. How can I delete files according to this list from a bash script on a remote machine?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hanneman, 2018-09-28
@arbuzzz

Delete on the remote from the list on the local?
PS I am writing in a specially detailed way (line by line, without optimizing the code and commands), so that the author can understand.

filename='/tmp/my.txt'
filelines=`cat $filename`
for line in $filelines ; do
    echo $line
    ssh [email protected]_hostname_or_ip "rm -rf $line; exit"
done

Between hosts, you need to configure authorization without a password (since you are going to do it with a script) so that you do not enter it every time - read here (items "Key management", "Copying the key to the server", "Server key", or look for another configuration guide - There are a million of them online. And in the same article you will find the item "Remote code execution" if you want to learn more about how your task is solved.
PPS: Of course, in order not to connect via ssh to the host in iteration each time (as in my example), it is better to copy the local file to the remote host with the scp command utility (in the same article, the description is just one command line), then with the local using the same script (next step) to remotely run a script on a remote machine, which, reading from the file copied there to the remote host (for it - local already) lines, will erase the files locally. To do this, my script, which is given above, needs to be modified - add a couple of lines. But it's already you. The fishing rod is already in your hands - catch fish.

P
planc, 2018-09-28
@planc

https://stackoverflow.com/questions/1521462/loopin...

while read p; do
  echo "$p"
done </путь/к/файлу/мой_файл.txt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question