12

This post is about removing muliple files from the remote server, when sftp password less connection is setup. I have the code as below. Only first file in the variable $file_list gets deleted, when I have the variable set as,

$file_list="file1 file2"

sftp $USER@$HOST
rm $file_list
quit
SFTP-Session

I even tried executing the commands in prompt mode.

sftp $USER@$HOST
rm file1 file2

However, I still see that only file1 is getting deleted.

I am not sure if I am missing any basic command. I tried mdelete/mdel/mrm, which were rejected as Invalid command in sftp prompt window.

8

Here is one possible solution that can be added to bash script. This is not ideal as it will make a new connection for each file.

#!/bin/bash
# set variables
USER="username"
HOST="hostname"
file_list="file1 file1 file3 file4"
# delete each file
for file in $file_list; do
    echo "rm $file" | sftp $USER@$HOST
done
exit 0

This one-liner is far better! file1-9 being file names to remove, use a variable if you like, it's the same thing.

for file in file1 file2 file3 file4 file5 file6 file7 file8 file9; do echo -e "rm $file" >> sftp_batch; done; sftp -b sftp_batch username@hostname; rm sftp_batch
  • What if the files have white spaces in their name? Will this solution work? – codeforester Apr 3 '18 at 1:16
  • You can quote the file names. If you have many of them I suggest you look into bash parameter expansion to get best results. Generally for simplicity an admin will not allow filenames with whitespaces and related characters. – mbiber Apr 4 '18 at 19:19
1

You cannot give a list to the rm command in sftp, only takes one path as is written in the man page:

rm path
    Delete remote file specified by path.
1

I typically use lftp to do this.

Setup - on rmeote server skinner
$ mkdir adir
$ touch afile1 afile2
$ ls
afile1  afile2
now we delete
$ file_list="afile1 afile2"
$ lftp sftp://sam@skinner -e "cd ~/adir; mrm $file_list"
cd ok, cwd=/home/sam/adir    
rm ok, 2 files removed                 
lftp sam@skinner:~/adir> ls
drwxr-xr-x    2 sam      users        4096 Nov  4 11:52 .
drwxr-x---  131 sam      users       20480 Nov  4 11:51 ..
lftp sam@skinner:~/adir> bye

To make it a single command:

$ lftp sftp://sam@skinner -e "cd ~/adir; mrm $file_list; bye"
  • I am not able to execute lftp command, may be it requires some library files. – bhawna Nov 5 '14 at 8:59
  • @bhawna - it may not be installed. – slm Nov 5 '14 at 12:19
1

If you can sftp, can you not run ssh? If so then a command such as:

 ssh user@hostname rm file1 file2 

should work fine. If you use public/private SSH keys there will be no password prompt.

  • Yes it worked, but the above command does not create any log information, and the requirement is to create log as well (Though i didn't specify it in the question). Thanks – bhawna Nov 5 '14 at 8:58
1

If those are the file names. You could delete them using a glob pattern with

rm file*
  • Well, it sure wasn't me. – Kusalananda Jan 5 '17 at 18:39
  • I've tried this method and it works if you are using SFTP to access your remote server. – MrMauricioLeite May 7 '18 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.