From this Question I know that I can run ls -d to show the directory names only instead of their contents.
Short question:
How to do the same with sftp.
Specific problem:
I have a script which should gather directories:
echo ls -1 '*/*/Folder*' | sftp -i /path/to/key user@hostIf there is more than one folder matching, I receive a list of the folders:
path/to/Folder1
path/to/Folder2
[...]If there is only one folder matching, I receive the contents of that folder:
path/to/Folder1/File1
path/to/Folder1/File2
[...]But I'd like to see just this:
path/to/Folder1Note:
- I cannot just use
ssh -cas I don't have full access.
1 Answer
When running ls in sftp, you are not running the system executable ls, but a stripped down version included as an internal command of the sftp environment. The -d option is not supported by this version since it isn't the actual ls you are familiar with,and only supports a limited set of options (see help in the sftp environment):
ls [-1afhlnrSt] [path] Display remote directory listingSo, your only choice is to parse the output:
echo ls -1 '*/*/Folder*' | sftp -i /path/to/key user@host | sed -E 's|(.*/Folder*[^/]*/).*|\1|' | grep -v '^sftp' | sort -uThe sed will remove anything after the */*/Folder*/, te grep -v will remove the line showing the sftp prompt and the command run (sftp> ls -1 */*/Folder*/) and the sort -u will only show unique entries.