-2

I have a list of files:

XX_1
XX_2
XX_3

whose numbers keep incrementing every single time I run the program. I want to find the latest file using a linux command. I tried:

find . -maxdepth 1 -name "*XX_*" -print

but this gives me all the files with XX_. I just want XX_3 and need to save the output that I get using this command to a variable so that I can copy the file. How do I do that? I tried:

var=$(ssh pi@192.168.0.101 ls -1 FlightLog* | sort -t_ -k2 -nr | head -1)
ssh pi@192.168.0.101 sftp "$var"

And I got the following error:

/Users/ykathur2/bin/GetFile.sh: line 3: var: command not found
ssh: Could not resolve hostname flightlog_88.dat: Name or service not known
Couldn't read packet: Connection reset by peer

Please help!

Yukti Kathuria
  • 61
  • 2
  • 11
  • Possible duplicate of [Bash function to find newest file matching pattern](https://stackoverflow.com/q/5885934/608639) – jww Oct 17 '18 at 06:25
  • 1
    The error indicates you're getting the newest filename (_flightlog_88.dat_). If you're just trying to copy that file to the local host, the second line won't do that. Try `scp pi@192.168.0.101:"$var" .` instead. – Beggarman Oct 17 '18 at 09:10
  • I tried that but I got: usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2 – Yukti Kathuria Oct 18 '18 at 17:30

1 Answers1

2

How about this

$ ls -1 XX*
XX_1
XX_2
XX_3

$ ls -1 XX* | sort -t_ -k2 -nr | head -1
XX_3
stack0114106
  • 8,534
  • 3
  • 13
  • 38