Link to home
Start Free TrialLog in
Avatar of mikha
mikhaFlag for United States of America

asked on

how to get the count of files while logged in via sftp?

I used sftp username@server to login to a server and cd into the directory I want. how to get count of the files it that directory. I tried ls | wc , but it doesn't work . it give me this message         ==> can't ls : "/mydirectoryname/ | " not found.
is there a easy way to do this, I don't have permission to write or create files on this server/directory
Avatar of Scott Silva
Scott Silva
Flag of United States of America image

Sftp doesn't have shell access. It just has access to its included command structure. You could ssh in and get your count.
Avatar of Bill Prew
Bill Prew

You should be able to do

! ls

but not sure if you can do

! ls|wc

give it a try...


»bp
You need to run the ls command remotely via scp and then pipe this to wc on your machine.
you may do that also via ssh and let both ls and wc run on the remote machine. try:

ssh username@server "ls -l | wc"

sftp username@server "ls -l" | wc -l
Avatar of mikha

ASKER

@thanks Scott - i can only sftp into this, now ssh.
Avatar of mikha

ASKER

@Bill - adding ! , gives me count of my local machine, not the remote one
Sorry, misunderstood when I read it originally.


»bp
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@omarfarid provided a working, likely easiest, solution for sftp.

@arnold provide a rock solid ssh solution.
Avatar of mikha

ASKER

@arnold - thanks.  following command actually worked on one of the development servers , we have. it makes sense because I can ssh into it . but another server where i can only sftp , the following command generated this error/message "PTY allocation requet failed on channel ..."

ssh -t user@remotehost "cd where_you_need_to_be &&  ls | wc -l"

I am new to this so, what does this mean , -t option not to establish a shell. I am assuming , this bascially connects, issues a command and closes the connection. I am new to this, ssh is establishing a secure connection , such that you can work with the remote machine, where as sftp or ftp is protocol for communitation similar to http , righ?
SFTP is a sub component of SSH. SFTP provides a similar interaction as FTP without the bifurcation of the authentication port 21 and data port 20.

if port 22 is open, both ssh and sftp can connect. the distinction/control is the shell assignment to the account of the user authenticating.
 
They can limit based on the shell they assign to your user.
your best option might be to get a graphical tool that supports sftp and then use this tool to count the files.
Avatar of mikha

ASKER

@arnold - can you suggest a GUI for Mac
Try filezile
add new site, make sure to set the mode to sftp from FTP or other options.

https://filezilla-project.org/download.php?platform=osx