Symlink files later than X age, then delete symlink after file age?

I have a large number of files / folders coming in every day that are sorted automatically into a wide variety of folders. I'm looking for a way to automatically search for these files / folders and create symlinks for them in the "in" folder. Searching for file age should be sufficient to search for files, however searching for age and owner would be ideal. Then, once the files / folders are associated with a certain age, say 5 days later, remove the symlinks automatically from the "in" folder. Is it possible to do this with a simple shell or python script that can be run using cron? Thanks!

+2


a source to share


2 answers


Use incron to create a symlink, then find -L

cron to break it.



+2


a source


Not really sure if you want to use symbolic links, but here's the first snapshot:

find /incoming -mtime -5 -user nr -exec ln -s '{}' /usr/local/symlinks ';'

      

He finds something in /incoming

owned nr

less than 5 days, and associates it with /usr/local/symlinks

. Unfortunately, ln

there is no way to ignore what already exists. You're better off writing a script that ties things together, 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 more efficient, you can accumulate a list of files to be concatenated in an array and concatenate them all with one command ln

, but that's a lot of notation and I probably wouldn't bother.

To remove symbolic links pointing 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

      

+1


a source







All Articles