Score:-3

Trying to pipe, redirect, or use `grep` or similar commands in a sftp environment

by flag

How can i limit the ls command output to display only the most recent X number of files created within a folder in a sftp environment ?

tail, grep, or > >> (redirect commands) are not available in sftp.

what alternatives are available ?

Pilot6 avatar
cn flag
It is totally unclear what you mean by "tail or grep or > >> are not available in sftp"
ru flag
@Pilot6 namely, I think OP is trying to use `ls` inside an `sftp` session to list data, and then sort/filter that data, alas SFTP's `ls` is not that capable, and inside an `sftp` command line / communication session, you can't redirect output to the filesystem.
Pilot6 avatar
cn flag
But why not do it using ssh?
ru flag
@Pilot6 not all endpoints *permit* SSH connections, there's ways to disallow SSH shell access but allow SFTP. SFTP does not imply SSH shell connections actually work.
Score:2
ru flag

SFTP just speaks FTP commands over an SSH tunnel. There is no function to list only a subset of files, you'd have to list all files and then identify the list you want to work with later. If that's what you want, then SFTP alone is not the right tool set to use.

A cheat sheet containing most SFTP commands and their capabilities can be found here (no affiliation, I just Googled SFTP Commands List).

If you are trying to do advanced filtration on ls output, SFTP is not the proper tool to use, as you are limited to what SFTP commands exist, and it is not an SSH session. You can do an ls with some options within SFTP, and then take that data separately (copy/paste on the environment) and load it into a notepad and manipulate it on your end, however this requires manual intervention and there is no mechanism to do this within the sftp command line environment.

GreuDeFumat avatar
by flag
Thank you very much Thomas for your time and reply. Being my first post on the platform, i's very pleasantly surprised to receive an answer so swiftly and professional. Much appreciated. As part of a backups check i have to do, i made a small script to jump in a few servers available only in sftp. i wanted to list the files in a folder but limit the listing somehow as the servers host quite a few files and having to scroll through the whole output beats a little the purpose of my try-script. Many thanks for your input. Highly appreciated
ru flag
@GreuDeFumat Without seeing your script I cannot suggest an alternative. There may be a way to execute the command in FTP and then pipe the output to a file for filtration, but I'd have to experiment, and generally speaking SFTP isn't the proper tool for mass backup checking, etc.
GreuDeFumat avatar
by flag
Thank you again for your input Thomas. i'll play around to see what else i can discover. All the best !
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.