Time in filename

Hi,

I've some files that I want to send them daily via ftp. Date and time are a part of filename, for example:

abcd_deft_2018-02-21_100012
abcd_mnpo_2018-02-21_100020

These files are created at 10 am and should be sent 17 pm. Time is different in filename but all begins with 10. How can I write the code so that sends all file that time begins with 10 and whatever after?

Hello,

Please let us know, what you have tried already to transmit the files.
we cannot create the complete script for you, if you are facing any issues with your script that you have created, we can assist you to resolve that.

To match filenames that end with "10" followed by four other characters, you can use the filename matching pattern:

*10????

If there might be other files that match that pattern and you only want to match files that end with "_10" followed by four decimal digits, you can use the filename matching pattern:

*_10[0-9][0-9][0-9][0-9]

Hi,
Here is the code:

#!/bin/ksh
#
#

DATE=`date "+%Y%m%d"`

HOST='abcd.com'
USER='abc'
PASSWD='****'
LOCALPATH='/opt/abc/Output'
LOGPATH='/opt/abc/logs'

ftp -n -v $HOST <<END_SCRIPT>>$LOGPATH/ftp_abc_$DATE_log.txt
quote USER $USER
quote PASS $PASSWD
lcd $LOCALPATH
put abcd_part_$DATE_10????.txt
put mnpg_part_$DATE_10????.txt
put fdop_part_$DATE_10????.txt
put cdpm_part_$DATE_10????.txt
put abcd_part_$DATE_10????.txt


quit
END_SCRIPT
exit 0

Files have diffrerent time, for example: abcd_part_2018-02-23_100004 , the otherone has 100012 for time,...
I've tested with 10???? and _10[0-9][0-9][0-9][0-9] but getting same error that

No such file or directory

.

It's not quite clear WHAT you tested, and what files / directories are available.

Does abcd_part_2018-02-23_100004 exist in your local directory, or is it abcd_part_2018-02-23_100004.txt ? Does LOCALPATH='/opt/abc/Output' exist? What's its contents?

Does

abcd_part_2018-02-23_100004

exist in your local directory, or is it

abcd_part_2018-02-23_100004.txt

? YES, they are there, all files.

Does

LOCALPATH='/opt/abc/Output'

exist? YES

What's its contents? All files that I need to send via ftp!

Problem is how to write the code in this line(below) so that it can read and send the files:

put abcd_part_$DATE_(What should i write here?).txt

That isn't an answer. You said the format of your filenames is:

abcd_deft_2018-02-21_100012
abcd_mnpo_2018-02-21_100020

but your code looks for filenames ending with .txt . Which form is correct? Is there a .txt at the end of your filenames or not?

Show us the output from the command:

ls -l /opt/abc/Output/*2018-02-21_10*

What are you trying to do? Are you trying to put all files matching a pattern found in a local directory into a directory on a remote system? Or, are you trying to get all files matching a pattern found in a directory on a remote system into a local directory?

Yes, the type of file is txt and as I mentioned the name of files are different.

Files are there in 'Output' directory, here is result:

abcd_part_2018-02-21_100004.txt
mnpg_part_2018-02-21_100012.txt
fdop_part_2018-02-21_100026.txt
cdpm_part_2018-02-21_100120.txt
abcd_part_2018-02-21_100135.txt

Please read my post one more time, in code you can see that I'm using PUT to send files matching a pattern in local directory to the remote server.

---------- Post updated at 04:01 AM ---------- Previous update was at 03:42 AM ----------

You said "YES, they are there, all files." which does not say they are of type text and does not clearly state whether or not your files' names end with .txt or not.

I can see what your code does and I can see that with the above listed files, your code will not work. But, since we don't have a clear statement of what you are trying to do, it is hard to guess at what should be done to fix your code. The code you showed us asking for a pattern to be inserted to fix it:

put abcd_part_$DATE_(What should i write here?).txt

has three possible answers to what you should write there depending on whether you want to transfer the first matching file, the second matching file, or both matching files. (Note that the put command will only transfer one file; not two. Note also that there are no files for some of the patterns you're passing to put which will generate what I would assume would be unneeded diagnostic messages.

So, I repeat. What are you trying to do? Do you want to move all of the files with the proper timestamp? Or, if there is more than one file with a given initial part of the pattern with different timestamps, do you just want to send the first of that base name or the last of that base name? Are you expecting to get at least one (or exactly one) file matching each of your initial file base name patterns during that hour every day?

If you want to send all of the files (which is not what your script is trying to do), you could try replacing all of the put s in your script with the single command:

mput *_$DATE_10????.txt

And, as the moderator said when fixing your post, please use CODE tags when displaying sample input, output, and code segments.

As I mentioned before they are text file which ends with

.txt

as type of file.
The last file name is missing 'e'. All daily files have different name but everyday the same files would be created(minute and second are not important).

abcd_part_2018-02-26_100004.txt
mnpg_part_2018-02-26_100012.txt
fdop_part_2018-02-26_100026.txt
cdpm_part_2018-02-26_100120.txt
abcde_part_2018-02-26_100135.txt

Sending some text files to a remote server by ftp. Files are created daily at 10 am.

As I wrote, daily files have different name, but they differ in timestamp.

YES. Then the script should find 5 match(above files) in directory and send them via ftp.
In the directory where the files are located there are more than 300 files with the same name but different date and time and I want to send only daily files, then I can't run your suggested command.

Still: replace put with mput as put doesn't seem to accept wildcard chars. The man page is a bit vague here, while it clearly mentions wildcards for the mput command. Not sure, though, what the "here document" does to the wildcards...

EDIT: Does this help?

. . . <<EOF
. . . 
mput $(ls ????_part_2018-02-26_10????.txt)
EOF

You might need to mangle this. I would suspect that the shell will want to expand/interpolate the ? wildcard and you want to avoid that and pass it into the ftp command as literal text. Of course, you will want $DATE to be interpolated so you might have to end up with something ugly like this:-

mput \?\?\?\?_part_${DATE}_10\?\?\?\?.txt

Not pretty, but it might do the job. An alternate contrived way might be to use single quotes, but of course you have to still allow $DATE to be interpolated, so you get the confusing looking:

mput '????_part_'$DATE'_10????.txt'

A further alternative might be to set a variable early and use that, allowing the shell to interpolate it into what you want:-

:
:
q4='????'     #  Note single quotes
ftp ........  # whatever
:
:
mput ${q4}_part_${DATE}_10${q4}.txt
:
:

If you let the shell expand everything, then you might end up with an invalid command depending on how mput is provided on your client. Some clients don't like mput fileA fileB fileC whilst accepting mput file? so have a look at the description in the (client side) manual page for ftp and also if you fire up the ftp command, you should be able to use help mput from there too. That might clarify things.

I hope that this helps,
Robin