How to hold or copy files at run time in CENTOS 7 - linux

I have a tool that converts COBOL files to C++.
When I run the command, it generates the.cpp file from the.cbl file but deletes it at run time.
It is not holding the files or not saving the file in the designated folder, despite the fact that I can see that the.cpp file is created but destroyed within seconds.
I'm using CentOS 7.
Can somebody tell me how to keep that.cpp file or copy it at runtime to a different location?

You can use inotifywait to monitor the directory, where the CPP files are created. Whenever a CREATE event occurs, you can link the file into a backup directory.
#! /bin/bash
test -d backup || mkdir backup
inotifywait --monitor --event CREATE --format %f . |
while read file; do
ln "$file" backup/"$file"
done

Related

Create new TAR file in current directory to a new directory without using "mv" (shell script)

I'm currently setting up a backupmanager to automatically archive directories from webserver. I'm searching for an answer how to create a new TAR file of the current directory to a new (different) directory without using mv after the archiving process. See my command:
dcreate=$(date +%Y_%d_%m) tar -cvpzf backup_$dcreate.tar.gz plugins/folder_to_archive/
This command works fine, but i'm struggeling now on how to move it to a new directory directly after the archiv process is terminated, for example:
plugins/plugin_name/ to plugins/backups/
Any help appreciated.
Regards
The -f option of tar is the destination file for the archive; it can be anywhere you want, i.e., you can change
-cvpzf "backup_$dcreate.tar.gz"
to
-cvpzf "plugins/backups/backup_$dcreate.tar.gz"
if you want your new archive to be created in plugins/backups/

Download file from linux server once it is created

I recently started to work with Linux server, I am very new. My CUDA/C++ program solves 2D differential equation and writes down output every, say, 1000 time steps. It happens roughly every minute. Is it possible to automatically download files to my PC once they generated on the Linux server, or save them directly to my PC? This would significantly accelerate my work since now I have to wait for my program to finish all the calculations and then download it manually. I also typically use 6 GPUS at the same time, they produce output in different specified folders on the LINUX server (say, folders 0, 1,2,3,4,5)
You can use inotify
In Debian or Ubuntu install the package :
apt-get install inotify-tools
Create two script, first for reading new file in directory, second for copying file to your computer
inotifywait_script.sh
#!/bin/bash
# Path to check :
DIR="./files"
while NEW_FILE=$(inotifywait -r -e create --format %w%f $DIR)
do
# Sctipt executed when new file is created :
./script_cp.sh "$NEW_FILE"
done
Used inotifywait options :
-e : Listen for specific event(s) only (here just creating event)
-r : Watch all subdirectories of any directories passed as arguments
--format : %w => Path %f => File
script_cp.sh
#!/bin/bash
echo "Copy file $1"
scp "$1" user#hostname:/path_to_save
You can use scp, rsync or other system to copying files

How can I automatically rename, copy and delete files in linux for my ip camera webcam?

I have an ip camera that automatically ftps images every few seconds to directory on my linux Ubuntu Server web server. I'd like to make a simple webcam page that references a static image and just refreshes every few seconds. The problem is that my ipcamera's firmware automatically names every file with a date_time.jpg type filename, and does not have the option to overwrite with the same file name over and over.
I'd like to have a script running on my linux machine to automatically copy a new file that has been ftp'd into a directory into a different directory, rename it in the process and then delete the original.
Regards,
Glen
I made a quick script, you would need to uncomment the rm -f line to make it delete things :)
it currently prints the command it would have run, so you can test with higher confidence.
You also need to set THE WORK_DIR and DEST_DIR variables near the top of the script.
#!/bin/bash
#########################
# configure vars
YYYYMMDD=`date +%Y%m%d`
WORK_DIR=/Users/neil/linuxfn
DEST_DIR=/Users/neil/linuxfn/dest_dir
##########################
LATEST=`ls -tr $WORK_DIR/$YYYYMMDD* 2>/dev/null | tail -1`
echo "rm -f $DEST_DIR/image.jpg ; mv $LATEST $DEST_DIR/image.jpg"
#rm -f $DEST_DIR/image.jpg ; mv $LATEST $DEST_DIR/image.jpg
This give me the following output when i run it on my laptop:
mba1:linuxfn neil$ bash renamer.sh
rm -f /Users/neil/linuxfn/dest_dir/image.jpg ; mv /Users/neil/linuxfn/20150411-2229 /Users/neil/linuxfn/dest_dir/image.jpg
Inotify (http://en.wikipedia.org/wiki/Inotify) can be set up to do as you ask, but it would probably be better to use a simple web script (PHP, Python, Perl, etc.) to serve the latest file from the directory, instead.

Copy directories on schedule linux

I am looking for help with a script that will allow me to copy the contents of a directory, or the whole directory to another directory on a schedule or when new files arrive in the source.
For example:
/stuff/folder1/file.txt
Copy to:
/stuff/folder2/file.txt
either when new files arrive or on a recurring schedule.
I do use a Centos machine.
You can use the following program to update folder2 whenever you save new files or update folder1:
while inotifywait -r -e modify -e move -e create -e delete; do
cp -r /stuff/folder1/. /stuff/folder2/
done
For the schedule thing I would add cp -r /stuff/folder1/. /stuff/folder2/ into a cron job. Instead of cp you can also use rsync. Please also have a look on the manpage of inotifywait.
Note: The above script will start the copy after the first file was altered inside the directory folder1. If you modify many files in folder1 in the same time, you might want to put a sleep command inside the while loop. But in this case it is better to add the copy command at the end of the program which alters the files of folder1.

linux server create symbolic links from filenames

I need to write a shell script to run as a cron task, or preferably on creation of a file in a certain folder.
I have an incoming and an outgoing folder (they will be used to log mail). There will be files created with codes as follows...
bmo-001-012-dfd-11 for outgoing and 012-dfd-003-11 for incoming. I need to filter the project/client code (012-dfd) and then place it in a folder in the specific project folder.
Project folders are located in /projects and follow the format 012-dfd. I need to create symbolic links inside the incoming or outgoing folders of the projects, that leads to the correct file in the general incoming and outgoing folders.
/incoming/012-dfd-003-11.pdf -> /projects/012-dfd/incoming/012-dfd-003-11.pdf
/outgoing/bmo-001-012-dfd-11.pdf -> /projects/012-dfd/outgoing/bmo-001-012-dfd-11.pdf
So my questions
How would I make my script run when a file is added to either incoming or outgoing folder
Additionally, is there any associated disadvantages with running upon file modification compared with running as cron task every 5 mins
How would I get the filename of recent (since script last run) files
How would I extract the code from the filename
How would I use the code to create a symlink in the desired folder
EDIT: What I ended up doing...
while inotifywait outgoing; do find -L . -type l -delete; ls outgoing | php -R '
if(
preg_match("/^\w{3}-\d{3}-(\d{3}-\w{3})-\d{2}(.+)$/", $argn, $m)
&& $m[1] && (file_exists("projects/$m[1]/outgoing/$argn") != TRUE)
){
`ln -s $(pwd)/outgoing/$argn projects/$m[1]/outgoing/$argn;`;
}
'; done;
This works quite well - cleaning up deleted symlinks also (with find -L . -type l -delete) but I would prefer to do it without the overhead of calling php. I just don't know bash well enough yet.
Some near-answers for your task breakdown:
On linux, use inotify, possibly through one of its command-line tools, or script language bindings.
See above
Assuming the project name can be extracted thinking positionally from your examples (meaning not only does the project name follows a strict 7-character format, but what precedes it in the outgoing file also does):
echo `basename /incoming/012-dfd-003-11.pdf` | cut -c 1-7
012-dfd
echo `basename /outgoing/bmo-001-012-dfd-11.pdf`| cut -c 9-15
012-dfd
mkdir -p /projects/$i/incoming/ creates directory /projects/012-dfd/incoming/ if i = 012-dfd,
ln -s /incoming/foo /projects/$i/incoming/foo creates a symbolic link from the latter argument, to the preexisting, former file /incoming/foo.
How would I make my script run when a file is added to either incoming or outgoing folder
Additionally, is there any associated disadvantages with running upon file modification compared with running as cron task
every 5 mins
If a 5 minutes delay isn't an issue, I would go for the cron job (it's easier and -IMHO- more flexible)
How would I get the filename of recent (since script last run) files
If your script runs every 5 minutes, then you can tell that all the files created in between now (and now - 5 minutes) are newso, using the command ls or find you can list those files.
How would I extract the code from the filename
You can use the sed command
How would I use the code to create a symlink in the desired folder
Once you have the desired file names, you can usen ln -s command to create the symbolic link

Resources