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?

10
  • 1
    Are you trying to run xargs as a command in the sftp session? Commented Feb 28, 2020 at 23:14
  • @Kusalananda Yes. Commented Feb 28, 2020 at 23:15
  • 2
    xargs is not a valid sftp command (see the sftp manual). Commented Feb 28, 2020 at 23:15
  • 1
    Use ssh or telnet connection. Commented Feb 28, 2020 at 23:17
  • Did you use an exclamation mark before the command in the sftp console: !localshellcommand ? Commented Feb 28, 2020 at 23:35

1 Answer 1

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.