I did use, instead of mv
, the cp
command to achieve it that you are able to have some logfiles right in place where a Software is running. Maybe in the different User home dir or in the app dir and do have all logs in one place as hardlinks. If you use the mv
command you lose the hard link. If you use the cp
command instead you will keep this hard link.
my code is something like:
TMP_FILE="$(mktemp "${TMPFILENAME}.XXX")"
for FILE in "${LOGFILE_DIR}"/* ; do
tail -n $MAXLINES "${FILE}" > "${TMP_FILE}"
if [ $(ls -g "${TMP_FILE}" | awk '{print $4}') -lt $(ls -g "${FILE}" | awk '{print $4}') ] ; then
cp "${TMP_FILE}" "${FILE}"
fi
done
So if the files are on the same Filesystem you may give as well some different rights to the users and in the ${LOGFILE_DIR}
you modify the length like I do.
If it is the mv
command you lose the hardlink between the files and so your second file is not more connected to the first one - maybe placed some where else.
If on the other place you don't allow someone to erase the file your logs stay together and be nice controlled via your own script.
logrotate
maybe nicer. But I am happy with this solution.
Don't be disturbed by the "" but in my case there are some files with spaces and other special letters in and If I don't do the "" around or the {} the whole lot doesn't work nice.
For example there is a Dir where older files gets automated zipped into an OLDFILE.zip
and everything that gets zipped is as well listed in File .zip_log
so the .zip_log
is in this Dir as well but in the LOGFILE_DIR
I've got with:
ln .zip_log "${LOGFILE_DIR}/USER_ZIP_log"
the equal file as it is a hard link.
logrotate
is the elegant solutiondays1=$(date +%Y-%m-%d -d "1 day ago")
days0=$(date +%Y-%m-%d)
grep -i "\|$days1\|$days0" myscript.log > myscript.log.new
mv myscript.log.new myscript.log