LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   FTP Upload based on Filename (https://www.linuxquestions.org/questions/linux-newbie-8/ftp-upload-based-on-filename-4175595539/)

barani_pd 12-16-2016 12:33 AM

FTP Upload based on Filename
 
Greetings All,

My requirement is I have to read the filename, based on which the files are to be moved to different folders from where to different ftp sites.

Eg. NNABRSTXYZ.txt
where NN is a 2 digit number
AB is a fixed code
RST is folder(s) in working one (also ftp site)
and XYZ is folder(s) inside RST (also folder in ftp site)

Now the script has to read the filename and copy the file to respective XYZ folder(s) in respective RST folder(s) from which the files are to be put into XYZ folder of RST ftp site. This should happen only once for each file and be logged with time of action.

Hope my query is understandable. I regret my English but need help. Someone please help me. Thank you all in advance.

Turbocapitalist 12-16-2016 02:21 AM

By script, if you mean shell script, then you probably want to look at parameter expansion to extract the pieces of the file name into variables which can in turn be used for the upload. Perl might be another option, too, if something more flexible and powerful than shell scripting is needed.

Also, I would highly recommend reconsidering the use of FTP. You can use SFTP with keys instead. With FTP, there is no way to secure it and your machine will eventually get broken into if you use it on a publicly facing machine. In contrast, SFTP works over SSH which has the most strongly vetted encryption available.

barani_pd 12-16-2016 05:10 AM

Hi Turbocapitalist,

Thank you for the reply.

Yes I meant to shell script in Linux. Eventually I will be considering SFTP when things are ready working.

To further my question, there are around 50 RST folders and 10 XYZ folders in it. Also it would appreciable if the script triggers whenever any new files are created (not while being created).

Thank you once again.

Turbocapitalist 12-16-2016 05:17 AM

Or you might move to SFTP now, while things are still working ;) It'll be cheaper to do it the right way the first time rather than messing with FTP and then redoing the script(s) for SFTP. Add to that the cost of needing to wipe the hardware and do a clean re-installation after an FTP-related break-in and SFTP is the clear choice.

However, regardless of that, once you have the script ready, you can use inotify to trigger using incron whenever a file is added to a specific directory.

The first step is to have the script. What do you have so far?

michaelk 12-16-2016 05:23 AM

In addition using inotifywait which is part of the inotify-tools package you can monitor the directory for changes and run the script.

barani_pd 12-16-2016 07:05 AM

Thanks guys.

I still don't have any script ready yet. That is where I need you guys' help.

Turbocapitalist 12-16-2016 09:44 AM

Quote:

Originally Posted by barani_pd (Post 5642839)
Thanks guys.

I still don't have any script ready yet. That is where I need you guys' help.

Build the script one small step at a time. What do you have for a start, even if is a small start? How much of the parameter expansion makes sense via the link above or any tutorials online?

The style here is to help people over the bumps rather than write scripts out of whole cloth.

barani_pd 12-17-2016 06:09 AM

Quote:

cd files
for fname in *.*; do
cen=${fname:7:3}
edi=${fname:4:3}
shopt -s nocasematch #Turning ON case-insensitive matching
case "$cen" in
bng)
case "$edi" in
mrl)
cp -v $fname $cen/$edi;
;;
cex)
cp -v $fname $cen/$edi;
;;
mex)
cp -v $fname $cen/$edi;
;;
sdt)
cp -v $fname $cen/$edi;
;;
cty)
cp -v $fname $cen/$edi;
;;
esac
echo $fname;
;;
bgm)
cp -v $fname $cen/;
echo $fname
;;
glb)
cp -v $fname $cen/;
echo $fname
;;
hub)
cp -v $fname $cen/;
echo $fname
;;
mng)
cp -v $fname $cen/;
echo $fname
;;
smg)
cp -v $fname $cen/;
echo $fname
;;
esac
shopt -u nocasematch #Turning off case-insensitive matching
done
This is the script I did so for. Hope this will copy the files into folders or sub-folders as I need.
Can you please check and correct me? Next I need to copy only once. Then have to avoid copying when the file is being written or moved or copied into the watch folder. After that from the destination folders upload to FTP.

Await your feedback.

michaelk 12-17-2016 11:27 AM

You do not need the ; at the end of your cp commands.
Use should use the absolute path in the cd command.
The destination directories are all subdirectories of the files directory?
Have you tested the script to see if it works?

nodir 12-17-2016 12:55 PM

When using case you can use | for or
Code:

case answer in
 Yes|yes) do_something;;
esac

Perhaps. Don't take my word for it. But that would make the code much smaller.

barani_pd 12-19-2016 03:57 AM

michaelk,

Hi.

I will rectify and remove the ";" (semicolon) at the end of "cp" commands.
Will rectify and use absolute path also.
Yes the destination directories of "files" directory.
I had tested and it is working.


All times are GMT -5. The time now is 04:33 AM.