+1 vote
in Operating Systems by (60.2k points)

I am putting some files to a destination server from a source computer. After SFTP, I run "rm -rf *" command to delete all the files from the folder on the destination server. However, I am getting the following error:

rm: Invalid flag -r

How to fix this error so that I can delete the files before copying the files?

1 Answer

+3 votes
by (73.6k points)
selected by
 
Best answer

The sftp command does not support recursive deletion (rm -r or rm -rf), which is why you are getting the rm: Invalid flag -r error.

To delete all files in a folder on the destination machine, you can SSH to the machine and then use "rm -rf *" command.

Here is an example:

SFTP_USER="your_username"

SFTP_HOST="destination_server"

SFTP_PORT="22"  # Replace with the port number if it's not 22

REMOTE_PATH="/path/to/destination/"

# Cleanup remote directory using SSH

ssh -p $SFTP_PORT $SFTP_USER@$SFTP_HOST "rm -rf $REMOTE_PATH*"

 

Related questions


...