Hello everyone, i have a large number of files/folders coming in each day that are being sorted automatically to a wide variety of folders. I'm looking for a way to automatically find these files/folders and create symlinks to them all within an "incoming" folder. Searching for file age should be sufficient for finding the files, however searching for age and owner would be ideal. Then once the files/folders being linked to reach a certain age, say 5 days, remove the symlinks to them automatically from the "incoming" folder. Is this possible to do with a simple shell or python script that can be run with cron? Thanks!
views:
42answers:
2Use incron to create the symlink, then find -L
in cron to break it.
Not quite sure what you want the symlinks to but here's a first shot:
find /incoming -mtime -5 -user nr -exec ln -s '{}' /usr/local/symlinks ';'
Finds anything in /incoming
owned by nr
less than 5 days old and links it into /usr/local/symlinks
. Unfortunately ln
doesn't have a nice option to ignore something that already exists. You are better off writing a script that links things in, and at the same time you can make things much more efficient:
find /incoming -mtime -5 -user nr -print0 | xargs -0 mylink
Where mylink
has
#!/bin/bash
for i
do
link=/usr/local/symlinks/"$(basename "$i")"
[[ -L "$link" ]] || ln -s "$i" /usr/local/symlinks
done
If you want to be even more efficient you can accumulate the list of files to be linked
in an array and than link them all with one ln
command, but that's a lot of notation and I probably wouldn't bother.
To remove the symlinks that point to files older than 5 days:
find -L /usr/local/symlinks -mtime +5 -user nr -exec rm '{}' ';'
or again you can use xargs
:
find -L /usr/local/symlinks -mtime +5 -user nr -print0 | xargs -0 rm -f