Detect a new file, and send it with mpack - linux

I have a very specific question. I am using Debian.
I have an FTP folder where an app will upload a pdf-file, and the file will be stored in ftpfolder/EMAIL_ADDRESS, and the name of the file will be CURRENT_DATE_AND_TIME.
What I want to do is whenever a new file is uploaded, in either of the EMAIL_ADDRESS folders, to send the file with mpack. As you might have guessed I want the file sent to the name of the folder, with the file attached.
So to break it down I need to:
Detect whenever a new file is uploaded
Extract the address from the foldername
Extract the filename, and attach it with mpack
Send it
I am stumped on how to approach this problem, so any suggestions will be greatly appreciated!

How about a cron that would launch a script doing all the stuff you need and then archive the files found in another folder?
#!/usr/bin/env bash
cd ftpfolder;
for email in *; do
mpack -s "New PDF file uploaded" $email/* $email;
mv $email /archivefolder;
done
Pros:
simplicity
Cons:
you have to have write permissions to move files
messing up with the original files
Note that the above script assumes only one file appears in the folder between the cron executions. If you cannot assure that (i.e. expect more than one file within a minute) you might have to loop over the folder contents.

Related

Generate hash of newly downloaded file

I'd like my bash script to perform an action every time new file is downloaded to /Downloads (generate hash of downloaded file and send it to API). So far I've been trying to make use of "inotify-tools", but it works only for newly created file and that won't do.
Script should work like this:
I download a file via browser (normal way)
Script notices new file and is executed automatically
Thanks in advance for help :D
You can use /etc/crontab to check ~/Downloads folder at startup and every n minutes. Script that will run every nth minute can do either
Keep the number of files. If number decreases script updates cache. And if number increases then gets the latest created file (or modified) and sends that file's hash to the api via curl.
Keep the name of files. If a file no longer exists, script then updates the cache of file names. If a new file appears again hashes and sends hash to the api via curl.
You can keep cache of files under /tmp.
If you can provide an example scenario I can write a simple script

Want to create a specific script for my Raspberry Pi to watch directory and do some actions

I am a complete newbie in writing scripts, I have just started a few days ago, and was already able to create simple scripts to find files, move them, delete them, etc...
I have a Raspberry Pi 4 with Raspberry Pi OS installed on it.
Now I want to create a better script, using "inotify" to monitor a specific directory and performs some actions if some specific files are found. Aaaaaand, I am a bit lost.
Here is what I have found and tested :
MONITORDIR="/my_dir"
inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
...
With this, I can generate an action whenever any new file appeared in my folder.
What I want :
If a new file with specific name (not the complete name, but just a part of the name of the file) with a specific .pdf extension is detected in the directory,
Then, move this file in another directory
And send an email using postfix, including the name of this new file, without the complete path of the file
Any help with this will be good for me, since I am a beginner, I know have a lot to learn, and I am sure I will.
Thank you !

Node.js file rotation

I have a process that periodically gets files from a server and copy them with SFTP to a local directory. It should not overwrite the file if it already exists. I know with something like Winston I can automatically rotate the log file when it fills up, but in this case I need a similar functionality to rotate files if they already exist.
An example:
The routine copies a remote file called testfile.txt to a local directory. The next time it's run the same remote file is found and copied. But now I want to rename the first testfile.txt to testfile.txt.0 so it's not overwritten. And so on - after a while I'd have a directory of files with the name testfile.txt.N and the most recent testfile.txt.
What you can do is you can append date and time on the file name that gives every filename a unique name and also helps you archive it.
For example you text.txt can be either 20170202_181921_test.txt or test_20170202_181921.txt
You can use a JavaScript Date Object to get date and time.
P.S show your code of downloading files so that I can add more to that.

bash script for creating and then downloading the links

Hellooo.
So I'm wanting to make a script for my girlfriend that uses an external file to append words to a URL, then download the links and iterate.
The awkward thing is she doesn't want to tell me too much (I suspect the result of using the script will be for my benefit :P), so I'm not certain about the function, kind of guessing.
The aim is to get the script to contain a base URL. The script will iterate through an external file that contains a list of word and then append each word to the link. Then the script will then open that link. Then iterate through, append, open etc.
Can someone help me out a bit with this? I'm a bit new to scripting.
Should I set up an external file to hold the base url and then refer to that as well?
I'm thinking somthing along the lines of:
url=$(grep * url.txt)
for i in $(cat file.txt);
do
>> $url
wget $url
done
What and how much do I need to change and add?
Thanks for any help.
I have a file named source which has below content in it :
which-2.16.tar.gz
which-2.17.tar.gz
which-2.21.tar.gz
I wrote a script named downloader with the below content :
#!/bin/bash
url="http://ftp.gnu.org/gnu/which" #source url
while read line
do
wget "$url/$line" #download url = source url + file name from the file
done <source #feeding filenames from the source file.
On running downloader will download the files mentioned in source file from the ftp site mentioned in url. Voila !!
I guess you could employ a similar concept.

A simple way to save a file version to a child folder and generate/increment a log?

I looked to TortoiseSVN and TortoiseHG... bit too much for me and I don't want to setup a structure where the files will be saved. All I want is:
When saving a file, first saves the file.
Then if there isn't in file's folder a sub-folder called "History", it creates it.
Then Copies the file to "History" and gives it the first available number suffix as "_[version]".
Then, if is no txt file, in the file's folder, called "[filename].[extension]_Notes.txt", it creates it and adds a line with:
Version: [version] " of the " [filename] " saved at: " [date]
Now how would this run from any application? Dunno. Maybe have a button in the file save dialog?
I'm asking this because I'm just one guy working and not on huge projects and 99% of the time I would just like to have a localized versioning, set on a local sub-folder and a log to track the versions and dates.
Is this super easy with Tortoise and I'm being a mule?
Cheers

Resources