LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-16-2016, 12:33 AM   #1
barani_pd
LQ Newbie
 
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26
Blog Entries: 1

Rep: Reputation: 0
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.
 
Old 12-16-2016, 02:21 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
Blog Entries: 4

Rep: Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817
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.
 
Old 12-16-2016, 05:10 AM   #3
barani_pd
LQ Newbie
 
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26

Original Poster
Blog Entries: 1

Rep: Reputation: 0
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.
 
Old 12-16-2016, 05:17 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
Blog Entries: 4

Rep: Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817
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?
 
Old 12-16-2016, 05:23 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,185

Rep: Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087
In addition using inotifywait which is part of the inotify-tools package you can monitor the directory for changes and run the script.
 
Old 12-16-2016, 07:05 AM   #6
barani_pd
LQ Newbie
 
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Thanks guys.

I still don't have any script ready yet. That is where I need you guys' help.
 
Old 12-16-2016, 09:44 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
Blog Entries: 4

Rep: Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817
Quote:
Originally Posted by barani_pd View Post
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.
 
Old 12-17-2016, 06:09 AM   #8
barani_pd
LQ Newbie
 
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26

Original Poster
Blog Entries: 1

Rep: Reputation: 0
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.
 
Old 12-17-2016, 11:27 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,185

Rep: Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087Reputation: 6087
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?
 
Old 12-17-2016, 12:55 PM   #10
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
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.
 
Old 12-19-2016, 03:57 AM   #11
barani_pd
LQ Newbie
 
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26

Original Poster
Blog Entries: 1

Rep: Reputation: 0
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.

Last edited by barani_pd; 12-19-2016 at 04:00 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
valid characters in FTP filename? FTP server that can convert them on the fly? psycroptic Linux - Software 1 05-09-2014 03:53 AM
Copy files based on filename Khandi Linux - Newbie 16 10-04-2012 12:05 PM
[SOLVED] Scheduled copy based on filename AndySocial Programming 10 06-18-2010 01:16 PM
Is there an ftp program that allows for multi-thread ftp uploads ? Want faster upload brjoon1021 Linux - Software 4 02-04-2009 06:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:13 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