0

I run this command on my local machine to delete a list of files contained in a text file.

xargs rm -r < deletion_list.txt

It works as expected and deletes all of the files in the current working directory that are listed in deletion_list.txt.

I then use sftp to connect to a remote host and create a list of files on my local machine that I want to delete from the remote host.

While connected to the remote host, I enter these commands one by one:

Change local working directory:

lcd /home/user/files_to_delete

Create a text file in my local working directory of files in the same directory matching the specified pattern:

!ls *.txt > deletion_list.txt

This is supposed to delete files on the remote host based on the file names in deletion_list.txt which is in my local working directory, but it results in an "Invalid Command" error:

xargs rm -r < /home/user/files_to_delete/deletion_list.txt

Why does this not work?

  • 1
    Are you trying to run xargs as a command in the sftp session? – Kusalananda Feb 28 '20 at 23:14
  • @Kusalananda Yes. – user-2147482428 Feb 28 '20 at 23:15
  • 2
    xargs is not a valid sftp command (see the sftp manual). – Kusalananda Feb 28 '20 at 23:15
  • 1
    Use ssh or telnet connection. – s3n0 Feb 28 '20 at 23:17
  • Did you use an exclamation mark before the command in the sftp console: !localshellcommand ? – s3n0 Feb 28 '20 at 23:35
  • @s3n0 If you are asking whether I used !xargs rm -r < /home/user/files_to_delete/deletion_list.txt then I did not. If you are asking if I used a local shell command before the xargs command, then yes, the command before it uses !ls. – user-2147482428 Feb 28 '20 at 23:42
  • I mean !rm -r < /home/user/files_to_delete, but I don't know where your files_to_delete file is located. This should call a local shell (such as /bin/bash -c "command"). Another option would be to download the deletion file to the machine, then start deleting the files remotely. BTW, similar questions have already been solved here: https://unix.stackexchange.com/questions/165919/how-to-remove-multiple-files-using-sftp (maybe it will help you though this is not the method you require). – s3n0 Feb 29 '20 at 00:03
  • @s3n0 I'm sorry if the question wasn't clear. The deletion_list.txt is the file containing the file names I want to delete, and it is in a local directory on my local machine. The files I want to actually delete are in the remote directory. So I want to delete remote files based on the local list. I've updated my question with a new command that doesn't rely on xargs. – user-2147482428 Feb 29 '20 at 00:22
  • ! will completely execute on the local machine. It will use local shell rm command, not remote sftp rm. – Martin Prikryl Feb 29 '20 at 07:44
  • @MartinPrikryl Everyone can read a man-page. Just type help or?into the sFTP console. If you want to explain what ! is for, then you should explain whether it is a "local shell" from the point of view of a server running sFTP service, or whether it is a "local shell" from the point of view of an administrator's machine (from which the administrator has connected to the sFTP server). The term "local" is therefore very misleading from the perspective of computer networks. There is still a problem that sFTP has limited commands and cannot be used as a regular SSH regular connection. – s3n0 Feb 29 '20 at 09:52

1 Answers1

2

The documentation for sftp (see man sftp) starts

DESCRIPTION sftp is a file transfer program, similar to ftp(1), which performs all operations over an encrypted ssh(1) transport.

A little later it writes,

INTERACTIVE COMMANDS Once in interactive mode, sftp understands a set of commands similar to those of ftp(1).

and continues with the set of valid commands (bye cd chgrp chmod chown df exit get help lcd lls lmkdir ln lpwd ls lumask mkdir progres put pwd quit reget reput rename rm rmdir symlink version ! ?). The xargs command is not amongst that set so you cannot use it inside the sftp application. (Note that ! is a local shell escape, which passes the remainder of the line to a local shell. Such commands are escaped from sftp and not processed by it.)

If you really want to use tools like xargs you should consider using ssh to provide a shell-based session to the remote host. Instead of sftp user@remotehost you would use ssh user@remotehost (assuming your systems administrator allows interactive sessions, that is). Once you have confirmed this works, you can string commands together across hosts:

# On the local host
xargs rm -r < deletion_list.txt

# Also on the local host connnecting to the remote
ssh user@remotehost xargs rm -r < delete_list.txt

What that second command does is to connect to the remote system and run xargs rm -r on it, feeding its stdin from your local file called delete_list.txt.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287