I have a file where my script outputs IP and file paths. I want to process that file further where it could automatically ssh to that IP and take file stat of that path from respective IP

cat final-list
Node Name: Out-ofSync-Filename: /home/user/nginx/Templates/file1
Node Name: Out-ofSync-Filename: /home/user/nginx/createfile /home/user/nginx/Templates/file3
Node Name: Out-ofSync-Filename: /home/user/nginx/nginx/Templates/file1 /home/user/nginx/nginx/Templates/file5 /home/user/nginx/nginx/Templates/file3 /home/user/nginx/nginx/Templates/file9 /home/user/nginx/nginx/file12 /home/user/nginx/Templates/file56

My script for parsing above file:

xargs -n1 < final-list | grep -v "Name:" | grep -v "Node" | grep -v "Out-ofSync-Filename:"

It has IP and respective files(filepaths per IP may increase in future) for which I want to find out last modified time of that file by ssh into that server IP using respective Path.

  • 2
    I'm noticing that you haven't accepted any of the answers to your other questions. Consider accepting the most helpful answers. Not ever accepting answers will eventually deter users from engaging with your questions. See also unix.stackexchange.com/help/someone-answers
    – Kusalananda
    Jul 23, 2021 at 6:37
  • Is there a reason why you don't simply run rsync in dry-run mode between some master repository of files and your various servers? This would tell you exactly what files are not up to date.
    – Kusalananda
    Jul 23, 2021 at 6:39

2 Answers 2


Reading from your final-list directly and assuming that the remote system is a Linux system:

while read -r a1 a2 remote a3 pathnames
    ssh -n "$remote" stat -c %n:%y "$pathnames"
done <final-list

This further relies on the pathnames in the list of pathnames to be nice, i.e. that they contain no embedded whitespace characters or filename globbing characters. It loops through the lines of final-list and calls stat on the remote system with the list of pathnames as argument.

The a1, a2, and a3 variables read with read are dummy variables whose values we aren't interested in. a1 will have the value Node, a2 will have the value Name:, and a3 will have the value Out-ofSync-Filename:.

The -n option to ssh is needed to stop ssh from reading the data stream that the loop is reading from.

The stat call uses the output format %n:%y which will produce a list of pathname:human-readable-modification-time lines. Use %Y in place of %y to get a Unix timestamp instead.


I've done something like this before. Looping thru every lines:

while read line; do
    host_ip=$(cut -d' ' -f3 <<< "$line")
    file_paths=($( cut -d' ' -f5- <<< "$line" ))
    for f in "${file_paths[@]}"; do
        ssh "$host_ip" "stat $f"
done < input-file
  • 2
    ssh must be run with -n or with standard input redirected from /dev/null, or it will read all lines from input-file.
    – Kusalananda
    Jul 23, 2021 at 6:03
  • @Kusalananda I see. Didn't know about that. Thanks!
    – annahri
    Jul 24, 2021 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .