Shell Script to Move a file from one location to another and create folder
Hi,
I'm new to this forum but been using Ubuntu for a while.
I'm in a bit of a mix up. I'm not totally new to Ubuntu but compared to you guys i'm sure i'm an ultimate "noob".
I'm not looking for help merely a point in the right direction.
At work, i have been asked to create:
A script that will take a PDFs from a folder, move it onto another folder on the same server. (Later in the development we will look at putting it in a folder on another server by mounting it from start)
It will create a folder (Named the same as the file name) and put the file in that folder. Then delete the file from the source. (As it's moved im sure there isn't a need for that).
The script will be a CRON job which will run lets say every 5 minutes.
Could someone point me in the right direction or help me out in terms of what i need to do to start this?
So far i've tried mounting a folder from another ubuntu server without any luck by following these guides : HERE And HERE
However, i had decided to try and create the script with 2 local folder on the same server instead of mounting from a different server which i will address later.
My setup is:
Proxmox 2.3
Server 1 : 12.04 LTS - 10.0.0.4
Server 2 : 12.04LTS - 10.0.0.5 (Script will run from here) to copy over to server 1.
Both have access to internet through 2nd ethernet interface.
I only just started experimenting with Ubuntu terminal about a month ago so i may not understand a whole lot of things.
Thanks for any help,
Mike
Re: [HELP]Shell Script to Move a file from one location to another and create folder
Thanks for the reply!
iNotify looks better than as we do want it to run whenever there are changes.
However, i am a bit confused on the code you wrote. (I apologize for my stupidity but i am still only just getting used to it).
Will that basically rename the pdf file into a 'nicer' way for Ubuntu to read?
For the script, i been reading the forums and come across something like:
#!/bin/bash
source_dir= #The source of the PDF
dest_dir= #Where i want to move it to
cp -r "$source_dir/* $dest_dir/"
Does that look okay?
If so, how would i use iNotify on that? + Creating the folder based on the filename.
Re: [HELP]Shell Script to Move a file from one location to another and create folder
I think you need to take a step back and clarify your requirements - for example
1. will the source .pdf files all be in a single folder, or can they be inside subfolders?
2. will there be other filetypes in the source folder or only .pdfs?
3. what should the script do about name conflicts i.e. if the destination folder already contains a myfile.pdf, should the script overwrite it? or rename it? or rename the old file? or abort?
4. what should happen if the destination folder is not available (possibly not an issue with the local destination, but definitely something to think about if it is a mounted remote server)
5. you initially wanted to move files but now you are talking about using cp - maybe rsync would be a better option?
Re: [HELP]Shell Script to Move a file from one location to another and create folder
Quote:
Originally Posted by
Michel_Laporte
... Creating the folder based on the filename.
Can you give a few examples of filenames?
Re: [HELP]Shell Script to Move a file from one location to another and create folder
Hi Steeldriver:
1. will the source .pdf files all be in a single folder, or can they be inside subfolders?
The source is from a single folder only.
2. will there be other filetypes in the source folder or only .pdfs?
No, only PDF's as it's files being scanned that's going to the only folder
3. what should the script do about name conflicts i.e. if the destination folder already contains a myfile.pdf, should the script overwrite it? or rename it? or rename the old file? or abort?
The printer gives unique ID's to each filename (I think by assigning a random number at the end of each file) however, if the file is moved the number could be generated again which im sure i can't allow to happen. So if a conflict a rename would be the way to go
4. what should happen if the destination folder is not available (possibly not an issue with the local destination, but definitely something to think about if it is a mounted remote server)
If destination folder is unavailable im guessing we should just end the task. Does Ubuntu have a way of alerting a sysadmin of any faults? Such as an email or system message?
@Lard Nodeen
... Creating the folder based on the filename.
Example file name : SKMBT_C28013070218080
I hope this helps. :)
Mike
Re: Shell Script to Move a file from one location to another and create folder
so which part of the filename is supposed to go to the dir name? If you want automation you need to provide exact rules governing it.
SKMBT_C28013070218080.pdf => SKMBT_C28013070218080/SKMBT_C28013070218080.pdf ?
Re: Shell Script to Move a file from one location to another and create folder
Vaphell
Re: Shell Script to Move a file from one location to another and create folder
so which part of the filename is supposed to go to the dir name? If you want automation you need to provide exact rules governing it.
SKMBT_C28013070218080.pdf => SKMBT_C28013070218080/SKMBT_C28013070218080.pdf ?
Hi Vaphell,
That's correct. I want the whole filename to be the file name and the PDF moved into it like you wrote up there ^^.
So
123.pdf > 123/123.pdf
456.pdf > 456/456.pdf
Etc Etc
Re: Shell Script to Move a file from one location to another and create folder
Forget the "rename" suggestion, it won't create the missing directories for you. My mistake.