简体   繁体   中英

Getting the timestamp of a file from a SFTP server using ls

I'm currently writing a Powershell script which retrieves the timestamps of all files in a directory on a SFTP server and writes it to a file. I am using the sftp utility which comes with cygwin to connect to the server.

The issue that I am having is that the ls command does not show the year in the timestamp. This is an example of the output:

sftp> ls -lt
-rw-r--r--   1 9665     9665          358 Mar 13 10:05 file.txt
-rw-r--r--   1 9665     9665          217 Mar 13 10:00 file2.txt

I don't seem to have access to any of the flags which force ls to show the year for the regular ls command. These are the options I have available to me via this:

ls [-1aflnrSt] [path]              Display remote directory listing

I have tried using psftp as well and it gives the same format without the year.

My question is how can I ensure that the year is shown in the date? This would be the ideal solution. But if it can't be shown, is it simply safe to assume that the file is from the current year, and that any files from previous years would specifically specify the year? Is this the default behavior of ls over sftp?

Ways to do that: I. You can parse date in string and try to find 4 numbers in a row. Also add checking of containig "19","20" or $targetYear going firsts.

II. Also you may use "ls -l --full-time" command showing year.

III. And at last in my practice working with files in remote Linux servers I used:

  1. mounting remote directory as disk "New-PSDrive -name $diskLiteral -psprovider FileSystem -Root $outsightFolder -Credential $cred"
  2. geting sorted list of files in directory "Get-ChildItem ($diskLiteral + $subfolder) -Force | Where-Object $_.CreationTime -ge $targetDate"
  3. unmounting directory "Remove-PSDrive -Name $diskLiteral -Force -PSProvider FileSystem".

    On second step you can add any sorting as you want, select any information about files and Format-Table (if it nessesary table form of data representation).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM