Linux Shell ftp command download file based on file date

Source: Internet
Author: User
Tags filetime

Requirements: FTP gets files for remote data, and downloads files according to the file's creation point in time. You can expand your own other requirements depending on the size of the file.

Summary of Knowledge points:

1, the time to obtain the file:

LS-LRT| awk ' {print $6 "" $7 "" $8} '

Time content:

SEP 8 16:03

2. Time format Conversion

Date " Sep 8 16:03 " +%y%m%d%h%m

Conversion Result:

201709081603

3. Specify file name, regular match

pattern="${month}.*.dat$"ls -lrt | awk ' $9 ~/ '$pattern'/{print $6 "" $7 "" $8}'

Notice that the pattern variable in awk is enclosed in ' ',. * is any character, and $ indicates a dat end. The content of pattern is set according to the requirement

4, compare the file time

ls-LRT |awk '$9 ~/'$pattern'/ {\Ctime="date-d \ ""$6" "$7" "$8"\ "+%y%m%d%h%m"; CTime|getline filetime;if(Filetime >'$checktime') Print $9; }

Gets the time of the file, converts the time, and then compares the converted time to the given $checktime, and displays the file name if it is larger than the $checkime time.

|getline Gets the result of the command execution for the execution command.

This completes the method of comparing the local file time. Next, determine the file time for the remote host.

5, FTP command log on to the remote host, and filter the contents of the file to be queried

Ftp_cmd () {ftp -i-n <<! Open $ftp _ipuser $get _user $get _pwdbinarycd ${get_dir}lcd ${local_dir}$*bye! }
Pattern= "${month}.*.dat$"
Files=$ (Ftp_cmdls-LRT |awk '$9 ~/'$pattern'/ {\ 
Ctime="date-d \ ""$6" "$7" "$8"\ "+%y%m%d%h%m"; \
CTime|getline filetime;if(Filetime >'$checktime') Print $9; }'|xargs)

Implement FTP to a specified host via a function call, CD to the specified directory, get LS-LRT results by awk, and then calculate the time to get the file name greater than the specified time.

6. Download the file

Ftp_cmd Mget $files

Linux Shell ftp command download file based on file date

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.