A crontab to move completed uploads from one dir to another? - linux

I'm using the following crontab, once an hour, to move any files with the .mp3 extension from the dir "webupload" to the dir "complete" :
60 * * * * find usr/webupload -type f -maxdepth 1 -name "*.mp3" -exec mv {} usr/webupload/complete \;
The problem is that "webupload" contains lots of partial files being transferred.
I've read about a lot of different ways to achieve this but I think i'm more confused now than I was when I started!
What is the best practice or easiest way to only move the completed uploads?
Many thanks :)

It's going to be hard to tell when a file is completely written unless it is renamed when the download is completed, but you could change your find command and add -mmin +1 so that it only looks for files which have been modified more than 1 minutes ago (meaning that download is likely completed). Also, you should use / at the beginning of your paths rather than the relative paths your using:
60 * * * * find /usr/webupload -type f -mmin +1 -maxdepth 1 -name "*.mp3" -exec mv {} /usr/webupload/complete \;
You could obviously make the modification time longer (eg. 10 minutes -mmin +10) if you want to be more certain that the file has been downloaded.

Related

Task Scheduler Script copy NEW files only - Synology

Similar to this question I want to make a task scheduler script to copy NEW files (last 24h) to a new folder.
I try to use this code:
find /volume1/start/ -mtime -1 -type f -exec cp -r {} /volume1/target/ \;
but it delivers a 1kb filename.pdf#SynoEAStream file instead of the file itself.
How can I fix that?
Ok actually it seems to work as supposed I actually just had the wrong file modification date in the files where it didn't work. The script additionally copies some "useless" #SynoEAStream files whicht I now avoid by only looking for pdf-files which ist what I wanted.
find /volume1/TestScan/start/ -mtime -1 -type f -iname '*.pdf' -exec rsync -r {} /volume1/TestScan/ziel/ \;
Maybe its helpful for someone

What does this cron do for each command?

find /home/root/public_html/_sess -type f -mtime +3 -name 'sess-*' -execdir rm -- {} \;
I feel like I understand find , but I'm not 100% sure what -type is, I think that is the file type f not sure yet -mtime I feel like -mtime means a time setting of some sort, and +3 means maybe that time setting +3? , I feel like -execdir rm -- just means remove the files in the directory call -name 'sess-*' as well. But again not 100% sure of all the command elements within and wanted to get clarification.
You can do man find to get information on how Linux find works and all the options you can pass to it.
In this case, the command is using the Linux find utility to search for files in the /home/root/public_html/_sess directory with the following options:
-file f - searches for files of filetype f, which is regular files (not directories, links, etc)
-mtime +3 - searches for files modified more than 3 days ago (the + is for more than, -3 would be less than 3 days old)
-name 'sess-* - searches for files whose name matches the regex sess-* (name starts with "sess-")
-execdir <command> {}; - executes <command> on each file that find finds in the directory that the file was found in, in this case <command> is rm to remove the file
So in summary, this job searches for files located in a certain directory, whose names start with a specific string, and which are more than 3 days old, and deletes them.

Compressing logs everyday

I'm struggling with compressing my logs. I have a simple script which runs everynight
find /directory/logs -type f -mmin +1440 -print -exec gzip {} \;
But sometimes it skips my logs with different ending than *.log. For example it don't compress logs with *.log.1 *.log.0.lck etc.
Any ideas?
I suppose that you just don't use correctly the find command.
-mmin +1440 - find all files except last 1440min (24 hours)
-mmin -1440 or -mmin 1440 - find all files created in last 1440min (24 hours)
You can use "-mtime n", from man:
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.
So for you:
find /directory/logs -type f -mtime 1 -print -exec gzip {} \;

find files which have been modified in the last 30 minutes in Linux

how to find files based upon time information, such as creation, modified and accessed. It is useful to find files before a certain time, after a certain time and between two times. what command in Linux would i have to use ?
I understand to find setuid files on linux computers i would have to use :
find / -xdev ( -perm -4000 ) -type f -print0 | xargs -0 ls -l
How do i check for files which have been modified in the last 30 minutes. (I created a new file called FILE2)
Just add -mtime -30m. I might be wrong about the actual syntax, but you get the idea. See man find.
Answer on your question is
find . -cmin -30 -exec ls -l {} \;

In linux shell, How to cp/rm files by time?

In linux shell, When I run
ls -al -t
that show the time of files.
How to cp/rm files by time? just like copy all the files that created today or yesterday. Thanks a lot.
Depending on what you actually want to do, find provides -[acm]time options for finding files by accessed, created or modified dates, along with -newer and -min. You can combine them with -exec to copy, delete, or whatever you want to do. For example:
find -maxdepth 1 -mtime +1 -type f -exec cp '{}' backup \;
Will copy all the regular files in the current directory more than 1 day old to the directory backup (assuming the directory backup exists).
Simple Example
find /path/to/folder/ -mtime 1 -exec rm {} \; // Deletes all Files modified yesterday
For more examples google for bash find time or take a look here

Resources