LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-16-2024, 01:40 AM   #1
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Rep: Reputation: 73
bash script to get only the latest file from server


This bash script gets messages stored on the server.

How can I alter it to only get the latest message, or messages after a certain date?

Quote:
# now get the latest HW
echo "Fetching any messages ... "
# ssh -p 22 -i ~/.ssh/my_cloud_ed25519 pedro@123.123.123.123
user="pedro"
server="123.123.123.123"
Answers="/home/pedro/messages"
# where the uploaded contact messages go on the cloud server
# can't get the email to work
path1="/var/www/pedro.com/public_html/contact/messages"
echo "Second, get the files from the remote server to this computer."
rsync -av --remove-source-files --progress -e 'ssh -p 22 -i ~/.ssh/my_cloud_ed25519' $user@$server:${path1}/* "${Answers}"
echo "Got the files and saved them to "$Answers
echo "All done!"
Thanks for any tips!
 
Old 03-16-2024, 04:25 AM   #2
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Figured it out! At least 1 way.

Quote:
ssh -p 22 -i ~/.ssh/my_cloud_ed25519 pedro@123.123.123.123
user="pedro"
path1="/var/www/mywebpage.com/public_html/contact/messages"
putpath="/var/www/mywebpage.com/public_html/contact/save_newest/"
# get the name of the newest file
newest=`ls $path1 | sort -n -t _ -k 2 | tail -1`
mv -v $path1$newest $putpath
 
Old 03-16-2024, 07:47 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,795

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Use the sort in ls!
It sorts alphabetically by default, and -t sorts by time.
Code:
# get the name of the newest file
newest=`ls -tr "$path1" | tail -1`
[ -n "$newest" ] &&
  mv -v "$path1/$newest" "$putpath"
 
1 members found this post helpful.
Old 03-17-2024, 01:16 AM   #4
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
I'd like to know what this does, if you have time:

Quote:
[ -n "$newest" ]
I think it says: if exist $newest

Last edited by Pedroski; 03-17-2024 at 01:32 AM.
 
Old 03-17-2024, 03:43 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,310
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Check "man test" or "man [" for -n and the other test options. If-exists is -e and -n has a different meaning.
 
1 members found this post helpful.
Old 03-17-2024, 04:29 AM   #6
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thanks, I found this:

Quote:
-n STRING
the length of STRING is nonzero

STRING equivalent to -n STRING

-z STRING
the length of STRING is zero
How would I collect all files whose date is greater than a certain time?

Last edited by Pedroski; 03-17-2024 at 05:03 AM.
 
Old 03-17-2024, 04:39 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,795

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
And the reason is:
if the ls would not find any file then the mv would move the $path1/ (directory!) into the $outpath/
 
Old 03-19-2024, 12:22 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Quote:
How would I collect all files whose date is greater than a certain time?
You'd need to format the date/time in a FORMAT that makes each one easily sort-able eg
Code:
ls -l --time-style="+%Y-%m-%dT%H:%M:%S"
See the 'date' cmd for details of avail date/time formats.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] schedule a bash script to move recent/latest dump files from server(rhel 5.7) system to another system(rhel 7.5) edpksrtc Red Hat 23 02-29-2020 02:45 AM
[To share Bash knowledge]Notes for Advanced Bash-Scripting Version 10 (Latest) jcky Programming 4 07-31-2014 09:24 AM
[SOLVED] Problem-Simple bash script to copy the latest .jpg file RCCollins Linux - Software 6 11-24-2011 02:24 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration