bash SFTP:返回远程目录中的文件数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25412497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SFTP: return number of files in remote directory?
提问by John
I sent a batch of files to a remote server via SFTP. If it were a local directory I could do something like this ls -l | wc -l
to get the total number of files. However, with SFTP, I get an error Can't ls: "/|" not found
.
我通过 SFTP 将一批文件发送到远程服务器。如果它是本地目录,我可以执行类似操作ls -l | wc -l
来获取文件总数。但是,使用 SFTP,我收到错误消息Can't ls: "/|" not found
。
回答by Cyrus
echo ls -l | sftp server | grep -v '^sftp' | wc -l
If you want to count the files in a directory the directory path should be put after the ls -l command like
如果你想计算一个目录中的文件,目录路径应该放在 ls -l 命令之后,比如
echo ls -l /my/directory/ | sftp server | grep -v '^sftp' | wc -l
回答by Eric Leschinski
Use a batch file to run commands remotely and get the data back to work with in bash:
使用批处理文件远程运行命令并在 bash 中恢复数据:
Make your batch file called mybatch.txt
with these sftp commands:
mybatch.txt
使用这些 sftp 命令调用批处理文件:
cd your_directory/your_sub_directory
ls -l
Save it out and give it 777 permissions.
将其保存并赋予其 777 权限。
chmod 777 mybatch.txt
Then run it like this:
然后像这样运行它:
sftp your_username@your_server.com < mybatch.txt
It will prompt you for the password, enter it.
它会提示你输入密码,输入它。
Then you get the output dumped to bash terminal. So you can pipe that to wc -l
like this:
然后你将输出转储到 bash 终端。所以你可以用管道把它变成wc -l
这样:
sftp your_user@your_server.com < mybatch.txt | wc -l
Connecting to your_server.com...
your_user@your_server.com's password:
8842
The 8842 is the number of lines returned by ls -l
in that directory.
8842 是该ls -l
目录中返回的行数。
Instead of piping it to wc, you could dump it to a file for parsing to determine how many files/folders.
您可以将其转储到文件中进行解析以确定有多少文件/文件夹,而不是通过管道将其传输到 wc。
回答by pedram bashiri
I would use sftp batch file. Create a file called batchfile and enter "ls -l" in it. Then run
我会使用 sftp 批处理文件。创建一个名为 batchfile 的文件并在其中输入“ls -l”。然后运行
sftp -b batchfile user@sftpHost | wc -l
回答by Alex
The easiest way I have found is to use the lftpclient which supports a shell-like syntax to transfer the output of remote ftp commands to local processes.
我发现的最简单的方法是使用lftp客户端,它支持类似 shell 的语法将远程 ftp 命令的输出传输到本地进程。
For example using the pipe character:
例如使用管道字符:
lftp -c 'connect sftp://user_name:password@host_name/directory; ls -l | wc -l'
This will make lftp spawn a local wc -l
and give it the output of the remote ls -l
ftp command on its stdin.
这将使 lftp 在本地生成wc -l
并ls -l
在其标准输入上提供远程ftp 命令的输出。
Shell redirection syntax is also supported and will write directly to local files:
还支持 Shell 重定向语法,并将直接写入本地文件:
lftp -c 'connect sftp://user_name:password@host_name/directory; ls -l >list.txt'
Thus a file named list.txt
containing the remote file listing will be created in the current folder on the local machine. Use >>
to append instead.
因此,list.txt
将在本地计算机的当前文件夹中创建一个名为包含远程文件列表的文件。使用>>
append 代替。
Works perfectly for me.
非常适合我。