I'd like to run a nightly cron job that deletes all the files in a folder that haven't been accessed in a week or more. What is the most efficient way to do this in bash?
Shell – How to delete all files in a folder that haven’t been accessed in a certain amount of time
crondatefilesshell-script
Related Solutions
Edit the /etc/sudoers
file (use visudo
!) and add an entry that allows the shell user to have sufficient privileges to run a specific command, without having to enter a password. If you use a script, make sure the script cannot by edited by anyone but root.
In /etc/sudoers
, where shelluser
is the shell user name:
shelluser ALL=NOPASSWD: /usr/bin/clean-up-sftp-temp-directory
In a /usr/bin/clean-up-sftp-temp-directory
script, you can put something like:
#!/bin/sh
rm -f /home/sftpuser/will-be-deleted/*
After making the script executable, you should be able to call sudo clean-up-sftp-temp-directory
and add it to the shell user's crontab.
Not very efficient, but you could do:
find /folder/downloading -type f -exec sh -c '
for file do
lsof -F a "$file" | grep -q w || mv "$file" /folder/downloaded
done' sh {} +
That is check that the file is not listed with a w
rite a
ccess mode in the l
is
t of o
pen f
iles before m
ov
ing.
The psmisc
implementation of fuser
as typically found on Linux-based operating systems has a -w
function (to check for files open for writing) but unfortunately it only work with -k
to kill the corresponding processes. However, it seems you can still use it by using the pseudo-signal 0 that does nothing:
find /folder/downloading -type f -exec sh -c '
for file do
fuser -s -w -k -0 "$file" || mv "$file" /folder/downloaded
done' sh {} +
Remove the -s
(or even replace it with -v
) if you want to see what process(es) is(are) preventing the move.
Note that if you're not running those commands as super-user, you will only get information about your processes. If the processes downloading the files are running as a different user, they will remain undetected.
Also note that unless you're moving the files to a different file system, moving the files will not prevent whatever process is currently writing to the file from finishing writing to it.
However, depending of what they've been designed to do afterwards, they might be confused if after they finish writing, the file is no longer there (for instance if they want to change some attributes of the file after downloading it and do so not via the file descriptor (like chmod()
vs fchmod()
, or utimes()
which cannot be done via a file descriptor)).
Best Answer
You want the
find
tool.(This will delete all files (only regular ones, no pipes, special devices, directories, symbolic links) in the given folder and all subdirectories (recursively) where the last access time is longer than 7 days ago.)