To delete all but 2 of the latest files in a dir running on a remote Linux box I use :
cd /data
ls -1tr | head -n -2 | xargs -d '\n' rm -f --
I execute these commands manually after logging into the remote Linux box but I'm attempting to combine login and running of these commands into a single command.
I perform something similar using scp where I login and scp the files to local machine at dir /local/data :
scp -i LightsailDefaultKey-eu-west-2.pem -r [email protected]:/data/ /local/data
Is there a method to perform something similar to above where login and commands :
cd /data
ls -1tr | head -n -2 | xargs -d '\n' rm -f --
are executed in a single command on remote machine from local machine ?