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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-16-2016, 12:33 AM
|
#1
|
LQ Newbie
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26
Rep:
|
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.
|
|
|
12-16-2016, 02:21 AM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
|
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.
|
|
|
12-16-2016, 05:10 AM
|
#3
|
LQ Newbie
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26
Original Poster
Rep:
|
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.
|
|
|
12-16-2016, 05:17 AM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
|
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?
|
|
|
12-16-2016, 05:23 AM
|
#5
|
Moderator
Registered: Aug 2002
Posts: 26,185
|
In addition using inotifywait which is part of the inotify-tools package you can monitor the directory for changes and run the script.
|
|
|
12-16-2016, 07:05 AM
|
#6
|
LQ Newbie
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26
Original Poster
Rep:
|
Thanks guys.
I still don't have any script ready yet. That is where I need you guys' help.
|
|
|
12-16-2016, 09:44 AM
|
#7
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
|
Quote:
Originally Posted by barani_pd
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.
|
|
|
12-17-2016, 06:09 AM
|
#8
|
LQ Newbie
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26
Original Poster
Rep:
|
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.
|
|
|
12-17-2016, 11:27 AM
|
#9
|
Moderator
Registered: Aug 2002
Posts: 26,185
|
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?
|
|
|
12-17-2016, 12:55 PM
|
#10
|
Member
Registered: May 2016
Posts: 222
Rep:
|
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.
|
|
|
12-19-2016, 03:57 AM
|
#11
|
LQ Newbie
Registered: Sep 2009
Location: Bangalore, India
Distribution: RHEL, Fedora, Ubuntu
Posts: 26
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 05:13 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|